Commit
·
f476dc8
1
Parent(s):
a8ad291
Upload 2 files
Browse files- app.py +60 -0
- requirements.txt +72 -0
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.system('pip install gradio')
|
3 |
+
os.system('pip install scipy')
|
4 |
+
|
5 |
+
# pip install transformers
|
6 |
+
# pip install gradio
|
7 |
+
# pip install torch
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
from transformers import AutoModelForSequenceClassification
|
12 |
+
from transformers import TFAutoModelForSequenceClassification
|
13 |
+
from transformers import AutoTokenizer, AutoConfig
|
14 |
+
import numpy as np
|
15 |
+
from scipy.special import softmax
|
16 |
+
import gradio as gr
|
17 |
+
import torch
|
18 |
+
|
19 |
+
|
20 |
+
# Requirements
|
21 |
+
model_path = f"MaryanneMuchai/twitter-finetuned-model"
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
23 |
+
config = AutoConfig.from_pretrained(model_path)
|
24 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
25 |
+
|
26 |
+
# Preprocess text (username and link placeholders)
|
27 |
+
def preprocess(text):
|
28 |
+
new_text = []
|
29 |
+
for t in text.split(" "):
|
30 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
31 |
+
t = 'http' if t.startswith('http') else t
|
32 |
+
new_text.append(t)
|
33 |
+
return " ".join(new_text)
|
34 |
+
|
35 |
+
|
36 |
+
def sentiment_analysis(text):
|
37 |
+
text = preprocess(text)
|
38 |
+
|
39 |
+
# PyTorch-based models
|
40 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
41 |
+
output = model(**encoded_input)
|
42 |
+
scores_ = output[0][0].detach().numpy()
|
43 |
+
scores_ = softmax(scores_)
|
44 |
+
|
45 |
+
# Format output dict of scores
|
46 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
47 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
48 |
+
|
49 |
+
return scores
|
50 |
+
|
51 |
+
demo = gr.Interface(
|
52 |
+
fn=sentiment_analysis,
|
53 |
+
inputs=gr.Textbox(placeholder="Write your tweet here..."),
|
54 |
+
outputs="label",
|
55 |
+
interpretation="default",
|
56 |
+
examples=[["This is wonderful!"]],
|
57 |
+
title="Twitter Sentiment Analysis with Gradio",
|
58 |
+
enable_queue=True)
|
59 |
+
|
60 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.1.0
|
2 |
+
aiohttp==3.8.4
|
3 |
+
aiosignal==1.3.1
|
4 |
+
altair==5.0.0
|
5 |
+
anyio==3.6.2
|
6 |
+
async-timeout==4.0.2
|
7 |
+
attrs==23.1.0
|
8 |
+
certifi==2023.5.7
|
9 |
+
charset-normalizer==3.1.0
|
10 |
+
click==8.1.3
|
11 |
+
colorama==0.4.6
|
12 |
+
contourpy==1.0.7
|
13 |
+
cycler==0.11.0
|
14 |
+
fastapi==0.95.1
|
15 |
+
ffmpy==0.3.0
|
16 |
+
filelock==3.12.0
|
17 |
+
fonttools==4.39.4
|
18 |
+
frozenlist==1.3.3
|
19 |
+
fsspec==2023.5.0
|
20 |
+
gradio==3.30.0
|
21 |
+
gradio_client==0.2.4
|
22 |
+
h11==0.14.0
|
23 |
+
httpcore==0.17.0
|
24 |
+
httpx==0.24.0
|
25 |
+
huggingface-hub==0.14.1
|
26 |
+
idna==3.4
|
27 |
+
Jinja2==3.1.2
|
28 |
+
jsonschema==4.17.3
|
29 |
+
kiwisolver==1.4.4
|
30 |
+
linkify-it-py==2.0.2
|
31 |
+
markdown-it-py==2.2.0
|
32 |
+
MarkupSafe==2.1.2
|
33 |
+
matplotlib==3.7.1
|
34 |
+
mdit-py-plugins==0.3.3
|
35 |
+
mdurl==0.1.2
|
36 |
+
mpmath==1.3.0
|
37 |
+
multidict==6.0.4
|
38 |
+
networkx==3.1
|
39 |
+
numpy==1.24.3
|
40 |
+
orjson==3.8.12
|
41 |
+
packaging==23.1
|
42 |
+
pandas==2.0.1
|
43 |
+
Pillow==9.5.0
|
44 |
+
pydantic==1.10.7
|
45 |
+
pydub==0.25.1
|
46 |
+
Pygments==2.15.1
|
47 |
+
pyparsing==3.0.9
|
48 |
+
pyrsistent==0.19.3
|
49 |
+
python-dateutil==2.8.2
|
50 |
+
python-multipart==0.0.6
|
51 |
+
pytz==2023.3
|
52 |
+
PyYAML==6.0
|
53 |
+
regex==2023.5.5
|
54 |
+
requests==2.30.0
|
55 |
+
semantic-version==2.10.0
|
56 |
+
six==1.16.0
|
57 |
+
sniffio==1.3.0
|
58 |
+
starlette==0.26.1
|
59 |
+
sympy==1.12
|
60 |
+
scipy
|
61 |
+
tokenizers==0.13.3
|
62 |
+
toolz==0.12.0
|
63 |
+
torch==2.0.1
|
64 |
+
tqdm==4.65.0
|
65 |
+
transformers==4.29.1
|
66 |
+
typing_extensions==4.5.0
|
67 |
+
tzdata==2023.3
|
68 |
+
uc-micro-py==1.0.2
|
69 |
+
urllib3==2.0.2
|
70 |
+
uvicorn==0.22.0
|
71 |
+
websockets==11.0.3
|
72 |
+
yarl==1.9.2
|