Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ body {
|
|
| 18 |
padding-bottom: 2rem;
|
| 19 |
}
|
| 20 |
#title-container {
|
| 21 |
-
background-color: rgba(255, 255, 255, 0.85);
|
| 22 |
border-radius: 16px;
|
| 23 |
padding: 1.5rem 2rem;
|
| 24 |
margin: 2rem 0;
|
|
@@ -26,7 +26,7 @@ body {
|
|
| 26 |
max-width: 500px;
|
| 27 |
text-align: left;
|
| 28 |
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 29 |
-
margin-left: 0rem;
|
| 30 |
}
|
| 31 |
#title-container h1 {
|
| 32 |
color: #222 !important;
|
|
@@ -65,29 +65,29 @@ body {
|
|
| 65 |
}
|
| 66 |
"""
|
| 67 |
|
| 68 |
-
#
|
|
|
|
| 69 |
client = InferenceClient(
|
| 70 |
-
base_url="https://router.huggingface.co/v1"
|
| 71 |
-
|
| 72 |
)
|
| 73 |
|
| 74 |
# Level prompt selector
|
| 75 |
def level_to_prompt(level):
|
| 76 |
return {
|
| 77 |
-
"A1": "You are a friendly French tutor. Focus on the user's specific question. Use simple French and explain in English.
|
| 78 |
-
"A2": "You are a patient French tutor. Respond to the user's question clearly
|
| 79 |
-
"B1": "You are a helpful French tutor. Use mostly French
|
| 80 |
-
"B2": "You are a French tutor. Respond primarily in French
|
| 81 |
-
"C1": "You are a native French tutor. Use fluent French and
|
| 82 |
-
"C2": "You are a French language professor. Use sophisticated French
|
| 83 |
}.get(level, "You are a helpful French tutor.")
|
| 84 |
|
| 85 |
|
| 86 |
-
#
|
| 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 |
# Handle history
|
| 92 |
if history and isinstance(history[0], tuple):
|
| 93 |
for user_msg, assistant_msg in history:
|
|
@@ -99,30 +99,34 @@ def respond(message, history, user_level, max_tokens, temperature, top_p):
|
|
| 99 |
messages.extend(history)
|
| 100 |
|
| 101 |
messages.append({"role": "user", "content": message})
|
| 102 |
-
|
| 103 |
response = ""
|
|
|
|
| 104 |
try:
|
| 105 |
-
|
| 106 |
model="HuggingFaceH4/zephyr-7b-beta",
|
| 107 |
messages=messages,
|
| 108 |
stream=True,
|
| 109 |
max_tokens=max_tokens,
|
| 110 |
temperature=temperature,
|
| 111 |
top_p=top_p,
|
| 112 |
-
)
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
delta = msg.get("choices", [{}])[0].get("delta", {})
|
| 115 |
token = delta.get("content", "")
|
| 116 |
-
|
| 117 |
if token:
|
| 118 |
response += token
|
| 119 |
-
|
| 120 |
yield response
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
yield f"Désolé! There was an error: {str(e)}"
|
| 124 |
|
| 125 |
-
|
|
|
|
| 126 |
with gr.Blocks(css=css) as demo:
|
| 127 |
gr.HTML("""
|
| 128 |
<div id="title-container">
|
|
@@ -130,8 +134,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 130 |
<p class="subtitle">French Tutor</p>
|
| 131 |
</div>
|
| 132 |
""")
|
| 133 |
-
|
| 134 |
with gr.Column(elem_id="chat-panel"):
|
|
|
|
| 135 |
with gr.Accordion("Advanced Settings", open=False):
|
| 136 |
user_level = gr.Dropdown(
|
| 137 |
choices=["A1", "A2", "B1", "B2", "C1", "C2"],
|
|
|
|
| 18 |
padding-bottom: 2rem;
|
| 19 |
}
|
| 20 |
#title-container {
|
| 21 |
+
background-color: rgba(255, 255, 255, 0.85);
|
| 22 |
border-radius: 16px;
|
| 23 |
padding: 1.5rem 2rem;
|
| 24 |
margin: 2rem 0;
|
|
|
|
| 26 |
max-width: 500px;
|
| 27 |
text-align: left;
|
| 28 |
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 29 |
+
margin-left: 0rem;
|
| 30 |
}
|
| 31 |
#title-container h1 {
|
| 32 |
color: #222 !important;
|
|
|
|
| 65 |
}
|
| 66 |
"""
|
| 67 |
|
| 68 |
+
# === PATCHED CLIENT ===
|
| 69 |
+
# Uses the new unified HF router for OpenAI-like chat completion
|
| 70 |
client = InferenceClient(
|
| 71 |
+
base_url="https://router.huggingface.co/v1"
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
# Level prompt selector
|
| 75 |
def level_to_prompt(level):
|
| 76 |
return {
|
| 77 |
+
"A1": "You are a friendly French tutor. Focus on the user's specific question. Use simple French and explain in English.",
|
| 78 |
+
"A2": "You are a patient French tutor. Respond to the user's question clearly and simply.",
|
| 79 |
+
"B1": "You are a helpful French tutor. Use mostly French with minimal English when needed.",
|
| 80 |
+
"B2": "You are a French tutor. Respond primarily in French with concise, relevant explanations.",
|
| 81 |
+
"C1": "You are a native French tutor. Use fluent French and include brief cultural or historical notes when relevant.",
|
| 82 |
+
"C2": "You are a French language professor. Use sophisticated French with precise explanations.",
|
| 83 |
}.get(level, "You are a helpful French tutor.")
|
| 84 |
|
| 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 |
# Handle history
|
| 92 |
if history and isinstance(history[0], tuple):
|
| 93 |
for user_msg, assistant_msg in history:
|
|
|
|
| 99 |
messages.extend(history)
|
| 100 |
|
| 101 |
messages.append({"role": "user", "content": message})
|
| 102 |
+
|
| 103 |
response = ""
|
| 104 |
+
|
| 105 |
try:
|
| 106 |
+
stream = client.chat.completions.create(
|
| 107 |
model="HuggingFaceH4/zephyr-7b-beta",
|
| 108 |
messages=messages,
|
| 109 |
stream=True,
|
| 110 |
max_tokens=max_tokens,
|
| 111 |
temperature=temperature,
|
| 112 |
top_p=top_p,
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
for msg in stream:
|
| 116 |
+
# NEW: Streaming dict-safe extraction
|
| 117 |
delta = msg.get("choices", [{}])[0].get("delta", {})
|
| 118 |
token = delta.get("content", "")
|
| 119 |
+
|
| 120 |
if token:
|
| 121 |
response += token
|
| 122 |
+
|
| 123 |
yield response
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
yield f"Désolé! There was an error: {str(e)}"
|
| 127 |
|
| 128 |
+
|
| 129 |
+
# === GRADIO INTERFACE ===
|
| 130 |
with gr.Blocks(css=css) as demo:
|
| 131 |
gr.HTML("""
|
| 132 |
<div id="title-container">
|
|
|
|
| 134 |
<p class="subtitle">French Tutor</p>
|
| 135 |
</div>
|
| 136 |
""")
|
| 137 |
+
|
| 138 |
with gr.Column(elem_id="chat-panel"):
|
| 139 |
+
|
| 140 |
with gr.Accordion("Advanced Settings", open=False):
|
| 141 |
user_level = gr.Dropdown(
|
| 142 |
choices=["A1", "A2", "B1", "B2", "C1", "C2"],
|