Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,88 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
device_map="auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
prompt = f"USER: Solve this math problem: {question}\nASSISTANT:"
|
| 17 |
|
| 18 |
-
# Generate response
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
inputs.input_ids,
|
| 22 |
max_new_tokens=256,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
pad_token_id=tokenizer.eos_token_id
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
answer =
|
| 29 |
-
|
| 30 |
return answer
|
| 31 |
|
| 32 |
# Gradio Interface
|
| 33 |
demo = gr.Interface(
|
| 34 |
-
fn=
|
| 35 |
-
inputs=gr.Textbox(
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
examples=[
|
| 39 |
-
["What is
|
| 40 |
-
["Solve for x:
|
| 41 |
-
["Calculate the area of a circle with radius
|
| 42 |
],
|
| 43 |
-
|
| 44 |
)
|
| 45 |
|
|
|
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import (
|
| 2 |
+
AutoModelForCausalLM,
|
| 3 |
+
AutoTokenizer,
|
| 4 |
+
BitsAndBytesConfig,
|
| 5 |
+
pipeline
|
| 6 |
+
)
|
| 7 |
import torch
|
| 8 |
+
import gradio as gr
|
| 9 |
|
| 10 |
+
# Configuration
|
| 11 |
+
MODEL_NAME = "WizardLM/WizardMath-7B-V1.1" # Use 7B model for Spaces
|
| 12 |
+
CACHE_DIR = "/tmp" # For Spaces limited storage
|
| 13 |
+
|
| 14 |
+
# 4-bit quantization setup
|
| 15 |
+
quant_config = BitsAndBytesConfig(
|
| 16 |
+
load_in_4bit=True,
|
| 17 |
+
bnb_4bit_quant_type="nf4",
|
| 18 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 19 |
+
bnb_4bit_use_double_quant=True
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Load model with optimizations
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir=CACHE_DIR)
|
| 24 |
model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
+
MODEL_NAME,
|
| 26 |
+
quantization_config=quant_config,
|
| 27 |
+
device_map="auto",
|
| 28 |
+
cache_dir=CACHE_DIR,
|
| 29 |
+
trust_remote_code=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Create a text generation pipeline
|
| 33 |
+
math_pipeline = pipeline(
|
| 34 |
+
"text-generation",
|
| 35 |
+
model=model,
|
| 36 |
+
tokenizer=tokenizer,
|
| 37 |
+
torch_dtype=torch.float16,
|
| 38 |
+
device_map="auto"
|
| 39 |
)
|
| 40 |
|
| 41 |
+
def solve_math(question):
|
| 42 |
+
prompt = f"USER: {question}\nASSISTANT:"
|
|
|
|
| 43 |
|
| 44 |
+
# Generate response with adjusted parameters
|
| 45 |
+
outputs = math_pipeline(
|
| 46 |
+
prompt,
|
|
|
|
| 47 |
max_new_tokens=256,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
top_k=50,
|
| 51 |
+
top_p=0.95,
|
| 52 |
pad_token_id=tokenizer.eos_token_id
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Extract and clean the answer
|
| 56 |
+
full_response = outputs[0]["generated_text"]
|
| 57 |
+
answer = full_response.split("ASSISTANT:")[-1].strip()
|
|
|
|
| 58 |
return answer
|
| 59 |
|
| 60 |
# Gradio Interface
|
| 61 |
demo = gr.Interface(
|
| 62 |
+
fn=solve_math,
|
| 63 |
+
inputs=gr.Textbox(
|
| 64 |
+
label="Math Problem",
|
| 65 |
+
placeholder="Enter your math question here...",
|
| 66 |
+
lines=3
|
| 67 |
+
),
|
| 68 |
+
outputs=gr.Textbox(
|
| 69 |
+
label="Solution",
|
| 70 |
+
lines=5
|
| 71 |
+
),
|
| 72 |
+
title="🧮 WizardMath Solver",
|
| 73 |
+
description="Solves math problems using WizardMath-7B (4-bit quantized)",
|
| 74 |
examples=[
|
| 75 |
+
["What is 2^10 + 5*3?"],
|
| 76 |
+
["Solve for x: 3x + 7 = 22"],
|
| 77 |
+
["Calculate the area of a circle with radius 5"]
|
| 78 |
],
|
| 79 |
+
allow_flagging="never"
|
| 80 |
)
|
| 81 |
|
| 82 |
+
# Launch with Space-optimized settings
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
demo.launch(
|
| 85 |
+
server_name="0.0.0.0",
|
| 86 |
+
server_port=7860,
|
| 87 |
+
share=False # Set to True for public link during testing
|
| 88 |
+
)
|