manabb commited on
Commit
1bd8cbd
·
verified ·
1 Parent(s): 22603de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -90,14 +90,18 @@ def chat(message, history):
90
  #============starting extract_docx_text
91
  def respond(message, history):
92
  answer = qa_chain.invoke(message)
93
- docs = retriever.invoke(message) # Global or pass as state
94
  refs = [f"Page {d.metadata.get('page', 'N/A')}" for d in docs]
95
- full_answer = f"{answer}\n\n**Refs:** {' | '.join(refs)}"
 
 
 
 
 
 
96
 
97
- # Append in messages format
98
- history.append({"role": "user", "content": message})
99
- history.append({"role": "assistant", "content": full_answer})
100
- return history
101
  #====================
102
  def extract_docx_text(file_path):
103
  doc = Document(file_path)
@@ -329,13 +333,12 @@ with gr.Blocks(css=css) as demo:
329
  </div>"""
330
  )
331
  with gr.TabItem("ChatBot-NRL manual-goods"):
332
- gr.Markdown("# RAG Chatbot with Page References")
333
- chatbot = gr.Chatbot(height=500) # No 'type' arg!
334
- msg = gr.Textbox(placeholder="What is GeM?", scale=4)
335
- submit = gr.Button("Send", scale=1)
336
- clear = gr.Button("Clear")
337
 
338
- submit.click(respond, [msg, chatbot], [chatbot, msg])
339
- msg.submit(respond, [msg, chatbot], [chatbot, msg])
340
- clear.click(lambda: [], None, chatbot, queue=False)
341
  demo.queue().launch()
 
90
  #============starting extract_docx_text
91
  def respond(message, history):
92
  answer = qa_chain.invoke(message)
93
+ docs = retriever.invoke(message)
94
  refs = [f"Page {d.metadata.get('page', 'N/A')}" for d in docs]
95
+ full_answer = f"{answer}\n\n**References:**\n" + "\n".join(refs)
96
+
97
+ # CRITICAL: Append ONLY pure dicts - no metadata, tuples, or extras
98
+ new_history = history + [ # Or history.append() then return history
99
+ {"role": "user", "content": message},
100
+ {"role": "assistant", "content": full_answer}
101
+ ]
102
 
103
+ # Clear input
104
+ return "", new_history # Return cleared msg, updated history
 
 
105
  #====================
106
  def extract_docx_text(file_path):
107
  doc = Document(file_path)
 
333
  </div>"""
334
  )
335
  with gr.TabItem("ChatBot-NRL manual-goods"):
336
+ gr.Markdown("# RAG Chatbot")
337
+ chatbot = gr.Chatbot(height=500) # Defaults to messages
338
+ msg = gr.Textbox(placeholder="Ask a question...", label="Query")
339
+ submit_btn = gr.Button("Submit")
 
340
 
341
+ # Events
342
+ submit_btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
343
+ msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
344
  demo.queue().launch()