flokabukie commited on
Commit
ffc29e2
1 Parent(s): aa6e86e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import the required Libraries
2
+ import gradio as gr
3
+ import numpy as np
4
+ import transformers
5
+ from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification, TFAutoModelForSequenceClassification
6
+ from scipy.special import softmax
7
+
8
+
9
+ # Requirements
10
+ model_path = "flokabukie/Finetuned-Distilbert-base-model"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
12
+ config = AutoConfig.from_pretrained(model_path)
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
14
+
15
+ # Preprocess text (username and link placeholders)
16
+ def preprocess(text):
17
+ new_text = []
18
+ for t in text.split(" "):
19
+ t = "@user" if t.startswith("@") and len(t) > 1 else t
20
+ t = "http" if t.startswith("http") else t
21
+ new_text.append(t)
22
+ return " ".join(new_text)
23
+
24
+ #Function to process the input and return prediction
25
+ def sentiment_analysis(text):
26
+ text = preprocess(text)
27
+
28
+ encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
29
+ output = model(**encoded_input)
30
+ scores_ = output[0][0].detach().numpy()
31
+ scores_ = softmax(scores_)
32
+
33
+ #Output of scores by converting a list of labels and scores into a dictionary format
34
+ labels = ["Negative", "Neutral", "Positive"]
35
+ scores = {l:float(s) for (l,s) in zip(labels, scores_) }
36
+ return scores
37
+ #App interface with gradio
38
+ app = gr.Interface(fn = sentiment_analysis,
39
+ inputs = gr.Textbox("Write your text or tweet here..."),
40
+ outputs = "label",
41
+ title = "Sentiment Analysis of Tweets on COVID-19 Vaccines",
42
+ description = "This app analyzes sentiment of text based on tweets about COVID-19 Vaccines using a fine-tuned DistilBERT model",
43
+ interpretation = "default"
44
+ )
45
+
46
+ app.launch()