Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from transformers import pipeline, AutoTokenizer,
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# Model name and Hugging Face token
|
|
@@ -7,30 +7,30 @@ MODEL_NAME = "Pisethan/sangapac-math"
|
|
| 7 |
TOKEN = os.getenv("HF_API_TOKEN")
|
| 8 |
|
| 9 |
if not TOKEN:
|
| 10 |
-
raise ValueError("Hugging Face API token not found. Set it as an environment variable.")
|
| 11 |
|
| 12 |
# Load model and tokenizer
|
| 13 |
try:
|
| 14 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME,
|
| 15 |
-
model =
|
| 16 |
-
|
| 17 |
except Exception as e:
|
| 18 |
-
|
| 19 |
print(f"Error loading model or tokenizer: {e}")
|
| 20 |
|
| 21 |
def predict(input_text):
|
| 22 |
-
if
|
| 23 |
return "Model not loaded properly.", {"Error": "Model not loaded properly."}
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
simple_result = f"
|
| 31 |
detailed_result = {
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
}
|
| 35 |
|
| 36 |
return simple_result, detailed_result
|
|
@@ -53,7 +53,10 @@ interface = gr.Interface(
|
|
| 53 |
gr.JSON(label="Detailed JSON Output"),
|
| 54 |
],
|
| 55 |
title="Sangapac Math Model",
|
| 56 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 57 |
examples=sample_inputs,
|
| 58 |
)
|
| 59 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# Model name and Hugging Face token
|
|
|
|
| 7 |
TOKEN = os.getenv("HF_API_TOKEN")
|
| 8 |
|
| 9 |
if not TOKEN:
|
| 10 |
+
raise ValueError("Hugging Face API token not found. Set it as an environment variable (HF_API_TOKEN).")
|
| 11 |
|
| 12 |
# Load model and tokenizer
|
| 13 |
try:
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=TOKEN)
|
| 15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, use_auth_token=TOKEN)
|
| 16 |
+
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 17 |
except Exception as e:
|
| 18 |
+
generator = None
|
| 19 |
print(f"Error loading model or tokenizer: {e}")
|
| 20 |
|
| 21 |
def predict(input_text):
|
| 22 |
+
if generator is None:
|
| 23 |
return "Model not loaded properly.", {"Error": "Model not loaded properly."}
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
# Generate output
|
| 27 |
+
result = generator(input_text, max_length=256, num_beams=5, early_stopping=True)
|
| 28 |
+
generated_text = result[0]["generated_text"]
|
| 29 |
|
| 30 |
+
simple_result = f"Generated Solution:\n{generated_text}"
|
| 31 |
detailed_result = {
|
| 32 |
+
"Input": input_text,
|
| 33 |
+
"Generated Solution": generated_text,
|
| 34 |
}
|
| 35 |
|
| 36 |
return simple_result, detailed_result
|
|
|
|
| 53 |
gr.JSON(label="Detailed JSON Output"),
|
| 54 |
],
|
| 55 |
title="Sangapac Math Model",
|
| 56 |
+
description=(
|
| 57 |
+
"A model that solves math problems and provides step-by-step solutions. "
|
| 58 |
+
"Examples include Arithmetic, Multiplication, Division, Algebra, and Geometry problems."
|
| 59 |
+
),
|
| 60 |
examples=sample_inputs,
|
| 61 |
)
|
| 62 |
|