Spaces:
Sleeping
Sleeping
update for API
Browse files
app.py
CHANGED
@@ -2,32 +2,27 @@ import gradio as gr
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
import torch
|
4 |
|
|
|
5 |
model_id = "Rerandaka/Cild_safety_bigbird"
|
6 |
-
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False)
|
8 |
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
}
|
14 |
-
|
15 |
-
def classify_text(text: str):
|
16 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
|
17 |
with torch.no_grad():
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
confidence = probs[0][predicted].item()
|
22 |
-
return f"{label_map.get(predicted, predicted)} (Confidence: {confidence:.2f})"
|
23 |
|
|
|
24 |
demo = gr.Interface(
|
25 |
-
fn=
|
26 |
-
inputs=gr.Textbox(label="
|
27 |
outputs=gr.Textbox(label="Prediction"),
|
28 |
title="Child-Safety Text Classifier",
|
29 |
-
description="This model detects unsafe or inappropriate text for children."
|
30 |
-
flagging_mode="never"
|
31 |
)
|
32 |
|
33 |
-
|
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
import torch
|
4 |
|
5 |
+
# Load model
|
6 |
model_id = "Rerandaka/Cild_safety_bigbird"
|
|
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False)
|
8 |
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
9 |
|
10 |
+
# Inference function
|
11 |
+
def classify(text):
|
12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
|
|
|
|
|
|
|
|
13 |
with torch.no_grad():
|
14 |
+
logits = model(**inputs).logits
|
15 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
16 |
+
return str(predicted_class)
|
|
|
|
|
17 |
|
18 |
+
# Gradio interface with a named API
|
19 |
demo = gr.Interface(
|
20 |
+
fn=classify,
|
21 |
+
inputs=gr.Textbox(label="Text Input"),
|
22 |
outputs=gr.Textbox(label="Prediction"),
|
23 |
title="Child-Safety Text Classifier",
|
24 |
+
description="This model detects unsafe or inappropriate text for children."
|
|
|
25 |
)
|
26 |
|
27 |
+
# ✅ Launch with API endpoint
|
28 |
+
demo.launch(api_name="predict")
|