Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,9 +20,9 @@ def solve(question, history):
|
|
| 20 |
messages = [
|
| 21 |
{"role": "system", "content": "You are a helpful math tutor. Solve problems step by step, showing all working clearly."},
|
| 22 |
]
|
| 23 |
-
for
|
| 24 |
-
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": question})
|
| 27 |
|
| 28 |
text = tokenizer.apply_chat_template(
|
|
@@ -48,7 +48,10 @@ def respond(question, history):
|
|
| 48 |
if not question.strip():
|
| 49 |
return "", history
|
| 50 |
answer = solve(question, history)
|
| 51 |
-
history = history + [
|
|
|
|
|
|
|
|
|
|
| 52 |
return "", history
|
| 53 |
|
| 54 |
# ββ UI βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -59,6 +62,9 @@ with gr.Blocks(title="Math Tutor AI") as demo:
|
|
| 59 |
---
|
| 60 |
""")
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
chatbot = gr.Chatbot(height=450)
|
| 63 |
|
| 64 |
with gr.Row():
|
|
@@ -86,8 +92,37 @@ with gr.Blocks(title="Math Tutor AI") as demo:
|
|
| 86 |
|
| 87 |
gr.Markdown("*Model: Qwen2.5-Math-1.5B fine-tuned on 10K math problems*")
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 20 |
messages = [
|
| 21 |
{"role": "system", "content": "You are a helpful math tutor. Solve problems step by step, showing all working clearly."},
|
| 22 |
]
|
| 23 |
+
for msg in history:
|
| 24 |
+
if msg["role"] in ("user", "assistant"):
|
| 25 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 26 |
messages.append({"role": "user", "content": question})
|
| 27 |
|
| 28 |
text = tokenizer.apply_chat_template(
|
|
|
|
| 48 |
if not question.strip():
|
| 49 |
return "", history
|
| 50 |
answer = solve(question, history)
|
| 51 |
+
history = history + [
|
| 52 |
+
{"role": "user", "content": question},
|
| 53 |
+
{"role": "assistant", "content": answer},
|
| 54 |
+
]
|
| 55 |
return "", history
|
| 56 |
|
| 57 |
# ββ UI βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 62 |
---
|
| 63 |
""")
|
| 64 |
|
| 65 |
+
# Store history as state
|
| 66 |
+
history_state = gr.State([])
|
| 67 |
+
|
| 68 |
chatbot = gr.Chatbot(height=450)
|
| 69 |
|
| 70 |
with gr.Row():
|
|
|
|
| 92 |
|
| 93 |
gr.Markdown("*Model: Qwen2.5-Math-1.5B fine-tuned on 10K math problems*")
|
| 94 |
|
| 95 |
+
# ββ Event Handlers βββββββββββββββββββββββββββββββββββββ
|
| 96 |
+
def respond_and_display(question, history):
|
| 97 |
+
if not question.strip():
|
| 98 |
+
return "", history, [[m["content"] for m in history[i:i+2]]
|
| 99 |
+
for i in range(0, len(history), 2)]
|
| 100 |
+
answer = solve(question, history)
|
| 101 |
+
history = history + [
|
| 102 |
+
{"role": "user", "content": question},
|
| 103 |
+
{"role": "assistant", "content": answer},
|
| 104 |
+
]
|
| 105 |
+
# Convert to tuple pairs for display
|
| 106 |
+
display = []
|
| 107 |
+
for i in range(0, len(history), 2):
|
| 108 |
+
u = history[i]["content"] if i < len(history) else ""
|
| 109 |
+
a = history[i+1]["content"] if i+1 < len(history) else ""
|
| 110 |
+
display.append((u, a))
|
| 111 |
+
return "", history, display
|
| 112 |
+
|
| 113 |
+
def clear_all():
|
| 114 |
+
return "", [], []
|
| 115 |
+
|
| 116 |
+
submit.click(
|
| 117 |
+
respond_and_display,
|
| 118 |
+
[question, history_state],
|
| 119 |
+
[question, history_state, chatbot]
|
| 120 |
+
)
|
| 121 |
+
question.submit(
|
| 122 |
+
respond_and_display,
|
| 123 |
+
[question, history_state],
|
| 124 |
+
[question, history_state, chatbot]
|
| 125 |
+
)
|
| 126 |
+
clear.click(clear_all, outputs=[question, history_state, chatbot])
|
| 127 |
|
| 128 |
demo.launch(theme=gr.themes.Soft())
|