Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
print("Loading model...")
|
| 6 |
MODEL_ID = "newtechdevng/qwen-math-tutor"
|
| 7 |
|
|
@@ -14,13 +15,13 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 14 |
model.eval()
|
| 15 |
print("โ
Model loaded!")
|
| 16 |
|
|
|
|
| 17 |
def solve(question, history):
|
| 18 |
messages = [
|
| 19 |
{"role": "system", "content": "You are a helpful math tutor. Solve problems step by step, showing all working clearly."},
|
| 20 |
]
|
| 21 |
-
for
|
| 22 |
-
messages.append({"role": "
|
| 23 |
-
messages.append({"role": "assistant", "content": h[1]})
|
| 24 |
messages.append({"role": "user", "content": question})
|
| 25 |
|
| 26 |
text = tokenizer.apply_chat_template(
|
|
@@ -41,38 +42,62 @@ def solve(question, history):
|
|
| 41 |
skip_special_tokens=True
|
| 42 |
)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
with gr.Blocks(title="Math Tutor AI") as demo:
|
| 45 |
gr.Markdown("""
|
| 46 |
# ๐งฎ Math Tutor AI
|
| 47 |
### Powered by Qwen2.5-Math โ Fine-tuned for NCERT & competitive math
|
|
|
|
| 48 |
""")
|
| 49 |
|
| 50 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
question = gr.Textbox(
|
| 54 |
placeholder = "Type your math question here...",
|
| 55 |
label = "Your Question",
|
| 56 |
-
|
|
|
|
| 57 |
)
|
| 58 |
submit = gr.Button("Solve โ", variant="primary", scale=1)
|
| 59 |
|
|
|
|
|
|
|
| 60 |
gr.Examples(
|
|
|
|
| 61 |
examples=[
|
| 62 |
"Solve: 2xยฒ - 7x + 3 = 0",
|
| 63 |
"Find the area of a triangle with base 12 cm and height 8 cm.",
|
| 64 |
"If sin ฮธ = 3/5, find cos ฮธ and tan ฮธ.",
|
| 65 |
"A train travels 360 km in 4 hours. Find its speed in m/s.",
|
|
|
|
|
|
|
| 66 |
],
|
| 67 |
-
inputs=question
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
|
|
|
| 75 |
submit.click(respond, [question, chatbot], [question, chatbot])
|
| 76 |
question.submit(respond, [question, chatbot], [question, chatbot])
|
|
|
|
| 77 |
|
| 78 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# โโ Load Model โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 6 |
print("Loading model...")
|
| 7 |
MODEL_ID = "newtechdevng/qwen-math-tutor"
|
| 8 |
|
|
|
|
| 15 |
model.eval()
|
| 16 |
print("โ
Model loaded!")
|
| 17 |
|
| 18 |
+
# โโ Inference Function โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 19 |
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 msg in history:
|
| 24 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
|
|
|
| 25 |
messages.append({"role": "user", "content": question})
|
| 26 |
|
| 27 |
text = tokenizer.apply_chat_template(
|
|
|
|
| 42 |
skip_special_tokens=True
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# โโ Response Handler โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 46 |
+
def respond(question, history):
|
| 47 |
+
if not question.strip():
|
| 48 |
+
return "", history
|
| 49 |
+
answer = solve(question, history)
|
| 50 |
+
history.append({"role": "user", "content": question})
|
| 51 |
+
history.append({"role": "assistant", "content": answer})
|
| 52 |
+
return "", history
|
| 53 |
+
|
| 54 |
+
# โโ UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 55 |
with gr.Blocks(title="Math Tutor AI") as demo:
|
| 56 |
gr.Markdown("""
|
| 57 |
# ๐งฎ Math Tutor AI
|
| 58 |
### Powered by Qwen2.5-Math โ Fine-tuned for NCERT & competitive math
|
| 59 |
+
---
|
| 60 |
""")
|
| 61 |
|
| 62 |
+
chatbot = gr.Chatbot(
|
| 63 |
+
height = 450,
|
| 64 |
+
label = "Math Tutor",
|
| 65 |
+
type = "messages",
|
| 66 |
+
show_copy_button = True,
|
| 67 |
+
)
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
question = gr.Textbox(
|
| 71 |
placeholder = "Type your math question here...",
|
| 72 |
label = "Your Question",
|
| 73 |
+
lines = 1,
|
| 74 |
+
scale = 4,
|
| 75 |
)
|
| 76 |
submit = gr.Button("Solve โ", variant="primary", scale=1)
|
| 77 |
|
| 78 |
+
clear = gr.Button("๐๏ธ Clear Chat", variant="secondary")
|
| 79 |
+
|
| 80 |
gr.Examples(
|
| 81 |
+
label = "Try these examples:",
|
| 82 |
examples=[
|
| 83 |
"Solve: 2xยฒ - 7x + 3 = 0",
|
| 84 |
"Find the area of a triangle with base 12 cm and height 8 cm.",
|
| 85 |
"If sin ฮธ = 3/5, find cos ฮธ and tan ฮธ.",
|
| 86 |
"A train travels 360 km in 4 hours. Find its speed in m/s.",
|
| 87 |
+
"Find the compound interest on โน5000 at 10% per annum for 2 years.",
|
| 88 |
+
"Prove that โ2 is irrational.",
|
| 89 |
],
|
| 90 |
+
inputs = question,
|
| 91 |
)
|
| 92 |
|
| 93 |
+
gr.Markdown("""
|
| 94 |
+
---
|
| 95 |
+
*Model: Qwen2.5-Math-1.5B fine-tuned on 10K math problems*
|
| 96 |
+
""")
|
| 97 |
|
| 98 |
+
# โโ Event Handlers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 99 |
submit.click(respond, [question, chatbot], [question, chatbot])
|
| 100 |
question.submit(respond, [question, chatbot], [question, chatbot])
|
| 101 |
+
clear.click(lambda: ([], ""), outputs=[chatbot, question])
|
| 102 |
|
| 103 |
demo.launch(theme=gr.themes.Soft())
|