gr8testgad-1 commited on
Commit
53d42f8
·
1 Parent(s): 46d6462

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import pickle
4
+ from scipy.special import softmax
5
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
6
+
7
+
8
+ # Requirements
9
+ model_path = "gr8testgad-1/sentiment_analysis"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
11
+ config = AutoConfig.from_pretrained(model_path)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
13
+
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
+
25
+ def sent_analysis(text):
26
+ text = preprocess(text)
27
+
28
+ # PyTorch-based models
29
+ encoded_input = tokenizer(text, return_tensors='pt')
30
+ output = model(**encoded_input)
31
+ scores_ = output[0][0].detach().numpy()
32
+ scores_ = softmax(scores_)
33
+
34
+ # Format output dict of scores
35
+ labels = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
36
+ scores = {labels[i]: float(s) for i, s in enumerate(scores_)}
37
+ return scores
38
+
39
+ demo = gr.Interface(
40
+ fn=sent_analysis,
41
+ inputs=gr.Textbox(placeholder="Share your thoughts on COVID vaccines..."),
42
+ outputs="label",
43
+ interpretation="default",
44
+ examples=[
45
+ ["I feel confident about covid vaccines"],
46
+ ["Will you take the jab"],
47
+ ["I like the covid vaccines"],
48
+ ["The covid vaccines are effective"]
49
+ ],
50
+ title="COVID Vaccine Sentiment Analysis",
51
+ description="An AI model that predicts sentiment about COVID vaccines, providing labels and probabilities for 'NEGATIVE', 'NEUTRAL', and 'POSITIVE' sentiments.",
52
+ theme="default",
53
+ live=True
54
+ )
55
+
56
+ demo.launch()
57
+