RahulGanapathy commited on
Commit
4a06593
·
verified ·
1 Parent(s): 5265021

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -12,22 +12,30 @@ SYSTEM_MESSAGE = (
12
 
13
  # Function to handle chatbot responses
14
  def respond(message, history):
15
- # Convert history to OpenAI-style format
16
- messages = [{"role": "system", "content": SYSTEM_MESSAGE}] + history
 
 
 
 
 
 
 
17
  messages.append({"role": "user", "content": message})
18
 
19
  # Get response from Hugging Face model
20
  response = client.chat_completion(messages, max_tokens=200, temperature=0.7)
21
  bot_reply = response.choices[0].message.content
22
 
23
- # Append response to history in OpenAI format
24
- history.append({"role": "assistant", "content": bot_reply})
25
- return history
 
26
 
27
  # Create Gradio chatbot UI
28
  demo = gr.ChatInterface(
29
  respond,
30
- chatbot=gr.Chatbot(type="messages"), # Correct format
31
  title="Misinformation Detection Chatbot",
32
  description="Ask anything, and the chatbot will verify whether it's true or false.",
33
  )
 
12
 
13
  # Function to handle chatbot responses
14
  def respond(message, history):
15
+ # Convert history into OpenAI-style messages
16
+ messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
17
+
18
+ # Convert Gradio history format (tuples) to OpenAI format
19
+ for user, bot in history:
20
+ messages.append({"role": "user", "content": user})
21
+ messages.append({"role": "assistant", "content": bot})
22
+
23
+ # Append latest user input
24
  messages.append({"role": "user", "content": message})
25
 
26
  # Get response from Hugging Face model
27
  response = client.chat_completion(messages, max_tokens=200, temperature=0.7)
28
  bot_reply = response.choices[0].message.content
29
 
30
+ # Append new message in (user, bot) tuple format (✅ Correct for Gradio)
31
+ history.append((message, bot_reply))
32
+
33
+ return history, bot_reply # ✅ Ensure return matches Gradio format
34
 
35
  # Create Gradio chatbot UI
36
  demo = gr.ChatInterface(
37
  respond,
38
+ chatbot=gr.Chatbot(), # Default format expects (user, bot) tuples
39
  title="Misinformation Detection Chatbot",
40
  description="Ask anything, and the chatbot will verify whether it's true or false.",
41
  )