Spaces:
Sleeping
Sleeping
Update DocuChat.py
Browse files- DocuChat.py +21 -21
DocuChat.py
CHANGED
@@ -49,26 +49,26 @@ class LangchainConversation:
|
|
49 |
self.chatbox.param.watch(self._chat, 'value')
|
50 |
self.chat_history = [] # Chat history to store previous queries and responses
|
51 |
|
52 |
-
def _chat(self, event):
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
@staticmethod
|
74 |
def remove_empty_lines(text):
|
@@ -104,7 +104,7 @@ def _chat(self, event):
|
|
104 |
|
105 |
layout = pn.Column(
|
106 |
pn.pane.Markdown("""
|
107 |
-
#
|
108 |
AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
|
109 |
## How it works:
|
110 |
1) Upload a PDF
|
|
|
49 |
self.chatbox.param.watch(self._chat, 'value')
|
50 |
self.chat_history = [] # Chat history to store previous queries and responses
|
51 |
|
52 |
+
def _chat(self, event):
|
53 |
+
user_message = event.new[-1]
|
54 |
+
input = user_message.get("User")
|
55 |
+
if input is None:
|
56 |
+
return
|
57 |
+
os.environ["OPENAI_API_KEY"] = self.openaikey.value
|
58 |
+
if self.file_input.value is not None:
|
59 |
+
file_path = "/.cache/temp.pdf"
|
60 |
+
self.file_input.save(file_path)
|
61 |
+
# Check if the uploaded file is a PDF
|
62 |
+
if self.file_input.filename.lower().endswith('.pdf'):
|
63 |
+
prompt_text = self.remove_empty_lines(input)
|
64 |
+
if prompt_text:
|
65 |
+
result = self.qa(file=file_path, query=prompt_text)
|
66 |
+
self.chatbox.append({"AI": result})
|
67 |
+
else:
|
68 |
+
# Delete the non-PDF file from cache
|
69 |
+
os.remove(file_path)
|
70 |
+
# Display an error message in the chatbox
|
71 |
+
self.chatbox.append({"AI": "Error: Only PDF files are allowed. Please upload a valid PDF file."})
|
72 |
|
73 |
@staticmethod
|
74 |
def remove_empty_lines(text):
|
|
|
104 |
|
105 |
layout = pn.Column(
|
106 |
pn.pane.Markdown("""
|
107 |
+
# Docu-Chat
|
108 |
AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
|
109 |
## How it works:
|
110 |
1) Upload a PDF
|