File size: 831 Bytes
fb3ac56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40

from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
import gradio as gr

def text_sentiments(text):

  model_name = "distilbert-base-uncased-finetuned-sst-2-english"
  
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
  tokenizer = AutoTokenizer.from_pretrained(model_name)

  classifier = pipeline(task= "sentiment-analysis", model = model, tokenizer = tokenizer)

  result = classifier(text)

  label = result[0]["label"]
  score = result[0]["score"] * 100

  return f"Sentiment is : {label} and Confidence is : {score: 0.2f} %"


gr.Interface(fn = text_sentiments, 
             inputs = gr.inputs.Textbox(label = "Input Text"),
             outputs = gr.outputs.Textbox(),
             title = "Sentiment Classification with Bert",
             ).launch()