Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,158 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import json
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
background-image: url('https://cdn-uploads.huggingface.co/production/uploads/67351c643fe51cb1aa28f2e5/YcsJnPk8HJvXiB5WkVmf1.jpeg');
|
| 9 |
-
background-size: cover;
|
| 10 |
-
background-position: center;
|
| 11 |
-
background-repeat: no-repeat;
|
| 12 |
-
}
|
| 13 |
-
.gradio-container {
|
| 14 |
-
display: flex;
|
| 15 |
-
flex-direction: column;
|
| 16 |
-
justify-content: center;
|
| 17 |
-
min-height: 100vh;
|
| 18 |
-
padding-top: 2rem;
|
| 19 |
-
padding-bottom: 2rem;
|
| 20 |
-
}
|
| 21 |
-
#title-container {
|
| 22 |
-
background-color: rgba(255, 255, 255, 0.85);
|
| 23 |
-
border-radius: 16px;
|
| 24 |
-
padding: 1.5rem 2rem;
|
| 25 |
-
margin: 2rem 0;
|
| 26 |
-
width: fit-content;
|
| 27 |
-
max-width: 500px;
|
| 28 |
-
text-align: left;
|
| 29 |
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 30 |
-
margin-left: 0rem;
|
| 31 |
-
}
|
| 32 |
-
#title-container h1 {
|
| 33 |
-
color: #222 !important;
|
| 34 |
-
font-size: 4rem;
|
| 35 |
-
font-family: 'Noto Sans JP', sans-serif;
|
| 36 |
-
margin: 0;
|
| 37 |
-
}
|
| 38 |
-
#title-container .subtitle {
|
| 39 |
-
font-size: 1.1rem;
|
| 40 |
-
font-family: 'Noto Sans', sans-serif;
|
| 41 |
-
color: #222 !important;
|
| 42 |
-
margin-top: 0.5rem;
|
| 43 |
-
margin-bottom: 0;
|
| 44 |
-
width: 100%;
|
| 45 |
-
display: block;
|
| 46 |
-
}
|
| 47 |
-
#chat-panel {
|
| 48 |
-
background-color: rgba(255, 255, 255, 0.85);
|
| 49 |
-
padding: 2rem;
|
| 50 |
-
border-radius: 12px;
|
| 51 |
-
justify-content: center;
|
| 52 |
-
width: 100%;
|
| 53 |
-
max-width: 700px;
|
| 54 |
-
height: 70vh;
|
| 55 |
-
box-shadow: 0 0 12px rgba(0, 0, 0, 0.3);
|
| 56 |
-
overflow-y: auto;
|
| 57 |
-
}
|
| 58 |
-
.gradio-container .chatbot h1 {
|
| 59 |
-
color: var(--custom-title-color) !important;
|
| 60 |
-
font-family: 'Noto Sans', serif !important;
|
| 61 |
-
font-size: 5rem !important;
|
| 62 |
-
font-weight: bold !important;
|
| 63 |
-
text-align: center !important;
|
| 64 |
-
margin-bottom: 1.5rem !important;
|
| 65 |
-
width: 100%;
|
| 66 |
-
}
|
| 67 |
-
"""
|
| 68 |
-
|
| 69 |
-
# === PATCHED CLIENT ===
|
| 70 |
-
# Uses the new unified HF router for OpenAI-like chat completion
|
| 71 |
client = InferenceClient(
|
| 72 |
-
base_url="https://router.huggingface.co/v1"
|
| 73 |
)
|
| 74 |
|
| 75 |
-
#
|
|
|
|
|
|
|
| 76 |
def level_to_prompt(level):
|
| 77 |
-
|
| 78 |
-
"A1": "You are a friendly French tutor.
|
| 79 |
-
"A2": "You are a patient French tutor.
|
| 80 |
-
"B1": "You are a helpful French tutor. Use mostly French with
|
| 81 |
-
"B2": "You are a French tutor. Respond primarily in French with
|
| 82 |
-
"C1": "You are a native French tutor.
|
| 83 |
-
"C2": "You are a French language professor. Use sophisticated French with precise explanations.",
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
def respond(message, history, user_level, max_tokens, temperature, top_p):
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
message.get("text") or
|
| 95 |
-
message.get("prompt") or
|
| 96 |
-
str(message)
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
#
|
|
|
|
|
|
|
|
|
|
| 100 |
message = str(message)
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
# Gradio passes many different formats; we must filter aggressively.
|
| 108 |
if history:
|
| 109 |
-
for
|
| 110 |
-
#
|
| 111 |
-
if isinstance(
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 147 |
-
else:
|
| 148 |
-
continue
|
| 149 |
-
|
| 150 |
-
# Add user message
|
| 151 |
messages.append({"role": "user", "content": message})
|
| 152 |
|
| 153 |
-
|
| 154 |
-
response = ""
|
| 155 |
|
|
|
|
|
|
|
|
|
|
| 156 |
try:
|
| 157 |
stream = client.chat.completions.create(
|
| 158 |
model="HuggingFaceH4/zephyr-7b-beta",
|
|
@@ -163,50 +81,48 @@ def respond(message, history, user_level, max_tokens, temperature, top_p):
|
|
| 163 |
top_p=top_p,
|
| 164 |
)
|
| 165 |
|
| 166 |
-
for
|
| 167 |
-
#
|
| 168 |
-
delta =
|
| 169 |
token = delta.get("content", "")
|
| 170 |
|
| 171 |
if token:
|
| 172 |
-
|
| 173 |
|
| 174 |
-
#
|
| 175 |
-
yield
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
-
yield f"Désolé!
|
| 179 |
-
|
| 180 |
-
|
| 181 |
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
# === GRADIO INTERFACE ===
|
| 185 |
with gr.Blocks(css=css) as demo:
|
| 186 |
-
gr.
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
with gr.Column(elem_id="chat-panel"):
|
| 194 |
-
|
| 195 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 196 |
-
user_level = gr.Dropdown(
|
| 197 |
-
choices=["A1", "A2", "B1", "B2", "C1", "C2"],
|
| 198 |
-
value="A1",
|
| 199 |
-
label="Your French Level (CEFR)"
|
| 200 |
-
)
|
| 201 |
-
max_tokens = gr.Slider(1, 2048, value=400, step=1, label="Response Length")
|
| 202 |
-
temperature = gr.Slider(0.1, 4.0, value=0.5, step=0.1, label="Creativity")
|
| 203 |
-
top_p = gr.Slider(0.1, 1.0, value=0.85, step=0.05, label="Dynamic Text Sampling")
|
| 204 |
-
|
| 205 |
-
gr.ChatInterface(
|
| 206 |
-
respond,
|
| 207 |
-
additional_inputs=[user_level, max_tokens, temperature, top_p],
|
| 208 |
-
type="messages"
|
| 209 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
if __name__ == "__main__":
|
| 212 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
+
# --------------------------
|
| 5 |
+
# NEW: HF Router Client
|
| 6 |
+
# --------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
client = InferenceClient(
|
| 8 |
+
base_url="https://router.huggingface.co/v1" # OpenAI-compatible API
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# --------------------------
|
| 12 |
+
# Tutor Levels
|
| 13 |
+
# --------------------------
|
| 14 |
def level_to_prompt(level):
|
| 15 |
+
prompts = {
|
| 16 |
+
"A1": "You are a friendly French tutor. Use simple French, short sentences, and explain difficult words in English.",
|
| 17 |
+
"A2": "You are a patient French tutor. Use simple French. Give short explanations in English when needed.",
|
| 18 |
+
"B1": "You are a helpful French tutor. Use mostly French, with short English explanations only when truly necessary.",
|
| 19 |
+
"B2": "You are a French tutor. Respond primarily in French with clear, concise explanations.",
|
| 20 |
+
"C1": "You are a native French tutor. Respond in fluent French with occasional linguistic or cultural notes.",
|
| 21 |
+
"C2": "You are a French language professor. Use sophisticated French with precise grammar explanations.",
|
| 22 |
+
}
|
| 23 |
+
return prompts.get(level, prompts["A1"])
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# --------------------------
|
| 27 |
+
# CLEAN, SAFE CHAT HANDLER
|
| 28 |
+
# --------------------------
|
| 29 |
def respond(message, history, user_level, max_tokens, temperature, top_p):
|
| 30 |
+
"""
|
| 31 |
+
Gradio's ChatInterface will trigger 'dict.strip' errors if anything
|
| 32 |
+
except a string is returned. This handler forces all history and message
|
| 33 |
+
fields to clean strings and always yields strings.
|
| 34 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Ensure user message is a string
|
| 37 |
+
if isinstance(message, dict):
|
| 38 |
+
# Gradio sometimes passes dicts
|
| 39 |
+
message = message.get("content") or message.get("text") or message.get("prompt") or ""
|
| 40 |
message = str(message)
|
| 41 |
|
| 42 |
+
# Build the conversation for HuggingFace router
|
| 43 |
+
messages = [
|
| 44 |
+
{"role": "system", "content": level_to_prompt(user_level)}
|
| 45 |
+
]
|
| 46 |
|
| 47 |
+
# Normalize history
|
|
|
|
| 48 |
if history:
|
| 49 |
+
for turn in history:
|
| 50 |
+
# Gradio gives pairs: (user_msg, assistant_msg)
|
| 51 |
+
if isinstance(turn, (list, tuple)) and len(turn) == 2:
|
| 52 |
+
user_msg, assistant_msg = turn
|
| 53 |
+
|
| 54 |
+
# normalize user message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if user_msg:
|
| 56 |
if isinstance(user_msg, dict):
|
| 57 |
+
user_msg = user_msg.get("content") or user_msg.get("text") or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
messages.append({"role": "user", "content": str(user_msg)})
|
| 59 |
|
| 60 |
+
# normalize assistant message
|
| 61 |
if assistant_msg:
|
| 62 |
if isinstance(assistant_msg, dict):
|
| 63 |
+
assistant_msg = assistant_msg.get("content") or assistant_msg.get("text") or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
messages.append({"role": "assistant", "content": str(assistant_msg)})
|
| 65 |
|
| 66 |
+
# Add new message
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
messages.append({"role": "user", "content": message})
|
| 68 |
|
| 69 |
+
reply = ""
|
|
|
|
| 70 |
|
| 71 |
+
# --------------------------
|
| 72 |
+
# STREAMING FROM HF ROUTER
|
| 73 |
+
# --------------------------
|
| 74 |
try:
|
| 75 |
stream = client.chat.completions.create(
|
| 76 |
model="HuggingFaceH4/zephyr-7b-beta",
|
|
|
|
| 81 |
top_p=top_p,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
for chunk in stream:
|
| 85 |
+
# chunk is dict-like: {"choices":[{"delta":{"content":"..."}}]}
|
| 86 |
+
delta = chunk.get("choices", [{}])[0].get("delta", {})
|
| 87 |
token = delta.get("content", "")
|
| 88 |
|
| 89 |
if token:
|
| 90 |
+
reply += token
|
| 91 |
|
| 92 |
+
# MUST yield only strings
|
| 93 |
+
yield reply
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
+
yield f"Désolé ! Une erreur s'est produite : {e}"
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
+
# --------------------------
|
| 100 |
+
# GRADIO APP
|
| 101 |
+
# --------------------------
|
| 102 |
+
css = """
|
| 103 |
+
body {
|
| 104 |
+
background: #f5f5f5;
|
| 105 |
+
}
|
| 106 |
+
"""
|
| 107 |
|
|
|
|
| 108 |
with gr.Blocks(css=css) as demo:
|
| 109 |
+
gr.Markdown("<h1 style='text-align:center;'>LE PROFESSEUR — French Tutor</h1>")
|
| 110 |
+
|
| 111 |
+
with gr.Accordion("Settings", open=False):
|
| 112 |
+
user_level = gr.Dropdown(
|
| 113 |
+
["A1", "A2", "B1", "B2", "C1", "C2"],
|
| 114 |
+
value="A1",
|
| 115 |
+
label="Your French Level"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
)
|
| 117 |
+
max_tokens = gr.Slider(10, 2048, value=200, label="Max Tokens")
|
| 118 |
+
temperature = gr.Slider(0.1, 2.0, value=0.7, label="Creativity")
|
| 119 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, label="Top-p Sampling")
|
| 120 |
+
|
| 121 |
+
gr.ChatInterface(
|
| 122 |
+
fn=respond,
|
| 123 |
+
additional_inputs=[user_level, max_tokens, temperature, top_p],
|
| 124 |
+
type="messages"
|
| 125 |
+
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch()
|