Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,42 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Map
|
| 13 |
-
label_map = {
|
| 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 |
-
def classify_numbers(input1, input2, input3):
|
| 44 |
-
inputs = [input1, input2, input3]
|
| 45 |
-
predictions = {}
|
| 46 |
-
|
| 47 |
-
for i, value in enumerate(inputs):
|
| 48 |
-
result = classifier(value)[0] # full list of label scores
|
| 49 |
-
number_index = str(i + 1)
|
| 50 |
-
|
| 51 |
-
input_result = {}
|
| 52 |
-
|
| 53 |
-
for item in result:
|
| 54 |
-
label = label_map[item["label"]]
|
| 55 |
-
if label.endswith(number_index):
|
| 56 |
-
input_result[label] = round(item["score"], 3)
|
| 57 |
-
|
| 58 |
-
# ✅ Sort the results by score (highest to lowest)
|
| 59 |
-
input_result = dict(
|
| 60 |
-
sorted(input_result.items(), key=lambda x: x[1], reverse=True)
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
predictions[f"Input{number_index}"] = input_result
|
| 64 |
-
|
| 65 |
-
return predictions
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# Gradio UI
|
| 71 |
-
iface = gr.Interface(
|
| 72 |
-
fn=classify_numbers,
|
| 73 |
-
inputs=["text", "text", "text"],
|
| 74 |
-
outputs="json",
|
| 75 |
-
title="Number Classifier",
|
| 76 |
-
description="Enter three numbers or expressions to classify them (e.g., 2, pi, 3.5)"
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
iface.launch()
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import uvicorn
|
| 5 |
+
|
| 6 |
+
# Load classifier
|
| 7 |
+
classifier = pipeline("text-classification", model="WarTitan2077/Number-Classifier", tokenizer="WarTitan2077/Number-Classifier", top_k=None)
|
| 8 |
+
|
| 9 |
+
# Define labels (same as during training)
|
| 10 |
+
labels = ["Symbolic", "Numeric", "Natural", "Integer", "Rational", "Irrational", "Real", "Prime", "Composite"]
|
| 11 |
+
|
| 12 |
+
# Map label IDs back to names
|
| 13 |
+
label_map = {f"LABEL_{i}": label for i, label in enumerate(labels)}
|
| 14 |
+
|
| 15 |
+
# Input schema
|
| 16 |
+
class Inputs(BaseModel):
|
| 17 |
+
Input1: str
|
| 18 |
+
Input2: str
|
| 19 |
+
Input3: str
|
| 20 |
+
|
| 21 |
+
# App
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
@app.post("/predict")
|
| 25 |
+
async def predict(data: Inputs):
|
| 26 |
+
inputs = {
|
| 27 |
+
"Input1": data.Input1,
|
| 28 |
+
"Input2": data.Input2,
|
| 29 |
+
"Input3": data.Input3
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
response = {}
|
| 33 |
+
|
| 34 |
+
for key, value in inputs.items():
|
| 35 |
+
if value.strip(): # only classify non-empty inputs
|
| 36 |
+
result = classifier(value)
|
| 37 |
+
preds = {label_map[item["label"]]: round(item["score"], 3) for item in result[0]}
|
| 38 |
+
response[key] = preds
|
| 39 |
+
else:
|
| 40 |
+
response[key] = {}
|
| 41 |
+
|
| 42 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|