chalisesagun commited on
Commit
5f5357d
·
verified ·
1 Parent(s): e48601c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -32
app.py CHANGED
@@ -23,51 +23,55 @@ if api_key:
23
 
24
  if uploaded_file:
25
  # Load and process the document
26
- with st.spinner("Processing document..."):
27
- # Save the uploaded file temporarily
28
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
29
- tmp_file.write(uploaded_file.getvalue())
30
- tmp_file_path = tmp_file.name
 
31
 
32
- # Use the temporary file path with PyPDFLoader
33
- loader = PyPDFLoader(tmp_file_path)
34
- documents = loader.load()
35
 
36
- # Remove the temporary file
37
- os.unlink(tmp_file_path)
38
 
39
- # Split the document into chunks
40
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
41
- chunks = text_splitter.split_documents(documents)
42
 
43
- # Generate embeddings and store them in a vector database
44
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
45
- vector_store = FAISS.from_documents(chunks, embeddings)
46
 
47
- st.success("Document processed successfully!")
 
 
 
48
 
49
  # Step 3: Ask Questions About the Document
50
  st.subheader("💬 Chat with Your Document")
51
  user_query = st.text_input("Ask a question:")
52
 
53
  if user_query:
54
- # Set up the RAG pipeline with DeepSeek LLM
55
- retriever = vector_store.as_retriever()
56
- llm = ChatOpenAI(
57
- model="deepseek-chat",
58
- openai_api_key=api_key,
59
- openai_api_base="https://api.deepseek.com/v1",
60
- temperature=0.85,
61
- max_tokens=4000 # Ensure compliance with DeepSeek's token limit
62
- )
63
- qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
 
64
 
65
- # Generate response
66
- with st.spinner("Generating response..."):
67
- try:
68
  response = qa_chain.run(user_query)
69
  st.write(f"**Answer:** {response}")
70
- except Exception as e:
71
- st.error(f"Error: {e}")
72
  else:
73
  st.warning("Please enter your DeepSeek API key to proceed.")
 
23
 
24
  if uploaded_file:
25
  # Load and process the document
26
+ try:
27
+ with st.spinner("Processing document..."):
28
+ # Save the uploaded file temporarily
29
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
30
+ tmp_file.write(uploaded_file.getvalue())
31
+ tmp_file_path = tmp_file.name
32
 
33
+ # Use the temporary file path with PyPDFLoader
34
+ loader = PyPDFLoader(tmp_file_path)
35
+ documents = loader.load()
36
 
37
+ # Remove the temporary file
38
+ os.unlink(tmp_file_path)
39
 
40
+ # Split the document into chunks
41
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
42
+ chunks = text_splitter.split_documents(documents)
43
 
44
+ # Generate embeddings and store them in a vector database
45
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
46
+ vector_store = FAISS.from_documents(chunks, embeddings)
47
 
48
+ st.success("Document processed successfully!")
49
+ except Exception as e:
50
+ st.error(f"Error processing document: {e}")
51
+ st.stop()
52
 
53
  # Step 3: Ask Questions About the Document
54
  st.subheader("💬 Chat with Your Document")
55
  user_query = st.text_input("Ask a question:")
56
 
57
  if user_query:
58
+ try:
59
+ # Set up the RAG pipeline with DeepSeek LLM
60
+ retriever = vector_store.as_retriever()
61
+ llm = ChatOpenAI(
62
+ model="deepseek-chat",
63
+ openai_api_key=api_key,
64
+ openai_api_base="https://api.deepseek.com/v1",
65
+ temperature=0.85,
66
+ max_tokens=1000 # Adjust token limit for safety
67
+ )
68
+ qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
69
 
70
+ # Generate response
71
+ with st.spinner("Generating response..."):
 
72
  response = qa_chain.run(user_query)
73
  st.write(f"**Answer:** {response}")
74
+ except Exception as e:
75
+ st.error(f"Error generating response: {e}")
76
  else:
77
  st.warning("Please enter your DeepSeek API key to proceed.")