adi2606 commited on
Commit
c8a4e18
ยท
verified ยท
1 Parent(s): 4c4de9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -38
app.py CHANGED
@@ -1,47 +1,75 @@
1
  import torch
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
4
 
5
- # Load tokenizer and model
6
- tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Math-1.5B")
7
- model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Math-1.5B", torch_dtype=torch.bfloat16, device_map="cpu")
8
 
9
-
10
- from transformers import GenerationConfig
11
- model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen2.5-Math-1.5B")
12
  model.generation_config.pad_token_id = model.generation_config.eos_token_id
13
 
14
 
15
- def solve_math_problem(questions):
16
- if isinstance(questions, str): # If input is a single string
17
- questions = [questions]
18
-
19
- results = []
20
- for question in questions:
21
- messages = [{"role": "user", "content": question}]
22
- input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
23
- outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
24
- result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
25
- results.append(result)
26
- return results
27
-
28
-
29
- interface = gr.Interface(
30
- fn=solve_math_problem,
31
- inputs="text",
32
- outputs="text",
33
- title="Math Wizard",
34
- description="""
35
- Welcome to the Math Wizard!
36
- Ask any math question, and let the wizard guide you through the solution step-by-step.
37
- """,
38
- allow_flagging=False,
39
- examples=[
40
- ["What is the integral of x^2?"],
41
- ["How do I solve a quadratic equation?"],
42
- ["Tell me about Ramanujan"]
43
- ]
44
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  if __name__ == "__main__":
47
- interface.launch()
 
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()