Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -30,32 +30,40 @@ prompt = PromptTemplate(template=template, input_variables=["context", "question
|
|
30 |
# upload PDF
|
31 |
pdf_file = st.file_uploader("Upload your pdf",type="pdf")
|
32 |
question = st.text_input("Ask a question:")
|
33 |
-
if pdf_file is not None:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
llm=llm,
|
60 |
chain_type="stuff",
|
61 |
retriever=db.as_retriever(),
|
@@ -63,10 +71,52 @@ if pdf_file is not None:
|
|
63 |
return_source_documents=False,
|
64 |
combine_docs_chain_kwargs={'prompt': prompt}
|
65 |
)
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
|
|
|
30 |
# upload PDF
|
31 |
pdf_file = st.file_uploader("Upload your pdf",type="pdf")
|
32 |
question = st.text_input("Ask a question:")
|
33 |
+
if pdf_file is not None:
|
34 |
+
try:
|
35 |
+
# Save the uploaded file to a temporary file
|
36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
37 |
+
temp_file.write(pdf_file.read())
|
38 |
+
temp_file_path = temp_file.name
|
39 |
+
|
40 |
+
# Load and process the PDF
|
41 |
+
loader = PyPDFLoader(temp_file_path)
|
42 |
+
pdf_data = loader.load()
|
43 |
+
st.write(f"Loaded {len(pdf_data)} documents from PDF.")
|
44 |
+
|
45 |
+
# Split the text into chunks
|
46 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
47 |
+
docs = text_splitter.split_documents(pdf_data)
|
48 |
+
st.write(f"Split text into {len(docs)} chunks.")
|
49 |
+
|
50 |
+
# Create a Chroma vector store
|
51 |
+
embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
|
52 |
+
db = Chroma.from_documents(docs, embeddings)
|
53 |
+
|
54 |
+
# Initialize message history for conversation
|
55 |
+
message_history = ChatMessageHistory()
|
56 |
+
|
57 |
+
# Memory for conversational context
|
58 |
+
memory = ConversationBufferMemory(
|
59 |
+
memory_key="chat_history",
|
60 |
+
output_key="answer",
|
61 |
+
chat_memory=message_history,
|
62 |
+
return_messages=True,
|
63 |
+
)
|
64 |
+
|
65 |
+
# Create a chain that uses the Chroma vector store
|
66 |
+
chain = ConversationalRetrievalChain.from_llm(
|
67 |
llm=llm,
|
68 |
chain_type="stuff",
|
69 |
retriever=db.as_retriever(),
|
|
|
71 |
return_source_documents=False,
|
72 |
combine_docs_chain_kwargs={'prompt': prompt}
|
73 |
)
|
74 |
+
|
75 |
+
if question:
|
76 |
+
# Process the question
|
77 |
+
res = chain({"question": question})
|
78 |
+
answer = res.get("answer", "No answer found.")
|
79 |
+
st.write(f"Answer: {answer}")
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
st.error(f"An error occurred: {e}")
|
83 |
+
# if pdf_file is not None:
|
84 |
+
# # Load and process the PDF
|
85 |
+
# loader = PyPDFLoader(pdf_file)
|
86 |
+
# pdf_data = loader.load()
|
87 |
+
|
88 |
+
# # Split the text into chunks
|
89 |
+
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
90 |
+
# docs = text_splitter.split_documents(pdf_data)
|
91 |
+
|
92 |
+
# # Create a Chroma vector store
|
93 |
+
# embeddings = HuggingFaceEmbeddings(model_name="embaas/sentence-transformers-multilingual-e5-base")
|
94 |
+
# db = Chroma.from_documents(docs, embeddings)
|
95 |
+
|
96 |
+
# # Initialize message history for conversation
|
97 |
+
# message_history = ChatMessageHistory()
|
98 |
+
|
99 |
+
# # Memory for conversational context
|
100 |
+
# memory = ConversationBufferMemory(
|
101 |
+
# memory_key="chat_history",
|
102 |
+
# output_key="answer",
|
103 |
+
# chat_memory=message_history,
|
104 |
+
# return_messages=True,
|
105 |
+
# )
|
106 |
+
|
107 |
+
# # Create a chain that uses the Chroma vector store
|
108 |
+
# chain = ConversationalRetrievalChain.from_llm(
|
109 |
+
# llm=llm,
|
110 |
+
# chain_type="stuff",
|
111 |
+
# retriever=db.as_retriever(),
|
112 |
+
# memory=memory,
|
113 |
+
# return_source_documents=False,
|
114 |
+
# combine_docs_chain_kwargs={'prompt': prompt}
|
115 |
+
# )
|
116 |
+
# if question:
|
117 |
+
# # Process the question
|
118 |
+
# res = chain({"question": question})
|
119 |
+
# answer = res["answer"]
|
120 |
+
# st.write(f"{answer}")
|
121 |
|
122 |
|