Spaces:
Runtime error
Runtime error
Gradio_App: Initial commit
Browse files- .gitignore +3 -0
- app.py +56 -0
- requirements.txt +6 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
*venv/
|
2 |
+
flagged/
|
3 |
+
__pycache__/
|
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForSequenceClassification
|
4 |
+
from transformers import TFAutoModelForSequenceClassification
|
5 |
+
from transformers import AutoTokenizer, AutoConfig
|
6 |
+
from scipy.special import softmax
|
7 |
+
|
8 |
+
#setup
|
9 |
+
model_path = "KAITANY/finetuned-roberta-base-sentiment"
|
10 |
+
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
12 |
+
#config = AutoConfig.from_pretrained(model_path)
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
14 |
+
|
15 |
+
def preprocess(text):
|
16 |
+
# Preprocess text (username and link placeholders)
|
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 |
+
def sentiment_analysis(text):
|
25 |
+
text = preprocess(text)
|
26 |
+
|
27 |
+
# Tokenize the text
|
28 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
29 |
+
|
30 |
+
# Make a prediction
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = model(**inputs)
|
33 |
+
|
34 |
+
# Get the predicted class probabilities
|
35 |
+
scores = torch.softmax(outputs.logits, dim=1).tolist()[0]
|
36 |
+
|
37 |
+
# Map the scores to labels
|
38 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
39 |
+
scores_dict = {label: score for label, score in zip(labels, scores)}
|
40 |
+
|
41 |
+
return scores_dict
|
42 |
+
|
43 |
+
title = "Sentiment Analysis Application\n\n\nThis application assesses if a twitter post relating to vaccination is positive,neutral or negative"
|
44 |
+
|
45 |
+
|
46 |
+
demo = gr.Interface(
|
47 |
+
fn=sentiment_analysis,
|
48 |
+
inputs=gr.Textbox(placeholder="Write your tweet here..."),
|
49 |
+
outputs=gr.Label(num_top_classes=3),
|
50 |
+
examples=[["The Vaccine is harmful!"],["I cant believe people don't vaccinate their kids"],["FDA think just not worth the AE unfortunately"],["For a vaccine given to healthy"]],
|
51 |
+
title=title
|
52 |
+
)
|
53 |
+
|
54 |
+
demo.launch(share=True)
|
55 |
+
|
56 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.1.1
|
2 |
+
transformers==4.35.0
|
3 |
+
scikit-learn==1.2.2
|
4 |
+
scipy==1.11.3
|
5 |
+
black
|
6 |
+
torch==2.1.0
|