Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
except ImportError:
|
| 10 |
-
raise ImportError("bitsandbytes not installed! Add it to requirements.txt")
|
| 11 |
|
| 12 |
-
#
|
| 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 |
-
|
| 20 |
-
torch_dtype=
|
| 21 |
model_kwargs={
|
| 22 |
-
"low_cpu_mem_usage": True
|
|
|
|
| 23 |
}
|
| 24 |
)
|
| 25 |
|
| 26 |
def solve_math(question):
|
| 27 |
-
prompt = f"Solve
|
| 28 |
try:
|
| 29 |
result = math_pipeline(
|
| 30 |
prompt,
|
| 31 |
-
max_new_tokens=
|
| 32 |
temperature=0.3,
|
| 33 |
-
do_sample=False
|
|
|
|
| 34 |
)
|
| 35 |
return result[0]['generated_text'].split("A:")[-1].strip()
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error: {str(e)}"
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 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")
|