File size: 1,106 Bytes
1ca8294
93bbccc
1ca8294
 
 
 
 
 
 
 
 
 
 
 
cc40b10
1ca8294
 
 
 
 
 
 
a1cb754
eb10631
 
871c34a
 
a8992e5
 
5228e84
 
1ca8294
4e1e67e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
import pandas as pd
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

labels = ['Not_Adult', 'Adult']
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device

model_name = 'valurank/finetuned-distilbert-adult-content-detection'
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)


def get_adult_content(text):
  input_tensor = tokenizer.encode(text, return_tensors='pt', truncation=True)
  logits = model(input_tensor).logits

  softmax = torch.nn.Softmax(dim=1)
  probs = softmax(logits)[0]
  probs = probs.cpu().detach().numpy()
  
  #max_index = np.argmax(probs)
  adult_content = f"{labels[0]} : {round(probs[0]*100,2)}  {labels[1]} : {round(probs[1]*100,2)}"
  return adult_content

demo = gr.Interface(get_adult_content, inputs = gr.Textbox(label= "Input your text here"),
                    outputs = gr.Textbox(label='Category'))
                    
                    
if __name__ == "__main__":
  demo.launch(debug=True)