Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,28 +19,18 @@ llm = Llama(
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
-
#
|
| 23 |
# =========================
|
| 24 |
-
def chat(
|
| 25 |
history = history or []
|
| 26 |
-
|
| 27 |
messages = []
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
messages.append({
|
| 33 |
-
"role": msg.get("role", "user"),
|
| 34 |
-
"content": str(msg.get("content", ""))
|
| 35 |
-
})
|
| 36 |
|
| 37 |
-
|
| 38 |
-
messages.append({
|
| 39 |
-
"role": "user",
|
| 40 |
-
"content": str(message)
|
| 41 |
-
})
|
| 42 |
|
| 43 |
-
# run model
|
| 44 |
output = llm.create_chat_completion(
|
| 45 |
messages=messages,
|
| 46 |
max_tokens=300,
|
|
@@ -49,27 +39,22 @@ def chat(message, history):
|
|
| 49 |
|
| 50 |
response = output["choices"][0]["message"]["content"]
|
| 51 |
|
| 52 |
-
|
| 53 |
-
messages.append({
|
| 54 |
-
"role": "assistant",
|
| 55 |
-
"content": response
|
| 56 |
-
})
|
| 57 |
|
| 58 |
-
return "",
|
| 59 |
|
| 60 |
# =========================
|
| 61 |
-
# UI (
|
| 62 |
# =========================
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
gr.Markdown("# 🧠
|
| 65 |
|
| 66 |
-
chatbot = gr.Chatbot(type="messages")
|
| 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, queue=False)
|
| 73 |
|
| 74 |
# =========================
|
| 75 |
# LAUNCH
|
|
|
|
| 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 |
|
| 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
|