Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
|
|
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
|
@@ -15,34 +15,33 @@ except Exception as e:
|
|
15 |
|
16 |
# Load dataset dynamically from Hugging Face or locally
|
17 |
try:
|
18 |
-
dataset = load_dataset("Pisethan/sangapac-math-dataset")["train"]
|
19 |
-
dataset_dict = {entry["input"]: entry for entry in dataset}
|
20 |
except Exception as e:
|
21 |
dataset_dict = {}
|
22 |
print(f"Error loading dataset: {e}")
|
23 |
|
|
|
|
|
24 |
|
25 |
def predict(input_text):
|
26 |
if classifier is None:
|
27 |
return "Model not loaded properly.", {"Error": "Model not loaded properly."}
|
28 |
|
29 |
try:
|
30 |
-
|
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 |
-
|
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,7 +50,6 @@ def predict(input_text):
|
|
51 |
f"Steps:\n{steps_text}"
|
52 |
)
|
53 |
|
54 |
-
# Create the full JSON output
|
55 |
detailed_result = {
|
56 |
"Category": label,
|
57 |
"Confidence": score,
|
@@ -66,7 +64,6 @@ def predict(input_text):
|
|
66 |
# Gradio interface
|
67 |
import gradio as gr
|
68 |
|
69 |
-
# Define sample inputs
|
70 |
sample_inputs = [
|
71 |
["1 + 1 = ?"],
|
72 |
["(5 + 3) × 2 = ?"],
|
@@ -78,13 +75,12 @@ 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"),
|
82 |
-
gr.JSON(label="Detailed JSON Output"),
|
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,
|
87 |
)
|
88 |
|
89 |
-
# Launch the app
|
90 |
interface.launch()
|
|
|
1 |
+
import re
|
2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
from datasets import load_dataset
|
4 |
|
|
|
5 |
MODEL_NAME = "Pisethan/sangapac-math"
|
6 |
|
7 |
# Load model and tokenizer
|
|
|
15 |
|
16 |
# Load dataset dynamically from Hugging Face or locally
|
17 |
try:
|
18 |
+
dataset = load_dataset("Pisethan/sangapac-math-dataset")["train"]
|
19 |
+
dataset_dict = {re.sub(r'\s+', ' ', entry["input"].strip()): entry for entry in dataset}
|
20 |
except Exception as e:
|
21 |
dataset_dict = {}
|
22 |
print(f"Error loading dataset: {e}")
|
23 |
|
24 |
+
def normalize_input(text):
|
25 |
+
return re.sub(r'\s+', ' ', text.strip())
|
26 |
|
27 |
def predict(input_text):
|
28 |
if classifier is None:
|
29 |
return "Model not loaded properly.", {"Error": "Model not loaded properly."}
|
30 |
|
31 |
try:
|
32 |
+
input_text = normalize_input(input_text)
|
33 |
result = classifier(input_text)
|
34 |
label = result[0]["label"]
|
35 |
score = result[0]["score"]
|
36 |
|
|
|
37 |
data = dataset_dict.get(input_text, {"output": "Unknown", "metadata": {}})
|
38 |
output = data["output"]
|
39 |
metadata = data["metadata"]
|
40 |
|
|
|
41 |
difficulty = metadata.get("difficulty", "Unknown")
|
42 |
steps = metadata.get("steps", ["No steps available"])
|
43 |
|
44 |
+
steps_text = "\n".join(steps)
|
|
|
45 |
simple_result = (
|
46 |
f"Category: {label}\n"
|
47 |
f"Confidence: {score:.2f}\n"
|
|
|
50 |
f"Steps:\n{steps_text}"
|
51 |
)
|
52 |
|
|
|
53 |
detailed_result = {
|
54 |
"Category": label,
|
55 |
"Confidence": score,
|
|
|
64 |
# Gradio interface
|
65 |
import gradio as gr
|
66 |
|
|
|
67 |
sample_inputs = [
|
68 |
["1 + 1 = ?"],
|
69 |
["(5 + 3) × 2 = ?"],
|
|
|
75 |
fn=predict,
|
76 |
inputs=gr.Textbox(lines=2, placeholder="Enter a math problem..."),
|
77 |
outputs=[
|
78 |
+
gr.Textbox(label="Simple Output"),
|
79 |
+
gr.JSON(label="Detailed JSON Output"),
|
80 |
],
|
81 |
title="Sangapac Math Model",
|
82 |
description="A model to classify math problems into categories like Arithmetic, Multiplication, Division, Algebra, and Geometry.",
|
83 |
+
examples=sample_inputs,
|
84 |
)
|
85 |
|
|
|
86 |
interface.launch()
|