WarTitan2077 commited on
Commit
124c819
·
verified ·
1 Parent(s): 14c3783

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -78
app.py CHANGED
@@ -1,79 +1,42 @@
1
  from transformers import pipeline
2
- import gradio as gr
3
-
4
- # Load model and tokenizer
5
- classifier = pipeline(
6
- "text-classification",
7
- model="WarTitan2077/Number-Classifier",
8
- tokenizer="WarTitan2077/Number-Classifier",
9
- top_k=None
10
- )
11
-
12
- # Map Hugging Face's LABEL_X to human-readable labels
13
- label_map = {
14
- "LABEL_0": "Symbolic1",
15
- "LABEL_1": "Numeric1",
16
- "LABEL_2": "Natural1",
17
- "LABEL_3": "Integer1",
18
- "LABEL_4": "Rational1",
19
- "LABEL_5": "Irrational1",
20
- "LABEL_6": "Real1",
21
- "LABEL_7": "Prime1",
22
- "LABEL_8": "Composite1",
23
- "LABEL_9": "Symbolic2",
24
- "LABEL_10": "Numeric2",
25
- "LABEL_11": "Natural2",
26
- "LABEL_12": "Integer2",
27
- "LABEL_13": "Rational2",
28
- "LABEL_14": "Irrational2",
29
- "LABEL_15": "Real2",
30
- "LABEL_16": "Prime2",
31
- "LABEL_17": "Composite2",
32
- "LABEL_18": "Symbolic3",
33
- "LABEL_19": "Numeric3",
34
- "LABEL_20": "Natural3",
35
- "LABEL_21": "Integer3",
36
- "LABEL_22": "Rational3",
37
- "LABEL_23": "Irrational3",
38
- "LABEL_24": "Real3",
39
- "LABEL_25": "Prime3",
40
- "LABEL_26": "Composite3"
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