Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,75 @@
|
|
1 |
import torch
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Math-1.5B", torch_dtype=torch.bfloat16, device_map="cpu")
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
model.generation_config = GenerationConfig.from_pretrained(
|
12 |
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
13 |
|
14 |
|
15 |
-
def solve_math_problem(
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
-
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Lightweight CPU-friendly model
|
6 |
+
model_name = "microsoft/phi-1_5"
|
|
|
7 |
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
+
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
11 |
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
12 |
|
13 |
|
14 |
+
def solve_math_problem(question):
|
15 |
+
messages = [{"role": "user", "content": question}]
|
16 |
+
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
17 |
+
input_tensor = input_tensor.to(model.device)
|
18 |
+
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model.generate(input_tensor, max_new_tokens=150)
|
21 |
+
|
22 |
+
response = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
23 |
+
return response.strip()
|
24 |
+
|
25 |
+
|
26 |
+
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
27 |
+
gr.Markdown("# ๐งโโ๏ธ Math Wizard AI")
|
28 |
+
gr.Markdown("""
|
29 |
+
<div style="font-size: 16px; line-height: 1.5">
|
30 |
+
Welcome to the <b>Math Wizard</b> โ your intelligent assistant for solving math problems of all kinds! <br>
|
31 |
+
Ask anything from algebra, calculus, or even about famous mathematicians.<br><br>
|
32 |
+
</div>
|
33 |
+
""")
|
34 |
+
|
35 |
+
with gr.Tabs():
|
36 |
+
with gr.Tab("๐งฎ General Math"):
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column():
|
39 |
+
question_box = gr.Textbox(
|
40 |
+
label="Ask your question here:",
|
41 |
+
placeholder="E.g. What is the derivative of x^2 + 3x + 2?",
|
42 |
+
lines=3
|
43 |
+
)
|
44 |
+
submit_btn = gr.Button("๐ Solve Now")
|
45 |
+
clear_btn = gr.Button("โ Clear")
|
46 |
+
|
47 |
+
with gr.Column():
|
48 |
+
answer_box = gr.Textbox(label="๐ Answer from the Wizard", lines=8, interactive=False)
|
49 |
+
copy_btn = gr.Button("๐ Copy Answer")
|
50 |
+
|
51 |
+
submit_btn.click(fn=solve_math_problem, inputs=question_box, outputs=answer_box)
|
52 |
+
clear_btn.click(lambda: ("", ""), outputs=[question_box, answer_box])
|
53 |
+
copy_btn.click(lambda x: x, inputs=answer_box, outputs=answer_box, show_progress=False)
|
54 |
+
|
55 |
+
with gr.Tab("๐ง Examples & Inspiration"):
|
56 |
+
gr.Markdown("""
|
57 |
+
<h4>Try asking things like:</h4>
|
58 |
+
<ul>
|
59 |
+
<li>๐งฉ Solve the equation xยฒ + 2x - 3 = 0</li>
|
60 |
+
<li>๐ What is the Pythagorean theorem?</li>
|
61 |
+
<li>๐ Integrate sin(x) from 0 to ฯ</li>
|
62 |
+
<li>๐งฎ Tell me about Euclid</li>
|
63 |
+
</ul>
|
64 |
+
""")
|
65 |
+
|
66 |
+
with gr.Tab("๐ About"):
|
67 |
+
gr.Markdown("""
|
68 |
+
<h4>About Math Wizard</h4>
|
69 |
+
<p>This assistant is powered by a lightweight AI model that runs smoothly even on CPUs.</p>
|
70 |
+
<p>Built with โค๏ธ using Gradio + HuggingFace Transformers</p>
|
71 |
+
<p>Model: <code>microsoft/phi-1_5</code> optimized for reasoning and small footprint.</p>
|
72 |
+
""")
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
+
demo.launch()
|