HarshAI commited on
Commit
82f4131
·
1 Parent(s): e98c02d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import nltk
3
+ from nltk.sentiment import SentimentIntensityAnalyzer
4
+ import gradio as gr
5
+ from transformers import AutoTokenizer
6
+ from transformers import AutoModelForSequenceClassification
7
+ from scipy.special import softmax
8
+ import nltk
9
+ import matplotlib.pyplot as plt
10
+ nltk.download('vader_lexicon')
11
+ sia = nltk.sentiment.SentimentIntensityAnalyzer()
12
+
13
+
14
+ def sentiment_analyzer(text, model_name):
15
+ positive_score = 0.0
16
+ negative_score = 0.0
17
+ if model_name == "Vader":
18
+ sentiment_score = sia.polarity_scores(text)
19
+ positive_score = sentiment_score['pos']
20
+ negative_score = sentiment_score['neg']
21
+ elif model_name == "RoBERTa":
22
+ MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
23
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
24
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL)
25
+ encoded_text = tokenizer(text, return_tensors='pt')
26
+ output = model(**encoded_text)
27
+ scores = output[0][0].detach().numpy()
28
+ scores = softmax(scores)
29
+ scores_dict = {
30
+ 'roberta_neg' : scores[0],
31
+ 'roberta_neu' : scores[1],
32
+ 'roberta_pos' : scores[2]
33
+ }
34
+ positive_score = float(scores[2])
35
+ negative_score = float(scores[0])
36
+
37
+ # print(f"Output positive score: {positive_score}")
38
+ # print(f"Output negative score: {negative_score}")
39
+ return positive_score, negative_score
40
+
41
+ sentiment_analyzer("I am a goog human being","RoBERTa")
42
+
43
+ input_text = gr.inputs.Textbox(lines=15, label="Input Text")
44
+ sentiment_model = gr.inputs.Radio(["Vader", "RoBERTa"], label="Choose Sentiment Analyzer")
45
+
46
+ outputs = [gr.Slider(minimum=0, maximum=1, step=0.01, label="Positive Sentiment"),
47
+ gr.Slider(minimum=0, maximum=1, step=0.01, label="Negative Sentiment")]
48
+
49
+ iface = gr.Interface(fn=sentiment_analyzer, inputs=[input_text, sentiment_model], outputs=outputs)
50
+
51
+ print(time.time())
52
+ iface.launch(inline=False)