Scaryscar commited on
Commit
5ee5ebc
·
verified ·
1 Parent(s): 0e74353

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -1,46 +1,46 @@
1
  from transformers import pipeline
2
- from huggingface_hub import login
3
  import gradio as gr
 
4
  import os
5
 
6
- # 1. First verify bitsandbytes is installed
7
- try:
8
- import bitsandbytes # noqa
9
- except ImportError:
10
- raise ImportError("bitsandbytes not installed! Add it to requirements.txt")
11
 
12
- # 2. Authenticate
13
- login(token=os.environ.get("HF_TOKEN"))
14
-
15
- # 3. Load model WITHOUT 4-bit (for compatibility)
16
  math_pipeline = pipeline(
17
  "text-generation",
18
  model="google/gemma-2b-it",
19
- device_map="auto",
20
- torch_dtype="auto", # Let transformers choose dtype
21
  model_kwargs={
22
- "low_cpu_mem_usage": True # Reduces memory spikes
 
23
  }
24
  )
25
 
26
  def solve_math(question):
27
- prompt = f"Solve concisely:\nQ: {question}\nA:"
28
  try:
29
  result = math_pipeline(
30
  prompt,
31
- max_new_tokens=100, # Shorter = faster
32
  temperature=0.3,
33
- do_sample=False # Faster generation
 
34
  )
35
  return result[0]['generated_text'].split("A:")[-1].strip()
36
  except Exception as e:
37
  return f"Error: {str(e)}"
38
 
39
- # Minimal UI
40
- with gr.Blocks() as demo:
41
- gr.Markdown("## Math Solver")
42
- question = gr.Textbox(label="Problem")
43
- answer = gr.Textbox(label="Solution")
 
 
 
44
  question.submit(solve_math, question, answer)
45
 
46
- demo.launch()
 
1
  from transformers import pipeline
 
2
  import gradio as gr
3
+ import torch
4
  import os
5
 
6
+ # Verify GPU support
7
+ if not torch.cuda.is_available():
8
+ raise RuntimeError("GPU not available - enable GPU in Space settings")
 
 
9
 
10
+ # Load model with GPU acceleration
 
 
 
11
  math_pipeline = pipeline(
12
  "text-generation",
13
  model="google/gemma-2b-it",
14
+ device=0, # Force GPU usage
15
+ torch_dtype=torch.float16,
16
  model_kwargs={
17
+ "low_cpu_mem_usage": True,
18
+ "trust_remote_code": True
19
  }
20
  )
21
 
22
  def solve_math(question):
23
+ prompt = f"Solve step by step:\nQ: {question}\nA:"
24
  try:
25
  result = math_pipeline(
26
  prompt,
27
+ max_new_tokens=150,
28
  temperature=0.3,
29
+ do_sample=False,
30
+ pad_token_id=math_pipeline.tokenizer.eos_token_id
31
  )
32
  return result[0]['generated_text'].split("A:")[-1].strip()
33
  except Exception as e:
34
  return f"Error: {str(e)}"
35
 
36
+ # Preload model
37
+ solve_math("2+2=")
38
+
39
+ # Optimized UI
40
+ with gr.Blocks(title="🚀 Math Solver") as demo:
41
+ gr.Markdown("## Enter a math problem:")
42
+ question = gr.Textbox(label="", placeholder="What is 2^8?")
43
+ answer = gr.Textbox(label="Solution", lines=3)
44
  question.submit(solve_math, question, answer)
45
 
46
+ demo.launch(server_name="0.0.0.0")