Mikecode123 commited on
Commit
c8333b1
·
verified ·
1 Parent(s): d0a3341

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -19,18 +19,28 @@ llm = Llama(
19
  )
20
 
21
  # =========================
22
- # CHAT FUNCTION (GRADIO 3 SAFE)
23
  # =========================
24
- def chat(prompt, history):
25
  history = history or []
 
26
  messages = []
27
 
28
- for user_msg, bot_msg in history:
29
- messages.append({"role": "user", "content": str(user_msg)})
30
- messages.append({"role": "assistant", "content": str(bot_msg)})
 
 
 
 
31
 
32
- messages.append({"role": "user", "content": str(prompt)})
 
 
 
 
33
 
 
34
  output = llm.create_chat_completion(
35
  messages=messages,
36
  max_tokens=300,
@@ -39,25 +49,27 @@ def chat(prompt, history):
39
 
40
  response = output["choices"][0]["message"]["content"]
41
 
42
- history.append((prompt, response))
 
 
 
 
43
 
44
- return "", history
45
 
46
  # =========================
47
- # UI (NO TYPE PARAMETER)
48
  # =========================
49
  with gr.Blocks() as demo:
50
  gr.Markdown("# 🧠 Living Legend AI Chatbot")
51
 
52
- chatbot = gr.Chatbot() # 🔥 FIXED (NO type="messages")
53
  msg = gr.Textbox()
54
  clear = gr.Button("Clear")
55
 
56
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
 
57
  clear.click(lambda: [], None, chatbot)
58
 
59
- # =========================
60
- # LAUNCH
61
- # =========================
62
  demo.queue()
63
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
19
  )
20
 
21
  # =========================
22
+ # CHAT FUNCTION (STRICT MESSAGES FORMAT)
23
  # =========================
24
+ def chat(message, history):
25
  history = history or []
26
+
27
  messages = []
28
 
29
+ # SAFE: ensure correct format
30
+ for msg in history:
31
+ if isinstance(msg, dict):
32
+ messages.append({
33
+ "role": msg.get("role", ""),
34
+ "content": str(msg.get("content", ""))
35
+ })
36
 
37
+ # add user message
38
+ messages.append({
39
+ "role": "user",
40
+ "content": str(message)
41
+ })
42
 
43
+ # inference
44
  output = llm.create_chat_completion(
45
  messages=messages,
46
  max_tokens=300,
 
49
 
50
  response = output["choices"][0]["message"]["content"]
51
 
52
+ # append assistant reply
53
+ messages.append({
54
+ "role": "assistant",
55
+ "content": response
56
+ })
57
 
58
+ return "", messages
59
 
60
  # =========================
61
+ # UI (GRADIO 4 SAFE STYLE BUT BACKWARD COMPATIBLE)
62
  # =========================
63
  with gr.Blocks() as demo:
64
  gr.Markdown("# 🧠 Living Legend AI Chatbot")
65
 
66
+ chatbot = gr.Chatbot(type="messages") # IMPORTANT FIX
67
  msg = gr.Textbox()
68
  clear = gr.Button("Clear")
69
 
70
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
71
+
72
  clear.click(lambda: [], None, chatbot)
73
 
 
 
 
74
  demo.queue()
75
  demo.launch(server_name="0.0.0.0", server_port=7860)