Scaryscar commited on
Commit
368865e
·
verified ·
1 Parent(s): fad79cb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2
+ import torch
3
+ import gradio as gr
4
+
5
+ # Configuration
6
+ MODEL_NAME = "google/gemma-2b-it"
7
+ CACHE_DIR = "/tmp" # For Spaces storage limits
8
+
9
+ # Load model with 4-bit quantization (reduces VRAM usage by ~4x)
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir=CACHE_DIR)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ MODEL_NAME,
13
+ device_map="auto",
14
+ torch_dtype=torch.float16,
15
+ cache_dir=CACHE_DIR
16
+ )
17
+
18
+ # Create optimized pipeline
19
+ math_pipeline = pipeline(
20
+ "text-generation",
21
+ model=model,
22
+ tokenizer=tokenizer,
23
+ max_new_tokens=256,
24
+ do_sample=True,
25
+ temperature=0.7,
26
+ top_k=50,
27
+ top_p=0.95
28
+ )
29
+
30
+ def solve_math(question):
31
+ prompt = f"Question: {question}\nAnswer:"
32
+ outputs = math_pipeline(prompt)
33
+ return outputs[0]["generated_text"].split("Answer:")[-1].strip()
34
+
35
+ # Gradio Interface (optimized for mobile/desktop)
36
+ with gr.Blocks(title="🚀 Gemma-2B Math Solver") as demo:
37
+ gr.Markdown("## 🧮 Solve Math Problems with Gemma-2B")
38
+ with gr.Row():
39
+ input_question = gr.Textbox(
40
+ label="Enter your math problem",
41
+ placeholder="E.g., What is 2^10 + 5*3?",
42
+ lines=3
43
+ )
44
+ with gr.Row():
45
+ submit_btn = gr.Button("Solve", variant="primary")
46
+ with gr.Row():
47
+ output_answer = gr.Textbox(label="Solution", lines=5, interactive=False)
48
+
49
+ submit_btn.click(
50
+ fn=solve_math,
51
+ inputs=input_question,
52
+ outputs=output_answer
53
+ )
54
+
55
+ # Launch with production settings
56
+ if __name__ == "__main__":
57
+ demo.launch(
58
+ server_name="0.0.0.0",
59
+ server_port=7860,
60
+ share=False # Set to True for temporary public link
61
+ )