Scaryscar commited on
Commit
6035c2a
·
verified ·
1 Parent(s): 2f2f576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -28
app.py CHANGED
@@ -1,47 +1,88 @@
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
 
 
 
 
 
1
+ from transformers import (
2
+ AutoModelForCausalLM,
3
+ AutoTokenizer,
4
+ BitsAndBytesConfig,
5
+ pipeline
6
+ )
7
  import torch
8
+ import gradio as gr
9
 
10
+ # Configuration
11
+ MODEL_NAME = "WizardLM/WizardMath-7B-V1.1" # Use 7B model for Spaces
12
+ CACHE_DIR = "/tmp" # For Spaces limited storage
13
+
14
+ # 4-bit quantization setup
15
+ quant_config = BitsAndBytesConfig(
16
+ load_in_4bit=True,
17
+ bnb_4bit_quant_type="nf4",
18
+ bnb_4bit_compute_dtype=torch.float16,
19
+ bnb_4bit_use_double_quant=True
20
+ )
21
+
22
+ # Load model with optimizations
23
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir=CACHE_DIR)
24
  model = AutoModelForCausalLM.from_pretrained(
25
+ MODEL_NAME,
26
+ quantization_config=quant_config,
27
+ device_map="auto",
28
+ cache_dir=CACHE_DIR,
29
+ trust_remote_code=True
30
+ )
31
+
32
+ # Create a text generation pipeline
33
+ math_pipeline = pipeline(
34
+ "text-generation",
35
+ model=model,
36
+ tokenizer=tokenizer,
37
+ torch_dtype=torch.float16,
38
+ device_map="auto"
39
  )
40
 
41
+ def solve_math(question):
42
+ prompt = f"USER: {question}\nASSISTANT:"
 
43
 
44
+ # Generate response with adjusted parameters
45
+ outputs = math_pipeline(
46
+ prompt,
 
47
  max_new_tokens=256,
48
+ do_sample=True,
49
+ temperature=0.7,
50
+ top_k=50,
51
+ top_p=0.95,
52
  pad_token_id=tokenizer.eos_token_id
53
  )
54
 
55
+ # Extract and clean the answer
56
+ full_response = outputs[0]["generated_text"]
57
+ answer = full_response.split("ASSISTANT:")[-1].strip()
 
58
  return answer
59
 
60
  # Gradio Interface
61
  demo = gr.Interface(
62
+ fn=solve_math,
63
+ inputs=gr.Textbox(
64
+ label="Math Problem",
65
+ placeholder="Enter your math question here...",
66
+ lines=3
67
+ ),
68
+ outputs=gr.Textbox(
69
+ label="Solution",
70
+ lines=5
71
+ ),
72
+ title="🧮 WizardMath Solver",
73
+ description="Solves math problems using WizardMath-7B (4-bit quantized)",
74
  examples=[
75
+ ["What is 2^10 + 5*3?"],
76
+ ["Solve for x: 3x + 7 = 22"],
77
+ ["Calculate the area of a circle with radius 5"]
78
  ],
79
+ allow_flagging="never"
80
  )
81
 
82
+ # Launch with Space-optimized settings
83
  if __name__ == "__main__":
84
+ demo.launch(
85
+ server_name="0.0.0.0",
86
+ server_port=7860,
87
+ share=False # Set to True for public link during testing
88
+ )