Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
# Background CSS
|
| 5 |
css = """
|
|
@@ -85,22 +86,71 @@ def level_to_prompt(level):
|
|
| 85 |
|
| 86 |
# === PATCHED CHAT HANDLER ===
|
| 87 |
def respond(message, history, user_level, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
system_message = level_to_prompt(user_level)
|
| 89 |
messages = [{"role": "system", "content": system_message}]
|
| 90 |
|
| 91 |
-
# FIX
|
| 92 |
-
|
| 93 |
-
message = message.get("content") or message.get("prompt") or str(message)
|
| 94 |
-
|
| 95 |
-
# FIX 2 — Safely reconstruct history
|
| 96 |
if history:
|
| 97 |
for h in history:
|
|
|
|
| 98 |
if isinstance(h, dict) and "role" in h and "content" in h:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
messages.append({"role": "user", "content": message})
|
| 103 |
|
|
|
|
| 104 |
response = ""
|
| 105 |
|
| 106 |
try:
|
|
@@ -114,13 +164,15 @@ def respond(message, history, user_level, max_tokens, temperature, top_p):
|
|
| 114 |
)
|
| 115 |
|
| 116 |
for msg in stream:
|
|
|
|
| 117 |
delta = msg.get("choices", [{}])[0].get("delta", {})
|
| 118 |
token = delta.get("content", "")
|
| 119 |
|
| 120 |
if token:
|
| 121 |
response += token
|
| 122 |
|
| 123 |
-
yield
|
|
|
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
yield f"Désolé! There was an error: {str(e)}"
|
|
@@ -128,6 +180,7 @@ def respond(message, history, user_level, max_tokens, temperature, top_p):
|
|
| 128 |
|
| 129 |
|
| 130 |
|
|
|
|
| 131 |
# === GRADIO INTERFACE ===
|
| 132 |
with gr.Blocks(css=css) as demo:
|
| 133 |
gr.HTML("""
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
# Background CSS
|
| 6 |
css = """
|
|
|
|
| 86 |
|
| 87 |
# === PATCHED CHAT HANDLER ===
|
| 88 |
def respond(message, history, user_level, max_tokens, temperature, top_p):
|
| 89 |
+
# --- DEBUG: normalize incoming message ---
|
| 90 |
+
# Gradio sometimes passes dicts like {"text": "..."} or {"content": "..."}
|
| 91 |
+
if isinstance(message, dict):
|
| 92 |
+
message = (
|
| 93 |
+
message.get("content") or
|
| 94 |
+
message.get("text") or
|
| 95 |
+
message.get("prompt") or
|
| 96 |
+
str(message)
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Force message to string
|
| 100 |
+
message = str(message)
|
| 101 |
+
|
| 102 |
+
# --- Build messages list for HF API ---
|
| 103 |
system_message = level_to_prompt(user_level)
|
| 104 |
messages = [{"role": "system", "content": system_message}]
|
| 105 |
|
| 106 |
+
# --- FIX: Safe history parsing ---
|
| 107 |
+
# Gradio passes many different formats; we must filter aggressively.
|
|
|
|
|
|
|
|
|
|
| 108 |
if history:
|
| 109 |
for h in history:
|
| 110 |
+
# History can be tuples, lists, dicts… normalize all of them.
|
| 111 |
if isinstance(h, dict) and "role" in h and "content" in h:
|
| 112 |
+
# Ensure content is a string
|
| 113 |
+
content = h["content"]
|
| 114 |
+
if isinstance(content, dict):
|
| 115 |
+
content = (
|
| 116 |
+
content.get("text") or
|
| 117 |
+
content.get("content") or
|
| 118 |
+
str(content)
|
| 119 |
+
)
|
| 120 |
+
messages.append({"role": h["role"], "content": str(content)})
|
| 121 |
+
|
| 122 |
+
# If Gradio gives tuples: (user_msg, assistant_msg)
|
| 123 |
+
elif isinstance(h, (list, tuple)) and len(h) == 2:
|
| 124 |
+
user_msg, assistant_msg = h
|
| 125 |
+
|
| 126 |
+
# Normalize user_msg
|
| 127 |
+
if user_msg:
|
| 128 |
+
if isinstance(user_msg, dict):
|
| 129 |
+
user_msg = (
|
| 130 |
+
user_msg.get("content") or
|
| 131 |
+
user_msg.get("text") or
|
| 132 |
+
str(user_msg)
|
| 133 |
+
)
|
| 134 |
+
messages.append({"role": "user", "content": str(user_msg)})
|
| 135 |
+
|
| 136 |
+
# Normalize assistant_msg
|
| 137 |
+
if assistant_msg:
|
| 138 |
+
if isinstance(assistant_msg, dict):
|
| 139 |
+
assistant_msg = (
|
| 140 |
+
assistant_msg.get("content") or
|
| 141 |
+
assistant_msg.get("text") or
|
| 142 |
+
str(assistant_msg)
|
| 143 |
+
)
|
| 144 |
+
messages.append({"role": "assistant", "content": str(assistant_msg)})
|
| 145 |
+
|
| 146 |
+
# Otherwise ignore weird entries entirely (prevents `.strip()` crash)
|
| 147 |
+
else:
|
| 148 |
+
continue
|
| 149 |
+
|
| 150 |
+
# Add user message
|
| 151 |
messages.append({"role": "user", "content": message})
|
| 152 |
|
| 153 |
+
# --- STREAMING RESPONSE ---
|
| 154 |
response = ""
|
| 155 |
|
| 156 |
try:
|
|
|
|
| 164 |
)
|
| 165 |
|
| 166 |
for msg in stream:
|
| 167 |
+
# msg is always a dict in HF OpenAI API format
|
| 168 |
delta = msg.get("choices", [{}])[0].get("delta", {})
|
| 169 |
token = delta.get("content", "")
|
| 170 |
|
| 171 |
if token:
|
| 172 |
response += token
|
| 173 |
|
| 174 |
+
# Ensure yield is always a string
|
| 175 |
+
yield str(response)
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
yield f"Désolé! There was an error: {str(e)}"
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
+
|
| 184 |
# === GRADIO INTERFACE ===
|
| 185 |
with gr.Blocks(css=css) as demo:
|
| 186 |
gr.HTML("""
|