Spaces:
Sleeping
Sleeping
Duplicate from bright1/sentiment-analysis-app-gradio
Browse filesCo-authored-by: Bright Eshun <[email protected]>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +77 -0
- requirements.txt +13 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Sentiment Analysis App Gradio
|
3 |
+
emoji: 📉
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: gray
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.28.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: bright1/sentiment-analysis-app-gradio
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification
|
5 |
+
from scipy.special import softmax
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Requirements
|
9 |
+
def load_distilbert():
|
10 |
+
model_path = "bright1/fine-tuned-distilbert-base-uncased"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
12 |
+
# config = AutoConfig.from_pretrained(model_path)
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
14 |
+
return model, tokenizer
|
15 |
+
|
16 |
+
def load_roberta():
|
17 |
+
model_path = "bright1/fine-tuned-twitter-Roberta-base-sentiment"
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
19 |
+
# config = AutoConfig.from_pretrained(model_path)
|
20 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
21 |
+
return model, tokenizer
|
22 |
+
|
23 |
+
# def check_csv(csv_file, data):
|
24 |
+
# if os.path.isfile(csv_file):
|
25 |
+
# data.to_csv(csv_file, mode='a', header=False, index=False, encoding='utf-8')
|
26 |
+
# else:
|
27 |
+
# history = data.copy()
|
28 |
+
# history.to_csv(csv_file, index=False)
|
29 |
+
|
30 |
+
#Preprocess text
|
31 |
+
def preprocess(text):
|
32 |
+
new_text = []
|
33 |
+
for t in text.split(" "):
|
34 |
+
t = "@user" if t.startswith("@") and len(t) > 1 else t
|
35 |
+
t = "http" if t.startswith("http") else t
|
36 |
+
print(t)
|
37 |
+
new_text.append(t)
|
38 |
+
print(new_text)
|
39 |
+
|
40 |
+
return " ".join(new_text)
|
41 |
+
|
42 |
+
#Process the input and return prediction
|
43 |
+
def sentiment_analysis(model_type, text):
|
44 |
+
if model_type== 'distilbert':
|
45 |
+
model, tokenizer = load_distilbert()
|
46 |
+
else:
|
47 |
+
model, tokenizer = load_roberta()
|
48 |
+
save_text = {'tweet': text}
|
49 |
+
text = preprocess(text)
|
50 |
+
encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
|
51 |
+
output = model(**encoded_input)
|
52 |
+
scores_ = output[0][0].detach().numpy()
|
53 |
+
scores_ = softmax(scores_)
|
54 |
+
|
55 |
+
# Format output dict of scores
|
56 |
+
labels = ["Negative", "Neutral", "Positive"]
|
57 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
58 |
+
# save_text.update(scores)
|
59 |
+
# user_data = {key: [value] for key,value in save_text.items()}
|
60 |
+
# data = pd.DataFrame(user_data,)
|
61 |
+
# check_csv('history.csv', data)
|
62 |
+
# hist_df = pd.read_csv('history.csv')
|
63 |
+
return scores
|
64 |
+
# , hist_df.head()
|
65 |
+
|
66 |
+
model_type = gr.Radio(choices=['distilbert', 'roberta'], label='Select model type', value='roberta' )
|
67 |
+
#Gradio app interface
|
68 |
+
#Gradio app interface
|
69 |
+
demo = gr.Interface(fn = sentiment_analysis,
|
70 |
+
inputs = [model_type, gr.TextArea("Write your text or tweet here", label="Analyze your COVID-19 tweets" )],
|
71 |
+
outputs = ["label"],
|
72 |
+
title = "COVID-19 Vaccine Tweet Analyzer App",
|
73 |
+
description = "COVID-19 Tweets Analyzer",
|
74 |
+
interpretation = "default",
|
75 |
+
examples = [["roberta", "Being vaccinated is actually awesome :)"]]
|
76 |
+
).launch()
|
77 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
nltk
|
2 |
+
torch
|
3 |
+
gradio
|
4 |
+
datasets==2.12.0
|
5 |
+
numpy==1.22.4
|
6 |
+
pandas==1.5.3
|
7 |
+
scikit-learn==1.2.2
|
8 |
+
transformers==4.28.1
|
9 |
+
wandb==0.15.0
|
10 |
+
IPython==7.34.0
|
11 |
+
jupyter_client==6.1.12
|
12 |
+
jupyter_core==5.3.0
|
13 |
+
notebook==6.4.8
|