Spaces:
Running
Running
File size: 2,962 Bytes
db4681c 8a2324e db4681c 7f5bdbb db4681c 7f5bdbb db4681c 8a2324e db4681c 7f5bdbb 768188f db4681c 7f5bdbb 8a2324e d2c8962 7f5bdbb 8a2324e c5d8c3b 6d02010 c5d8c3b 768188f 8a2324e d2c8962 8a2324e 7f5bdbb 768188f 7f5bdbb 768188f db4681c 8a2324e aa4acd3 db4681c 768188f db4681c aa4acd3 db4681c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
from datasets import load_dataset
# Model details
MODEL_NAME = "Pisethan/sangapac-math"
# Load model and tokenizer
try:
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
except Exception as e:
classifier = None
print(f"Error loading model or tokenizer: {e}")
# Load dataset dynamically from Hugging Face or locally
try:
dataset = load_dataset("Pisethan/sangapac-math-dataset")["train"] # Load your dataset
dataset_dict = {entry["input"]: entry for entry in dataset} # Create a dictionary for lookup
except Exception as e:
dataset_dict = {}
print(f"Error loading dataset: {e}")
def predict(input_text):
if classifier is None:
return "Model not loaded properly.", {"Error": "Model not loaded properly."}
try:
# Predict the category
result = classifier(input_text)
label = result[0]["label"]
score = result[0]["score"]
# Retrieve output and metadata dynamically from the dataset
data = dataset_dict.get(input_text, {"output": "Unknown", "metadata": {}})
output = data["output"]
metadata = data["metadata"]
# Extract metadata details
difficulty = metadata.get("difficulty", "Unknown")
steps = metadata.get("steps", ["No steps available"])
# Create a simple result string without dashes
steps_text = "\n".join(steps) # No dash or prefix for each step
simple_result = (
f"Category: {label}\n"
f"Confidence: {score:.2f}\n"
f"Result: {output}\n"
f"Difficulty: {difficulty}\n"
f"Steps:\n{steps_text}"
)
# Create the full JSON output
detailed_result = {
"Category": label,
"Confidence": score,
"Output (Result)": output,
"Metadata": metadata,
}
return simple_result, detailed_result
except Exception as e:
return "An error occurred.", {"Error": str(e)}
# Gradio interface
import gradio as gr
# Define sample inputs
sample_inputs = [
["1 + 1 = ?"],
["(5 + 3) × 2 = ?"],
["12 ÷ 4 = ?"],
["Solve for x: x + 5 = 10"],
]
interface = gr.Interface(
fn=predict,
inputs=gr.Textbox(lines=2, placeholder="Enter a math problem..."),
outputs=[
gr.Textbox(label="Simple Output"), # Display only the result
gr.JSON(label="Detailed JSON Output"), # Display full JSON
],
title="Sangapac Math Model",
description="A model to classify math problems into categories like Arithmetic, Multiplication, Division, Algebra, and Geometry.",
examples=sample_inputs, # Add examples below the Clear and Submit buttons
)
# Launch the app
interface.launch()
|