Spaces:
Running
Running
update the repo name
Browse files
app.py
CHANGED
@@ -1,43 +1,43 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
-
import torch
|
4 |
-
|
5 |
-
# Load model and tokenizer
|
6 |
-
model_id = "Rerandaka/
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False)
|
8 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_id)
|
9 |
-
|
10 |
-
# Class mapping (optional — edit as needed)
|
11 |
-
label_map = {
|
12 |
-
0: "Safe / Normal",
|
13 |
-
1: "Inappropriate / Unsafe"
|
14 |
-
}
|
15 |
-
|
16 |
-
# Inference function
|
17 |
-
def classify_text(text: str):
|
18 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
|
19 |
-
with torch.no_grad():
|
20 |
-
outputs = model(**inputs)
|
21 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
22 |
-
predicted = torch.argmax(probs, dim=1).item()
|
23 |
-
confidence = probs[0][predicted].item()
|
24 |
-
return {
|
25 |
-
"label": label_map.get(predicted, str(predicted)),
|
26 |
-
"confidence": round(confidence, 4)
|
27 |
-
}
|
28 |
-
|
29 |
-
# Define Gradio Interface
|
30 |
-
demo = gr.Interface(
|
31 |
-
fn=classify_text,
|
32 |
-
inputs=gr.Textbox(label="Enter text to classify"),
|
33 |
-
outputs=[
|
34 |
-
gr.Textbox(label="Predicted Label"),
|
35 |
-
gr.Textbox(label="Confidence")
|
36 |
-
],
|
37 |
-
title="Child-Safety Text Classifier",
|
38 |
-
description="This model detects if text content is unsafe or inappropriate for children.",
|
39 |
-
allow_flagging="never"
|
40 |
-
)
|
41 |
-
|
42 |
-
# Expose API endpoint explicitly
|
43 |
-
demo.launch(api_name="predict")
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load model and tokenizer
|
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 |
+
# Class mapping (optional — edit as needed)
|
11 |
+
label_map = {
|
12 |
+
0: "Safe / Normal",
|
13 |
+
1: "Inappropriate / Unsafe"
|
14 |
+
}
|
15 |
+
|
16 |
+
# Inference function
|
17 |
+
def classify_text(text: str):
|
18 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model(**inputs)
|
21 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
22 |
+
predicted = torch.argmax(probs, dim=1).item()
|
23 |
+
confidence = probs[0][predicted].item()
|
24 |
+
return {
|
25 |
+
"label": label_map.get(predicted, str(predicted)),
|
26 |
+
"confidence": round(confidence, 4)
|
27 |
+
}
|
28 |
+
|
29 |
+
# Define Gradio Interface
|
30 |
+
demo = gr.Interface(
|
31 |
+
fn=classify_text,
|
32 |
+
inputs=gr.Textbox(label="Enter text to classify"),
|
33 |
+
outputs=[
|
34 |
+
gr.Textbox(label="Predicted Label"),
|
35 |
+
gr.Textbox(label="Confidence")
|
36 |
+
],
|
37 |
+
title="Child-Safety Text Classifier",
|
38 |
+
description="This model detects if text content is unsafe or inappropriate for children.",
|
39 |
+
allow_flagging="never"
|
40 |
+
)
|
41 |
+
|
42 |
+
# Expose API endpoint explicitly
|
43 |
+
demo.launch(api_name="predict")
|