Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,18 +19,28 @@ llm = Llama(
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
-
# CHAT FUNCTION (
|
| 23 |
# =========================
|
| 24 |
-
def chat(
|
| 25 |
history = history or []
|
|
|
|
| 26 |
messages = []
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
return "",
|
| 45 |
|
| 46 |
# =========================
|
| 47 |
-
# UI (
|
| 48 |
# =========================
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
gr.Markdown("# 🧠 Living Legend AI Chatbot")
|
| 51 |
|
| 52 |
-
chatbot = gr.Chatbot(
|
| 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)
|