Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 7 |
if not torch.cuda.is_available():
|
| 8 |
-
raise RuntimeError("
|
| 9 |
|
| 10 |
-
# Load model
|
| 11 |
-
|
| 12 |
"text-generation",
|
| 13 |
model="google/gemma-2b-it",
|
| 14 |
-
device=0, # Force GPU
|
| 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 =
|
| 26 |
prompt,
|
| 27 |
-
max_new_tokens=
|
| 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
|
| 37 |
solve_math("2+2=")
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
with gr.Blocks(
|
| 41 |
-
gr.Markdown("##
|
| 42 |
-
question = gr.Textbox(label=""
|
| 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 |
|
| 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()
|