Scaryscar commited on
Commit
2c63970
·
verified ·
1 Parent(s): f7e0cde

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+ import torch
4
+
5
+ # Load WizardMath (adjust model size if needed)
6
+ model_name = "WizardLM/WizardMath-7B-V1.1" # Smaller: "WizardLM/WizardMath-70B-V1.0"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.float16, # Reduce memory usage
11
+ device_map="auto" # Auto-select GPU/CPU
12
+ )
13
+
14
+ def solve_math_problem(question):
15
+ # Format input for WizardMath
16
+ prompt = f"USER: Solve this math problem: {question}\nASSISTANT:"
17
+
18
+ # Generate response
19
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
20
+ outputs = model.generate(
21
+ inputs.input_ids,
22
+ max_new_tokens=256,
23
+ pad_token_id=tokenizer.eos_token_id
24
+ )
25
+
26
+ # Decode and clean output
27
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
+ answer = answer.split("ASSISTANT:")[-1].strip() # Extract the answer part
29
+
30
+ return answer
31
+
32
+ # Gradio Interface
33
+ demo = gr.Interface(
34
+ fn=solve_math_problem,
35
+ inputs=gr.Textbox(lines=2, placeholder="Enter your math problem here..."),
36
+ outputs=gr.Textbox(label="Solution"),
37
+ title="🧙 WizardMath Problem Solver",
38
+ examples=[
39
+ ["What is the integral of x^2 from 0 to 3?"],
40
+ ["Solve for x: 2x + 5 = 15"],
41
+ ["Calculate the area of a circle with radius 4."]
42
+ ],
43
+ theme="soft" # Try "default" or "huggingface"
44
+ )
45
+
46
+ if __name__ == "__main__":
47
+ demo.launch(server_port=7860, share=False) # Set share=True for public link