abdulmatinomotoso commited on
Commit
1ca8294
1 Parent(s): d22f753

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+
6
+ labels = ['Not_Adult', 'Adult']
7
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8
+ device
9
+
10
+ model_name = 'valurank/finetuned-distilbert-adult-content-detection'
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+
14
+ def get_adult_content(text):
15
+ input_tensor = tokenizer.encode(text, return_tensors='pt', truncation=True)
16
+ logits = model(input_tensor).logits
17
+
18
+ softmax = torch.nn.Softmax(dim=1)
19
+ probs = softmax(logits)[0]
20
+ probs = probs.cpu().detach().numpy()
21
+ max_index = np.argmax(probs)
22
+ adult_content = labels[max_index]
23
+ return adult_content
24
+
25
+ demo = gr.Interface(get_adult_content, inputs = gr.inputs.Textbox(label= "Input your text here"),
26
+ outputs = gr.outputs.Textbox(label='Category'))
27
+
28
+ if __name__ == "__main__":
29
+ demo.launch(debug=True)