HOLYBOY commited on
Commit
e638994
1 Parent(s): 6f27d2f
Files changed (2) hide show
  1. app.py +58 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Installing Gradio
2
+ !pip install gradio transformers -q
3
+
4
+ # Import the required Libraries
5
+ import gradio as gr
6
+ import numpy as np
7
+ import pandas as pd
8
+ import pickle
9
+ import transformers
10
+ from transformers import AutoTokenizer
11
+ from transformers import AutoConfig
12
+ from transformers import AutoModelForSequenceClassification
13
+ from transformers import TFAutoModelForSequenceClassification
14
+ from transformers import pipeline
15
+ from scipy.special import softmax
16
+
17
+ # Requirements
18
+ model_path ="HOLYBOY/Sentiment_Analysis_distilBERT"
19
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
20
+ config = AutoConfig.from_pretrained(model_path)
21
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
22
+
23
+ # Preprocess text (username and link placeholders)
24
+ def preprocess(text):
25
+ new_text = []
26
+ for t in text.split(" "):
27
+ t = "@user" if t.startswith("@") and len(t) > 1 else t
28
+ t = "http" if t.startswith("http") else t
29
+ new_text.append(t)
30
+ return " ".join(new_text)
31
+
32
+ # ---- Function to process the input and return prediction
33
+ def sentiment_analysis(text):
34
+ text = preprocess(text)
35
+
36
+ encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
37
+ output = model(**encoded_input)
38
+ scores_ = output[0][0].detach().numpy()
39
+ scores_ = softmax(scores_)
40
+
41
+ # Format output dict of scores
42
+ labels = ["Negative", "Neutral", "Positive"]
43
+ scores = {l:float(s) for (l,s) in zip(labels, scores_) }
44
+
45
+ return scores
46
+
47
+
48
+ # ---- Gradio app interface
49
+ app = gr.Interface(fn = sentiment_analysis,
50
+ inputs = gr.Textbox("Write your text or tweet here..."),
51
+ outputs = "label",
52
+ title = "Sentiment Analysis of Tweets on COVID-19 Vaccines",
53
+ description = "To vaccinate or not? This app analyzes sentiment of text based on tweets tweets about COVID-19 Vaccines using a fine-tuned roBERTA model",
54
+ interpretation = "default",
55
+ examples = [["The idea of a vaccine in record time sure sounds interesting!"]]
56
+ )
57
+
58
+ app.launch()
requirements.txt ADDED
File without changes