mckplus commited on
Commit
73e42b6
1 Parent(s): b21235c

Update DocuChat.py

Browse files
Files changed (1) hide show
  1. DocuChat.py +17 -9
DocuChat.py CHANGED
@@ -49,18 +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
- 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
- self.file_input.save("/.cache/temp.pdf")
 
 
 
60
  prompt_text = self.remove_empty_lines(input)
61
  if prompt_text:
62
- result = self.qa(file="/.cache/temp.pdf", query=prompt_text)
63
  self.chatbox.append({"AI": result})
 
 
 
 
 
64
 
65
  @staticmethod
66
  def remove_empty_lines(text):
 
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):