Update app.py
Browse files
app.py
CHANGED
@@ -23,6 +23,7 @@ class AdvancedPdfChatbot:
|
|
23 |
|
24 |
self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
25 |
self.qa_chain = None
|
|
|
26 |
self.template = """
|
27 |
You are a study partner assistant, students give you pdfs
|
28 |
and you help them to answer their questions.
|
@@ -41,6 +42,7 @@ class AdvancedPdfChatbot:
|
|
41 |
documents = loader.load()
|
42 |
texts = self.text_splitter.split_documents(documents)
|
43 |
self.db = FAISS.from_documents(texts, self.embeddings)
|
|
|
44 |
self.setup_conversation_chain()
|
45 |
|
46 |
def setup_conversation_chain(self):
|
@@ -57,6 +59,13 @@ class AdvancedPdfChatbot:
|
|
57 |
result = self.qa_chain({"question": query})
|
58 |
return result['answer']
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# Initialize the chatbot
|
61 |
pdf_chatbot = AdvancedPdfChatbot(openai_api_key)
|
62 |
|
@@ -76,6 +85,10 @@ def clear_chatbot():
|
|
76 |
pdf_chatbot.memory.clear()
|
77 |
return []
|
78 |
|
|
|
|
|
|
|
|
|
79 |
# Create the Gradio interface
|
80 |
with gr.Blocks() as demo:
|
81 |
gr.Markdown("# PDF Chatbot")
|
@@ -93,6 +106,7 @@ with gr.Blocks() as demo:
|
|
93 |
|
94 |
msg.submit(respond, inputs=[msg, chatbot_interface], outputs=[msg, chatbot_interface])
|
95 |
clear.click(clear_chatbot, outputs=[chatbot_interface])
|
|
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
demo.launch()
|
|
|
23 |
|
24 |
self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
25 |
self.qa_chain = None
|
26 |
+
self.pdf_path = None
|
27 |
self.template = """
|
28 |
You are a study partner assistant, students give you pdfs
|
29 |
and you help them to answer their questions.
|
|
|
42 |
documents = loader.load()
|
43 |
texts = self.text_splitter.split_documents(documents)
|
44 |
self.db = FAISS.from_documents(texts, self.embeddings)
|
45 |
+
self.pdf_path = pdf_path
|
46 |
self.setup_conversation_chain()
|
47 |
|
48 |
def setup_conversation_chain(self):
|
|
|
59 |
result = self.qa_chain({"question": query})
|
60 |
return result['answer']
|
61 |
|
62 |
+
def get_pdf_path(self):
|
63 |
+
# Return the stored PDF path
|
64 |
+
if self.pdf_path:
|
65 |
+
return self.pdf_path
|
66 |
+
else:
|
67 |
+
return "No PDF uploaded yet."
|
68 |
+
|
69 |
# Initialize the chatbot
|
70 |
pdf_chatbot = AdvancedPdfChatbot(openai_api_key)
|
71 |
|
|
|
85 |
pdf_chatbot.memory.clear()
|
86 |
return []
|
87 |
|
88 |
+
def get_pdf_path():
|
89 |
+
# Call the method to return the current PDF path
|
90 |
+
return pdf_chatbot.get_pdf_path()
|
91 |
+
|
92 |
# Create the Gradio interface
|
93 |
with gr.Blocks() as demo:
|
94 |
gr.Markdown("# PDF Chatbot")
|
|
|
106 |
|
107 |
msg.submit(respond, inputs=[msg, chatbot_interface], outputs=[msg, chatbot_interface])
|
108 |
clear.click(clear_chatbot, outputs=[chatbot_interface])
|
109 |
+
path_button.click(get_pdf_path, outputs=[pdf_path_display])
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
demo.launch()
|