File size: 2,004 Bytes
bd9e726
 
 
 
 
 
 
421226a
c6291e6
 
bd9e726
 
27981fd
c6291e6
bd9e726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421226a
bd9e726
 
 
 
 
 
10bc06b
bd9e726
 
4812b3b
aab5119
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
from scipy.special import softmax
from transformers import AutoTokenizer, AutoConfig
from transformers import AutoModelForSequenceClassification
import numpy as np

#Setup

model_path = f"bambadij/Tweet_sentiment_analysis_Distilbert"
tokenizer = AutoTokenizer.from_pretrained(model_path) 
config = AutoConfig.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)


#Function

# Preprocess text (username and link placeholders)
def preprocess(text):
    new_text = []
    for t in text.split(" "):
        t = '@user' if t.startswith('@') and len(t) > 1 else t
        t = 'http' if t.startswith('http') else t
        new_text.append(t)
    return " ".join(new_text)

# Input preprocessing
text = "Covid cases are increasing fast!"
text = preprocess(text)

# PyTorch-based models
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
scores = output[0][0].detach().numpy()
scores = softmax(scores)

def sentiment_analysis(text):
    text =preprocess(text)
    #Pytorch-based models
    encoded_input = tokenizer(text,return_tensors='pt')
    output = model(**encoded_input)
    scores_ = output[0][0].detach().numpy()
    scores_ =softmax(scores_)

    #Foramt ouptput dict of scores
    labels =['Negative','Neutral','Positive']
    scores = {l:float(s) for (l,s) in zip(labels,scores_)}
    return scores

demo = gr.Interface(
    fn=sentiment_analysis,
    inputs=gr.Textbox(placeholder="Copy and paste /Write a tweet her..."),
    outputs="label",
    examples=[["what's up with the vaccine"],
              ["Covid cases are increasing fast!"],
              ["Covid has been invented by Issa"],
              ["I have a covid"],
              ["All the people are sick maybe it's covid"],
              ],
    title="Covid 19 vaccin Sentiment Analysis App",
    description = "This Aplication assesses if a twitter post relating vaccination is positive"
  )
if __name__ == "__main__":
    demo.launch()