mbabazif commited on
Commit
e26aa98
1 Parent(s): 16fa7c0

Add application file

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForSequenceClassification
2
+ from transformers import TFAutoModelForSequenceClassification
3
+ from transformers import AutoTokenizer, AutoConfig
4
+ import numpy as np
5
+ import gradio as gr
6
+
7
+
8
+
9
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
10
+
11
+ # Specifying the model path, which points to the Hugging Face Model Hub
12
+ model_path = f'Mbabazi/twitter-roberta-base-sentiment-latest'
13
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
14
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
15
+
16
+
17
+ # Function to predict sentiment of a given tweet
18
+ def predict_tweet(tweet):
19
+ # Tokenize the input tweet using the specified tokenizer
20
+ inputs = tokenizer(tweet, return_tensors="pt", padding=True, truncation=True, max_length=128)
21
+
22
+ # Passing the tokenized input through the pre-trained sentiment analysis model
23
+ outputs = model(**inputs)
24
+
25
+ # Applying softmax to obtain probabilities for each sentiment class
26
+ probs = outputs.logits.softmax(dim=-1)
27
+
28
+ # Defining sentiment classes
29
+ sentiment_classes = ['Negative', 'Neutral', 'Positive']
30
+
31
+ # Creating a dictionary with sentiment classes as keys and their corresponding probabilities as values
32
+ return {sentiment_classes[i]: float(probs.squeeze()[i]) for i in range(len(sentiment_classes))}
33
+
34
+
35
+ # Create a Gradio Interface for the tweet sentiment prediction function
36
+ iface = gr.Interface(
37
+ fn=predict_tweet, # Set the prediction function
38
+ inputs="text", # Specify input type as text
39
+ outputs="label", # Specify output type as label
40
+ title="Tweet Sentiment Classifier", # Set the title of the interface
41
+ description="Enter a tweet to determine if the sentiment is negative, neutral, or positive." # Provide a brief description
42
+ )
43
+
44
+ iface.launch()