IsaacSarps commited on
Commit
f52846d
·
1 Parent(s): 4725b18

create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import pickle
5
+ from scipy.special import softmax
6
+ # import transfomers
7
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
8
+ # from transformers import AutoModelForSequenceClassification
9
+ # from transformers import TFAutoModelForSequenceClassification
10
+ # from transformers import AutoTokenizer, AutoConfig
11
+
12
+ # Requirements
13
+ model_path = "IsaacSarps/sentiment_analysis"
14
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
15
+ config = AutoConfig.from_pretrained(model_path)
16
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
17
+
18
+
19
+ # Preprocess text (username and link placeholders)
20
+ def preprocess(text):
21
+ new_text = []
22
+ for t in text.split(" "):
23
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
24
+ t = 'http' if t.startswith('http') else t
25
+ new_text.append(t)
26
+ return " ".join(new_text)
27
+
28
+
29
+ def sent_analysis(text):
30
+ text = preprocess(text)
31
+
32
+ # PyTorch-based models
33
+ encoded_input = tokenizer(text, return_tensors='pt')
34
+ output = model(**encoded_input)
35
+ scores_ = output[0][0].detach().numpy()
36
+ scores_ = softmax(scores_)
37
+
38
+ # Format output dict of scores
39
+ labels = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
40
+ scores = {labels[i]: float(s) for i, s in enumerate(scores_)}
41
+ return scores
42
+
43
+ demo = gr.Interface(
44
+ fn=sent_analysis,
45
+ inputs=gr.Textbox(placeholder="Share your thoughts on COVID vaccines..."),
46
+ outputs="label",
47
+ interpretation="default",
48
+ examples=[["I feel confident about covid vaccines."]],
49
+ title="COVID Vaccine Sentiment Analysis",
50
+ description="An AI model that predicts sentiment about COVID vaccines, providing labels and probabilities for 'NEGATIVE', 'NEUTRAL', and 'POSITIVE' sentiments.",
51
+ theme="default",
52
+ live=True
53
+ )
54
+
55
+ demo.launch()