SyedZainAliShah commited on
Commit
9c493f4
·
verified ·
1 Parent(s): 382fd58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -164,7 +164,7 @@ def retrieve_relevant_chunks(query, top_k=3):
164
  return [], []
165
 
166
  def chat(message, history):
167
- """Chat function - processes message and returns response string"""
168
  global client
169
 
170
  # Reinitialize client if needed
@@ -201,10 +201,11 @@ def chat(message, history):
201
  {"role": "system", "content": "You are a helpful assistant that answers questions based on provided context. Be concise and accurate."}
202
  ]
203
 
204
- # Add history - history is already in messages format
205
  if history:
206
- for msg in history[-6:]: # Last 6 messages
207
- messages.append(msg)
 
208
 
209
  # Add current query
210
  messages.append({
@@ -236,7 +237,6 @@ def chat(message, history):
236
  'answer': answer
237
  })
238
 
239
- # Return just the string - ChatInterface handles adding to history
240
  return full_answer
241
 
242
  except Exception as e:
@@ -255,7 +255,7 @@ def download_history():
255
  except:
256
  return None
257
 
258
- # Build interface - USE gr.ChatInterface!
259
  with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
260
 
261
  gr.Markdown("""
@@ -280,10 +280,9 @@ with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
280
  download_file = gr.File(label="Download")
281
 
282
  with gr.Column(scale=2):
283
- # Use ChatInterface directly - it handles everything!
284
  chat_interface = gr.ChatInterface(
285
  fn=chat,
286
- type="messages",
287
  chatbot=gr.Chatbot(height=500, label="Conversation"),
288
  textbox=gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2),
289
  submit_btn="Ask",
 
164
  return [], []
165
 
166
  def chat(message, history):
167
+ """Chat function - returns response string for ChatInterface"""
168
  global client
169
 
170
  # Reinitialize client if needed
 
201
  {"role": "system", "content": "You are a helpful assistant that answers questions based on provided context. Be concise and accurate."}
202
  ]
203
 
204
+ # Add history - convert from tuples to message format
205
  if history:
206
+ for user_msg, assistant_msg in history[-3:]: # Last 3 exchanges
207
+ messages.append({"role": "user", "content": user_msg})
208
+ messages.append({"role": "assistant", "content": assistant_msg})
209
 
210
  # Add current query
211
  messages.append({
 
237
  'answer': answer
238
  })
239
 
 
240
  return full_answer
241
 
242
  except Exception as e:
 
255
  except:
256
  return None
257
 
258
+ # Build interface
259
  with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
260
 
261
  gr.Markdown("""
 
280
  download_file = gr.File(label="Download")
281
 
282
  with gr.Column(scale=2):
283
+ # ChatInterface - REMOVED type="messages" parameter
284
  chat_interface = gr.ChatInterface(
285
  fn=chat,
 
286
  chatbot=gr.Chatbot(height=500, label="Conversation"),
287
  textbox=gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2),
288
  submit_btn="Ask",