Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,51 +23,55 @@ if api_key:
|
|
| 23 |
|
| 24 |
if uploaded_file:
|
| 25 |
# Load and process the document
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
response = qa_chain.run(user_query)
|
| 69 |
st.write(f"**Answer:** {response}")
|
| 70 |
-
|
| 71 |
-
|
| 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.")
|