File size: 2,204 Bytes
acdfef5
e26aa98
 
 
 
1c790d0
e26aa98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c790d0
 
e26aa98
1c790d0
 
 
 
 
 
 
e26aa98
 
1c790d0
2deff65
59b4b59
 
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
from transformers import AutoTokenizer
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer

# Specifying the model path, which points to the Hugging Face Model Hub
model_path = f'Mbabazi/cardiffnlp_twitter_roberta_base_sentiment_latest_Nov2023'
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)

# Function to predict sentiment of a given tweet
def predict_tweet(tweet):
    # Tokenize the input tweet using the specified tokenizer
    inputs = tokenizer(tweet, return_tensors="pt", padding=True, truncation=True, max_length=128)
    
    # Passing the tokenized input through the pre-trained sentiment analysis model
    outputs = model(**inputs)
    
    # Applying softmax to obtain probabilities for each sentiment class
    probs = outputs.logits.softmax(dim=-1)
    
    # Defining sentiment classes
    sentiment_classes = ['Negative', 'Neutral', 'Positive']
    
    # Creating a dictionary with sentiment classes as keys and their corresponding probabilities as values
    return {sentiment_classes[i]: float(probs.squeeze()[i]) for i in range(len(sentiment_classes))}


# Create a Gradio Interface for the tweet sentiment prediction function
iface = gr.Interface(
    fn=predict_tweet, # Set the prediction function
    inputs="text",    # Specify input type as text
    outputs="label",  # Specify output type as label
    title="Vaccine Sentiment Classifier", # Set the title of the interface
    description="Enter a text about vaccines to determine if the sentiment is negative, neutral, or positive.", # Provide a brief description
    examples=[
        ["Vaccinations have been a game-changer in public health, significantly reducing the incidence of many dangerous diseases and saving countless lives."],
        ["Vaccinations are a medical intervention that introduces a vaccine to stimulate an individual’s immune response against a particular disease."],
        ["Vaccines are rushed to the market without proper testing and are pushed by corporations that value profits over the well-being of the public."]
    ]
)


iface.launch()

# with gr.Blocks() as demo: