shanzaejaz commited on
Commit
b89596a
·
verified ·
1 Parent(s): 972671b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -47,15 +47,25 @@ def download_pdf(text):
47
 
48
  # ---------- AI CHATBOT ----------
49
  def chat(message, history):
 
 
 
 
 
 
 
 
 
50
  try:
51
  res = client.chat.completions.create(
52
  model="llama-3.3-70b-versatile",
53
- messages=[{"role": "user", "content": message}]
54
  )
55
  reply = res.choices[0].message.content
56
  except Exception as e:
57
  reply = str(e)
58
 
 
59
  history.append((message, reply))
60
  return history, history
61
 
@@ -86,5 +96,5 @@ with gr.Blocks() as app:
86
 
87
  msg.submit(chat, [msg, state], [chatbot, state])
88
 
89
- # Launch app
90
  app.launch()
 
47
 
48
  # ---------- AI CHATBOT ----------
49
  def chat(message, history):
50
+ # Convert Gradio chat history to API messages format
51
+ messages = []
52
+ for user_msg, bot_msg in history:
53
+ messages.append({"role": "user", "content": user_msg})
54
+ messages.append({"role": "assistant", "content": bot_msg})
55
+
56
+ # Add current user message
57
+ messages.append({"role": "user", "content": message})
58
+
59
  try:
60
  res = client.chat.completions.create(
61
  model="llama-3.3-70b-versatile",
62
+ messages=messages
63
  )
64
  reply = res.choices[0].message.content
65
  except Exception as e:
66
  reply = str(e)
67
 
68
+ # Append current turn to history for Gradio display
69
  history.append((message, reply))
70
  return history, history
71
 
 
96
 
97
  msg.submit(chat, [msg, state], [chatbot, state])
98
 
99
+ # Launch the app
100
  app.launch()