Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
from datasets import load_dataset
|
| 4 |
|
| 5 |
-
#
|
| 6 |
MODEL_NAME = "Pisethan/sangapac-math"
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
@@ -12,10 +13,10 @@ except Exception as e:
|
|
| 12 |
classifier = None
|
| 13 |
print(f"Error loading model or tokenizer: {e}")
|
| 14 |
|
| 15 |
-
# Load dataset
|
| 16 |
try:
|
| 17 |
-
dataset = load_dataset("Pisethan/sangapac-math-dataset")["train"]
|
| 18 |
-
dataset_dict = {entry["input"]: entry for entry in dataset}
|
| 19 |
except Exception as e:
|
| 20 |
dataset_dict = {}
|
| 21 |
print(f"Error loading dataset: {e}")
|
|
@@ -26,18 +27,22 @@ def predict(input_text):
|
|
| 26 |
return "Model not loaded properly.", {"Error": "Model not loaded properly."}
|
| 27 |
|
| 28 |
try:
|
|
|
|
| 29 |
result = classifier(input_text)
|
| 30 |
label = result[0]["label"]
|
| 31 |
score = result[0]["score"]
|
| 32 |
|
|
|
|
| 33 |
data = dataset_dict.get(input_text, {"output": "Unknown", "metadata": {}})
|
| 34 |
output = data["output"]
|
| 35 |
metadata = data["metadata"]
|
| 36 |
|
|
|
|
| 37 |
difficulty = metadata.get("difficulty", "Unknown")
|
| 38 |
steps = metadata.get("steps", ["No steps available"])
|
| 39 |
|
| 40 |
-
|
|
|
|
| 41 |
simple_result = (
|
| 42 |
f"Category: {label}\n"
|
| 43 |
f"Confidence: {score:.2f}\n"
|
|
@@ -46,6 +51,7 @@ def predict(input_text):
|
|
| 46 |
f"Steps:\n{steps_text}"
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
detailed_result = {
|
| 50 |
"Category": label,
|
| 51 |
"Confidence": score,
|
|
@@ -57,6 +63,8 @@ def predict(input_text):
|
|
| 57 |
except Exception as e:
|
| 58 |
return "An error occurred.", {"Error": str(e)}
|
| 59 |
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Define sample inputs
|
| 62 |
sample_inputs = [
|
|
@@ -66,51 +74,17 @@ sample_inputs = [
|
|
| 66 |
["Solve for x: x + 5 = 10"],
|
| 67 |
]
|
| 68 |
|
| 69 |
-
# Define Gradio interface
|
| 70 |
interface = gr.Interface(
|
| 71 |
fn=predict,
|
| 72 |
inputs=gr.Textbox(lines=2, placeholder="Enter a math problem..."),
|
| 73 |
outputs=[
|
| 74 |
-
gr.Textbox(label="Simple Output"),
|
| 75 |
-
gr.JSON(label="Detailed JSON Output"),
|
| 76 |
],
|
| 77 |
title="Sangapac Math Model",
|
| 78 |
description="A model to classify math problems into categories like Arithmetic, Multiplication, Division, Algebra, and Geometry.",
|
| 79 |
-
examples=sample_inputs,
|
| 80 |
-
allow_flagging="never",
|
| 81 |
)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
<script>
|
| 86 |
-
function addSampleButtons() {
|
| 87 |
-
const buttonsContainer = document.createElement('div');
|
| 88 |
-
buttonsContainer.style.display = 'flex';
|
| 89 |
-
buttonsContainer.style.justifyContent = 'center';
|
| 90 |
-
buttonsContainer.style.marginTop = '10px';
|
| 91 |
-
|
| 92 |
-
const examples = [
|
| 93 |
-
"1 + 1 = ?",
|
| 94 |
-
"(5 + 3) × 2 = ?",
|
| 95 |
-
"12 ÷ 4 = ?",
|
| 96 |
-
"Solve for x: x + 5 = 10"
|
| 97 |
-
];
|
| 98 |
-
|
| 99 |
-
examples.forEach((example, index) => {
|
| 100 |
-
const button = document.createElement('button');
|
| 101 |
-
button.innerHTML = example;
|
| 102 |
-
button.style.margin = '0 10px';
|
| 103 |
-
button.onclick = function() {
|
| 104 |
-
document.querySelector('textarea').value = example;
|
| 105 |
-
};
|
| 106 |
-
buttonsContainer.appendChild(button);
|
| 107 |
-
});
|
| 108 |
-
|
| 109 |
-
document.querySelector('body > div').appendChild(buttonsContainer);
|
| 110 |
-
}
|
| 111 |
-
window.onload = addSampleButtons;
|
| 112 |
-
</script>
|
| 113 |
-
"""
|
| 114 |
-
|
| 115 |
-
# Launch Gradio app with custom JavaScript
|
| 116 |
-
interface.launch(components=custom_js)
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
from datasets import load_dataset
|
| 3 |
|
| 4 |
+
# Model details
|
| 5 |
MODEL_NAME = "Pisethan/sangapac-math"
|
| 6 |
+
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
try:
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
|
|
| 13 |
classifier = None
|
| 14 |
print(f"Error loading model or tokenizer: {e}")
|
| 15 |
|
| 16 |
+
# Load dataset dynamically from Hugging Face or locally
|
| 17 |
try:
|
| 18 |
+
dataset = load_dataset("Pisethan/sangapac-math-dataset")["train"] # Load your dataset
|
| 19 |
+
dataset_dict = {entry["input"]: entry for entry in dataset} # Create a dictionary for lookup
|
| 20 |
except Exception as e:
|
| 21 |
dataset_dict = {}
|
| 22 |
print(f"Error loading dataset: {e}")
|
|
|
|
| 27 |
return "Model not loaded properly.", {"Error": "Model not loaded properly."}
|
| 28 |
|
| 29 |
try:
|
| 30 |
+
# Predict the category
|
| 31 |
result = classifier(input_text)
|
| 32 |
label = result[0]["label"]
|
| 33 |
score = result[0]["score"]
|
| 34 |
|
| 35 |
+
# Retrieve output and metadata dynamically from the dataset
|
| 36 |
data = dataset_dict.get(input_text, {"output": "Unknown", "metadata": {}})
|
| 37 |
output = data["output"]
|
| 38 |
metadata = data["metadata"]
|
| 39 |
|
| 40 |
+
# Extract metadata details
|
| 41 |
difficulty = metadata.get("difficulty", "Unknown")
|
| 42 |
steps = metadata.get("steps", ["No steps available"])
|
| 43 |
|
| 44 |
+
# Create a simple result string without dashes
|
| 45 |
+
steps_text = "\n".join(steps) # No dash or prefix for each step
|
| 46 |
simple_result = (
|
| 47 |
f"Category: {label}\n"
|
| 48 |
f"Confidence: {score:.2f}\n"
|
|
|
|
| 51 |
f"Steps:\n{steps_text}"
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Create the full JSON output
|
| 55 |
detailed_result = {
|
| 56 |
"Category": label,
|
| 57 |
"Confidence": score,
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
return "An error occurred.", {"Error": str(e)}
|
| 65 |
|
| 66 |
+
# Gradio interface
|
| 67 |
+
import gradio as gr
|
| 68 |
|
| 69 |
# Define sample inputs
|
| 70 |
sample_inputs = [
|
|
|
|
| 74 |
["Solve for x: x + 5 = 10"],
|
| 75 |
]
|
| 76 |
|
|
|
|
| 77 |
interface = gr.Interface(
|
| 78 |
fn=predict,
|
| 79 |
inputs=gr.Textbox(lines=2, placeholder="Enter a math problem..."),
|
| 80 |
outputs=[
|
| 81 |
+
gr.Textbox(label="Simple Output"), # Display only the result
|
| 82 |
+
gr.JSON(label="Detailed JSON Output"), # Display full JSON
|
| 83 |
],
|
| 84 |
title="Sangapac Math Model",
|
| 85 |
description="A model to classify math problems into categories like Arithmetic, Multiplication, Division, Algebra, and Geometry.",
|
| 86 |
+
examples=sample_inputs, # Add examples below the Clear and Submit buttons
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
+
# Launch the app
|
| 90 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|