jacky.liu
commited on
Commit
•
8f163d4
1
Parent(s):
e388217
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from transformers import BertForSequenceClassification, BertTokenizer
|
5 |
+
|
6 |
+
# load model
|
7 |
+
tokenizer = BertTokenizer.from_pretrained("uget/sexual_content_dection")
|
8 |
+
model = BertForSequenceClassification.from_pretrained("uget/sexual_content_dection")
|
9 |
+
|
10 |
+
def predict(text):
|
11 |
+
encoding = tokenizer(text, return_tensors="pt")
|
12 |
+
encoding = {k: v.to(model.device) for k,v in encoding.items()}
|
13 |
+
|
14 |
+
outputs = model(**encoding)
|
15 |
+
probs = torch.sigmoid(outputs.logits)
|
16 |
+
|
17 |
+
predictions = torch.argmax(probs, dim=-1)
|
18 |
+
label_map = {0: "None", 1: "Sexual"}
|
19 |
+
predicted_label = label_map[predictions.item()]
|
20 |
+
print(f"Predictions:{predictions.item()}, Label:{predicted_label}")
|
21 |
+
return {"predictions": predictions.item(), "label": predicted_label}
|
22 |
+
|
23 |
+
def greet(name):
|
24 |
+
print("Hello " + name + "!!")
|
25 |
+
return "Hello " + name + "!!"
|
26 |
+
|
27 |
+
demo = gr.Interface(fn=predict, inputs="text", outputs="text")
|
28 |
+
demo.launch(share=True)
|