Update app.py
Browse files
app.py
CHANGED
|
@@ -128,11 +128,10 @@ def render_latex(text):
|
|
| 128 |
return text
|
| 129 |
|
| 130 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 131 |
-
"""
|
| 132 |
client = InferenceClient(model="Qwen/Qwen2.5-Math-7B-Instruct")
|
| 133 |
|
| 134 |
messages = [{"role": "system", "content": system_message}]
|
| 135 |
-
# Iterate over history dicts and add user/assistant pairs
|
| 136 |
for msg in history:
|
| 137 |
if msg["role"] == "user":
|
| 138 |
messages.append({"role": "user", "content": msg["content"]})
|
|
@@ -140,17 +139,21 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 140 |
messages.append({"role": "assistant", "content": msg["content"]})
|
| 141 |
messages.append({"role": "user", "content": message})
|
| 142 |
|
|
|
|
| 143 |
try:
|
| 144 |
-
|
| 145 |
messages,
|
| 146 |
max_tokens=max_tokens,
|
| 147 |
temperature=temperature,
|
| 148 |
top_p=top_p,
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
except Exception as e:
|
| 153 |
-
|
| 154 |
|
| 155 |
def get_random_sample():
|
| 156 |
"""Get a random sample problem - loads datasets if needed"""
|
|
@@ -199,12 +202,13 @@ with gr.Blocks(title="🧮 Mathetics AI") as demo:
|
|
| 199 |
)
|
| 200 |
|
| 201 |
def chat_response(message, history):
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
| 208 |
|
| 209 |
def clear_chat():
|
| 210 |
"""Clear the chat history and textbox."""
|
|
|
|
| 128 |
return text
|
| 129 |
|
| 130 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 131 |
+
"""Streaming response with yield"""
|
| 132 |
client = InferenceClient(model="Qwen/Qwen2.5-Math-7B-Instruct")
|
| 133 |
|
| 134 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 135 |
for msg in history:
|
| 136 |
if msg["role"] == "user":
|
| 137 |
messages.append({"role": "user", "content": msg["content"]})
|
|
|
|
| 139 |
messages.append({"role": "assistant", "content": msg["content"]})
|
| 140 |
messages.append({"role": "user", "content": message})
|
| 141 |
|
| 142 |
+
response = ""
|
| 143 |
try:
|
| 144 |
+
for chunk in client.chat_completion(
|
| 145 |
messages,
|
| 146 |
max_tokens=max_tokens,
|
| 147 |
temperature=temperature,
|
| 148 |
top_p=top_p,
|
| 149 |
+
stream=True # Enable streaming
|
| 150 |
+
):
|
| 151 |
+
if chunk.choices[0].delta.content:
|
| 152 |
+
response += chunk.choices[0].delta.content
|
| 153 |
+
yield render_latex(response) # Yield progressively
|
| 154 |
+
|
| 155 |
except Exception as e:
|
| 156 |
+
yield f"❌ Error: {str(e)[:100]}..."
|
| 157 |
|
| 158 |
def get_random_sample():
|
| 159 |
"""Get a random sample problem - loads datasets if needed"""
|
|
|
|
| 202 |
)
|
| 203 |
|
| 204 |
def chat_response(message, history):
|
| 205 |
+
"""Streaming chat response"""
|
| 206 |
+
history.append({"role": "user", "content": message})
|
| 207 |
+
history.append({"role": "assistant", "content": ""})
|
| 208 |
+
|
| 209 |
+
for partial_response in respond(message, history[:-1], create_math_system_message(), 1024, 0.3, 0.85):
|
| 210 |
+
history[-1]["content"] = partial_response
|
| 211 |
+
yield history, ""
|
| 212 |
|
| 213 |
def clear_chat():
|
| 214 |
"""Clear the chat history and textbox."""
|