Spaces:
Running
Running
File size: 2,359 Bytes
db4681c 8a2324e db4681c 7f5bdbb db4681c 7f5bdbb db4681c 8a2324e db4681c 7f5bdbb 768188f db4681c 7f5bdbb 8a2324e d2c8962 7f5bdbb 8a2324e 768188f 8a2324e d2c8962 8a2324e 7f5bdbb 768188f 7f5bdbb 768188f db4681c 8a2324e db4681c 768188f 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 |
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"]
# Create a simple result string
simple_result = f"Category: {label}\nConfidence: {score:.2f}\nResult: {output}"
# 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
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.",
)
# Launch the app
interface.launch()
|