Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
MODEL_ID,
|
| 10 |
+
torch_dtype = torch.float32,
|
| 11 |
+
device_map = "cpu"
|
| 12 |
+
)
|
| 13 |
+
print("Model loaded!")
|
| 14 |
+
|
| 15 |
+
def solve(question, history):
|
| 16 |
+
messages = [
|
| 17 |
+
{"role": "system", "content": "You are a helpful math tutor. Solve problems step by step, showing all working clearly."},
|
| 18 |
+
]
|
| 19 |
+
for h in history:
|
| 20 |
+
messages.append({"role": "user", "content": h[0]})
|
| 21 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 22 |
+
messages.append({"role": "user", "content": question})
|
| 23 |
+
|
| 24 |
+
text = tokenizer.apply_chat_template(
|
| 25 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 26 |
+
)
|
| 27 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 28 |
+
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
out = model.generate(
|
| 31 |
+
**inputs,
|
| 32 |
+
max_new_tokens = 512,
|
| 33 |
+
temperature = 0.1,
|
| 34 |
+
do_sample = True,
|
| 35 |
+
pad_token_id = tokenizer.eos_token_id,
|
| 36 |
+
)
|
| 37 |
+
return tokenizer.decode(
|
| 38 |
+
out[0][inputs["input_ids"].shape[1]:],
|
| 39 |
+
skip_special_tokens=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Math Tutor AI") as demo:
|
| 43 |
+
gr.Markdown("""
|
| 44 |
+
# 🧮 Math Tutor AI
|
| 45 |
+
### Powered by Qwen2.5-Math — Fine-tuned for NCERT & competitive math
|
| 46 |
+
""")
|
| 47 |
+
|
| 48 |
+
chatbot = gr.Chatbot(height=400, label="Math Tutor")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
question = gr.Textbox(
|
| 52 |
+
placeholder = "Type your math question here...",
|
| 53 |
+
label = "Your Question",
|
| 54 |
+
scale = 4
|
| 55 |
+
)
|
| 56 |
+
submit = gr.Button("Solve →", variant="primary", scale=1)
|
| 57 |
+
|
| 58 |
+
gr.Examples(
|
| 59 |
+
examples=[
|
| 60 |
+
"Solve: 2x² - 7x + 3 = 0",
|
| 61 |
+
"Find the area of a triangle with base 12 cm and height 8 cm.",
|
| 62 |
+
"If sin θ = 3/5, find cos θ and tan θ.",
|
| 63 |
+
"A train travels 360 km in 4 hours. Find its speed in m/s.",
|
| 64 |
+
],
|
| 65 |
+
inputs=question
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
def respond(q, history):
|
| 69 |
+
answer = solve(q, history)
|
| 70 |
+
history.append((q, answer))
|
| 71 |
+
return "", history
|
| 72 |
+
|
| 73 |
+
submit.click(respond, [question, chatbot], [question, chatbot])
|
| 74 |
+
question.submit(respond, [question, chatbot], [question, chatbot])
|
| 75 |
+
|
| 76 |
+
demo.launch()
|