apahilaj commited on
Commit
b4b5a4d
·
1 Parent(s): 29f30e1

app.py chat

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -156,19 +156,19 @@ def load_db(file, k):
156
 
157
  chat_history = [] # initialize chat history
158
 
159
- def chatbot_conversation(interim_responses, pdf_file):
160
  global chat_history
161
- question = interim_responses[-1]["message"] if interim_responses else "Hello"
162
- a = load_db(pdf_file, 3)
163
- r = a.invoke({"question": question, "chat_history": chat_history})
164
  match = re.search(r'Helpful Answer:(.*)', r['answer'])
165
  if match:
166
  helpful_answer = match.group(1).strip()
167
  # Extend chat history with the current question and answer
168
- chat_history.extend([(question, helpful_answer)])
169
- return helpful_answer, None
170
  else:
171
- return "No helpful answer found.", None
172
 
173
- iface = gr.ChatInterface(fn=chatbot_conversation, inputs=["text", "file"], outputs="text")
174
  iface.launch(share=True)
 
156
 
157
  chat_history = [] # initialize chat history
158
 
159
+ def greet(messages):
160
  global chat_history
161
+ user_message = messages[-1]['content']
162
+ a = load_db("temp.pdf", 3) # assuming you've uploaded the file and saved it as "temp.pdf"
163
+ r = a.invoke({"question": user_message, "chat_history": chat_history})
164
  match = re.search(r'Helpful Answer:(.*)', r['answer'])
165
  if match:
166
  helpful_answer = match.group(1).strip()
167
  # Extend chat history with the current question and answer
168
+ chat_history.extend([{"role": "user", "content": user_message}, {"role": "assistant", "content": helpful_answer}])
169
+ return [{"role": "assistant", "content": helpful_answer}]
170
  else:
171
+ return [{"role": "assistant", "content": "No helpful answer found."}]
172
 
173
+ iface = gr.ChatInterface(fn=greet, inputs=gr.Textbox(), outputs=gr.ChatResponse(continuous_update=True))
174
  iface.launch(share=True)