Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,34 +19,28 @@ llm = Llama(
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
-
# SAFE CHAT FUNCTION
|
| 23 |
# =========================
|
| 24 |
-
def chat(
|
| 25 |
history = history or []
|
| 26 |
-
messages = []
|
| 27 |
-
|
| 28 |
-
# Convert Gradio history safely (tuple-based)
|
| 29 |
-
for item in history:
|
| 30 |
-
if isinstance(item, tuple) and len(item) == 2:
|
| 31 |
-
user_msg, bot_msg = item
|
| 32 |
|
| 33 |
-
|
| 34 |
-
"role": "user",
|
| 35 |
-
"content": str(user_msg)
|
| 36 |
-
})
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
messages.append({
|
| 39 |
-
"role": "
|
| 40 |
-
"content": str(
|
| 41 |
})
|
| 42 |
|
| 43 |
-
#
|
| 44 |
messages.append({
|
| 45 |
"role": "user",
|
| 46 |
-
"content": str(
|
| 47 |
})
|
| 48 |
|
| 49 |
-
#
|
| 50 |
output = llm.create_chat_completion(
|
| 51 |
messages=messages,
|
| 52 |
max_tokens=300,
|
|
@@ -55,17 +49,21 @@ def chat(prompt, history):
|
|
| 55 |
|
| 56 |
response = output["choices"][0]["message"]["content"]
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
return "",
|
| 61 |
|
| 62 |
# =========================
|
| 63 |
-
# UI (GRADIO SAFE MODE)
|
| 64 |
# =========================
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
-
gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend
|
| 67 |
|
| 68 |
-
chatbot = gr.Chatbot()
|
| 69 |
msg = gr.Textbox(label="Ask your AI")
|
| 70 |
clear = gr.Button("Clear")
|
| 71 |
|
|
@@ -74,7 +72,7 @@ with gr.Blocks() as demo:
|
|
| 74 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 75 |
|
| 76 |
# =========================
|
| 77 |
-
# LAUNCH
|
| 78 |
# =========================
|
| 79 |
demo.queue()
|
| 80 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# =========================
|
| 22 |
+
# SAFE CHAT FUNCTION (NO MIXING)
|
| 23 |
# =========================
|
| 24 |
+
def chat(message, history):
|
| 25 |
history = history or []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
messages = []
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# STRICT: only dict format allowed
|
| 30 |
+
for msg in history:
|
| 31 |
+
if isinstance(msg, dict):
|
| 32 |
messages.append({
|
| 33 |
+
"role": msg.get("role", "user"),
|
| 34 |
+
"content": str(msg.get("content", ""))
|
| 35 |
})
|
| 36 |
|
| 37 |
+
# add new user message
|
| 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 |
|
| 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 MODE)
|
| 62 |
# =========================
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Stable Build)")
|
| 65 |
|
| 66 |
+
chatbot = gr.Chatbot(type="messages")
|
| 67 |
msg = gr.Textbox(label="Ask your AI")
|
| 68 |
clear = gr.Button("Clear")
|
| 69 |
|
|
|
|
| 72 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 73 |
|
| 74 |
# =========================
|
| 75 |
+
# LAUNCH
|
| 76 |
# =========================
|
| 77 |
demo.queue()
|
| 78 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|