dnzblgn commited on
Commit
acb03a2
·
verified ·
1 Parent(s): 3f6d062

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -147,28 +147,31 @@ def user_query_with_rag(query, qa_chain, chatbot):
147
  if not query.strip():
148
  yield history, ""
149
  return
 
150
  if qa_chain is None:
151
- history.append(("user", query))
152
- history.append(("assistant", "Please upload and process a file first."))
153
  yield history, ""
154
  return
155
 
156
- history.append(("user", query))
157
- history.append(("assistant", ""))
158
  yield history, ""
159
 
160
  try:
161
  response = qa_chain.invoke({"question": query, "chat_history": []})
162
  assistant_response = response.get("answer", "No answer generated.")
 
 
163
  for i in range(len(assistant_response)):
164
- history[-1] = ("assistant", assistant_response[:i + 1])
165
  yield history, ""
166
  time.sleep(0.01)
167
  except Exception as e:
168
  logger.error(f"RAG error: {e}")
169
- history[-1] = ("assistant", "An error occurred while answering your question.")
170
  yield history, ""
171
 
 
172
  def demo():
173
  with gr.Blocks(title="RAG Analyzer") as app:
174
  db_state = gr.State(None)
 
147
  if not query.strip():
148
  yield history, ""
149
  return
150
+
151
  if qa_chain is None:
152
+ history.append((query, "Please upload and process a file first."))
 
153
  yield history, ""
154
  return
155
 
156
+ # Add user's question and placeholder for assistant's answer
157
+ history.append((query, ""))
158
  yield history, ""
159
 
160
  try:
161
  response = qa_chain.invoke({"question": query, "chat_history": []})
162
  assistant_response = response.get("answer", "No answer generated.")
163
+
164
+ # Typing effect
165
  for i in range(len(assistant_response)):
166
+ history[-1] = (query, assistant_response[:i + 1])
167
  yield history, ""
168
  time.sleep(0.01)
169
  except Exception as e:
170
  logger.error(f"RAG error: {e}")
171
+ history[-1] = (query, "An error occurred while answering your question.")
172
  yield history, ""
173
 
174
+
175
  def demo():
176
  with gr.Blocks(title="RAG Analyzer") as app:
177
  db_state = gr.State(None)