Scaryscar commited on
Commit
0b0079d
·
verified ·
1 Parent(s): a895fd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -1,17 +1,16 @@
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,
@@ -22,25 +21,24 @@ math_pipeline = pipeline(
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")
 
1
  from transformers import pipeline
2
  import gradio as gr
3
  import torch
 
4
 
5
+ # Verify GPU
6
  if not torch.cuda.is_available():
7
+ raise RuntimeError("Enable GPU in Space settings")
8
 
9
+ # Load model (without bitsandbytes)
10
+ model = pipeline(
11
  "text-generation",
12
  model="google/gemma-2b-it",
13
+ device=0, # Force GPU
14
  torch_dtype=torch.float16,
15
  model_kwargs={
16
  "low_cpu_mem_usage": True,
 
21
  def solve_math(question):
22
  prompt = f"Solve step by step:\nQ: {question}\nA:"
23
  try:
24
+ result = model(
25
  prompt,
26
+ max_new_tokens=100,
27
  temperature=0.3,
28
+ do_sample=False
 
29
  )
30
  return result[0]['generated_text'].split("A:")[-1].strip()
31
  except Exception as e:
32
  return f"Error: {str(e)}"
33
 
34
+ # Preload
35
  solve_math("2+2=")
36
 
37
+ # Simple UI
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("## Math Solver")
40
+ question = gr.Textbox(label="Problem")
41
+ answer = gr.Textbox(label="Solution")
42
  question.submit(solve_math, question, answer)
43
 
44
+ demo.launch()