Spaces:
Running
Running
danwath weda krpn
Browse files
app.py
CHANGED
@@ -1,28 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
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 |
-
#
|
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 |
-
|
16 |
-
return
|
17 |
|
18 |
-
# API-
|
19 |
demo = gr.Interface(
|
20 |
fn=classify,
|
21 |
-
inputs=gr.Textbox(label="Enter
|
22 |
-
outputs=gr.
|
23 |
-
api_name="/classify"
|
24 |
)
|
25 |
|
26 |
-
# β
Enable API and queue
|
27 |
demo.queue()
|
28 |
demo.launch(show_api=True)
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
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 |
+
# Classification 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 |
+
prediction = torch.argmax(logits, dim=1).item()
|
16 |
+
return prediction # 0 = safe, 1 = unsafe
|
17 |
|
18 |
+
# β
API-compatible Interface with explicit name
|
19 |
demo = gr.Interface(
|
20 |
fn=classify,
|
21 |
+
inputs=gr.Textbox(label="Enter paragraph..."),
|
22 |
+
outputs=gr.Number(label="Prediction (0=safe, 1=unsafe)"),
|
23 |
+
api_name="/classify" # π₯ This only works in gr.Interface (not Blocks)
|
24 |
)
|
25 |
|
|
|
26 |
demo.queue()
|
27 |
demo.launch(show_api=True)
|