Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 3 |
|
| 4 |
# Model details
|
| 5 |
MODEL_NAME = "Pisethan/sangapac-math"
|
|
@@ -13,6 +13,15 @@ except Exception as e:
|
|
| 13 |
classifier = None
|
| 14 |
print(f"Error loading model or tokenizer: {e}")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def predict(input_text):
|
| 17 |
if classifier is None:
|
| 18 |
return {"Error": "Model not loaded properly."}
|
|
@@ -20,17 +29,26 @@ def predict(input_text):
|
|
| 20 |
try:
|
| 21 |
# Predict the category
|
| 22 |
result = classifier(input_text)
|
| 23 |
-
label = result[0]["label"]
|
| 24 |
score = result[0]["score"]
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return {
|
| 27 |
-
"Category": label,
|
| 28 |
"Confidence": score,
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
except Exception as e:
|
| 31 |
return {"Error": str(e)}
|
| 32 |
|
| 33 |
# Gradio interface
|
|
|
|
|
|
|
| 34 |
interface = gr.Interface(
|
| 35 |
fn=predict,
|
| 36 |
inputs=gr.Textbox(lines=2, placeholder="Enter a math problem..."),
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
|
| 4 |
# Model details
|
| 5 |
MODEL_NAME = "Pisethan/sangapac-math"
|
|
|
|
| 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}")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
def predict(input_text):
|
| 26 |
if classifier is None:
|
| 27 |
return {"Error": "Model not loaded properly."}
|
|
|
|
| 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 |
return {
|
| 41 |
+
"Category": label,
|
| 42 |
"Confidence": score,
|
| 43 |
+
"Output (Result)": output,
|
| 44 |
+
"Metadata": metadata,
|
| 45 |
}
|
| 46 |
except Exception as e:
|
| 47 |
return {"Error": str(e)}
|
| 48 |
|
| 49 |
# Gradio interface
|
| 50 |
+
import gradio as gr
|
| 51 |
+
|
| 52 |
interface = gr.Interface(
|
| 53 |
fn=predict,
|
| 54 |
inputs=gr.Textbox(lines=2, placeholder="Enter a math problem..."),
|