Spaces:
Sleeping
Sleeping
Idhamm15 commited on
Commit ·
780562d
1
Parent(s): c91b86f
updates
Browse files- ml-service/Dockerfile +8 -0
- ml-service/app.py +33 -0
- ml-service/classifier.py +22 -0
- ml-service/docker-compose.yml +7 -0
- ml-service/download_model.ipynb +44 -0
- ml-service/model.py +16 -0
- ml-service/nlp.py +8 -0
- requirements.txt +155 -0
ml-service/Dockerfile
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10.8-slim
|
| 2 |
+
LABEL description="Sentiment classifier of tweets service"
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY requirements.txt /app/requirements.txt
|
| 5 |
+
RUN pip install -r requirements.txt
|
| 6 |
+
COPY . /app/
|
| 7 |
+
EXPOSE 6000
|
| 8 |
+
CMD ["python", "app.py"]
|
ml-service/app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from fastapi import FastAPI, APIRouter
|
| 3 |
+
import uvicorn
|
| 4 |
+
from classifier import Classifier
|
| 5 |
+
from model import Model
|
| 6 |
+
from nlp import NLP
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level = logging.INFO)
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
nlp = NLP()
|
| 13 |
+
router = APIRouter()
|
| 14 |
+
classifier = Classifier()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@router.get("/")
|
| 18 |
+
async def home():
|
| 19 |
+
return {"message": "Machine Learning service"}
|
| 20 |
+
|
| 21 |
+
@router.post("/sentiment")
|
| 22 |
+
async def data(data: dict):
|
| 23 |
+
try:
|
| 24 |
+
input_text = data["text"]
|
| 25 |
+
res = nlp.sentiment_analysis(classifier, input_text)
|
| 26 |
+
return res
|
| 27 |
+
except Exception as e:
|
| 28 |
+
log.error("Something went wrong")
|
| 29 |
+
|
| 30 |
+
app.include_router(router)
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
uvicorn.run("app:app", reload=True, port=6000, host="0.0.0.0)
|
ml-service/classifier.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# classifier.py
|
| 2 |
+
from scipy.special import softmax
|
| 3 |
+
from model import Model
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
class Classifier:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.model = Model.load_model()
|
| 9 |
+
self.tokenizer = Model.load_tokenizer()
|
| 10 |
+
|
| 11 |
+
def get_sentiment_label_and_score(self, text: str):
|
| 12 |
+
result = {}
|
| 13 |
+
labels = ["Negative", "Neutral", "Positive"]
|
| 14 |
+
encoded_input = self.tokenizer(text, return_tensors='pt')
|
| 15 |
+
output = self.model(**encoded_input)
|
| 16 |
+
scores = output[0][0]jj.detach().numpy()
|
| 17 |
+
scores = softmax(scores)
|
| 18 |
+
ranking = np.argsort(scores)
|
| 19 |
+
ranking = ranking[::-1]
|
| 20 |
+
result["label"] = str(labels[ranking[0]])
|
| 21 |
+
result["score"] = np.round(float(scores[ranking[0]]), 4)
|
| 22 |
+
return result
|
ml-service/docker-compose.yml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: '3
|
| 2 |
+
|
| 3 |
+
services:
|
| 4 |
+
ml-service:
|
| 5 |
+
build: ./ml-service
|
| 6 |
+
ports:
|
| 7 |
+
- 6000:6000
|
ml-service/download_model.ipynb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from transformers import AutoModelForSequenceClassification, TFAutoModelForSequenceClassification, AutoTokenize"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": null,
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"# download the model\n",
|
| 19 |
+
"MODEL = \"cardiffnlp/twitter-roberta-base-sentiment\"\n",
|
| 20 |
+
"tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
|
| 21 |
+
"model = AutoModelForSequenceClassification.from_pretrained(MODEL)"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": null,
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"outputs": [],
|
| 29 |
+
"source": [
|
| 30 |
+
"# save the model\n",
|
| 31 |
+
"save_dir = \"ml-service/models/roberta-base\"\n",
|
| 32 |
+
"tokenizer.save_pretrained(save_dir)\n",
|
| 33 |
+
"model.save_pretrained(save_dir)"
|
| 34 |
+
]
|
| 35 |
+
}
|
| 36 |
+
],
|
| 37 |
+
"metadata": {
|
| 38 |
+
"language_info": {
|
| 39 |
+
"name": "python"
|
| 40 |
+
}
|
| 41 |
+
},
|
| 42 |
+
"nbformat": 4,
|
| 43 |
+
"nbformat_minor": 2
|
| 44 |
+
}
|
ml-service/model.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# model.py
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, TFAutoModelForSequenceClassification, AutoTokenize
|
| 3 |
+
|
| 4 |
+
class Model:
|
| 5 |
+
"""A model class to lead the model and tokenizer"""
|
| 6 |
+
|
| 7 |
+
def __init__(self) -> None:
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
def load_model():
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained("./models/roberta-base/")
|
| 12 |
+
return model
|
| 13 |
+
|
| 14 |
+
def load_tokenizer():
|
| 15 |
+
tokenizer = AutoTokenize.from_pretrained("./models/roberta-base/")
|
| 16 |
+
return tokenizer
|
ml-service/nlp.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# nlp.py
|
| 2 |
+
from classifier import Classifier
|
| 3 |
+
|
| 4 |
+
classifier = Classifier()
|
| 5 |
+
|
| 6 |
+
def sentiment_analysis(self, text:str, classifier:
|
| 7 |
+
sentiment = classifier.get_sentiment_label_and_score(text)
|
| 8 |
+
return sentiment
|
requirements.txt
ADDED
|
@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-common==1.1.28
|
| 5 |
+
azure-core==1.30.1
|
| 6 |
+
azure-identity==1.15.0
|
| 7 |
+
azure-mgmt-core==1.4.0
|
| 8 |
+
azure-mgmt-rdbms==10.1.0
|
| 9 |
+
azure-mgmt-resource==23.0.1
|
| 10 |
+
azure-mgmt-subscription==3.1.1
|
| 11 |
+
Babel==2.15.0
|
| 12 |
+
bcrypt==4.0.1
|
| 13 |
+
bidict==0.23.1
|
| 14 |
+
blinker==1.8.2
|
| 15 |
+
boto3==1.33.13
|
| 16 |
+
botocore==1.33.13
|
| 17 |
+
Brlapi==0.8.3
|
| 18 |
+
Brotli==1.1.0
|
| 19 |
+
cachetools==5.3.3
|
| 20 |
+
certifi==2020.6.20
|
| 21 |
+
cffi==1.16.0
|
| 22 |
+
chardet==4.0.0
|
| 23 |
+
click==8.1.7
|
| 24 |
+
colorama==0.4.4
|
| 25 |
+
command-not-found==0.3
|
| 26 |
+
cryptography==42.0.7
|
| 27 |
+
cupshelpers==1.0
|
| 28 |
+
dbus-python==1.2.18
|
| 29 |
+
defer==1.0.6
|
| 30 |
+
distro==1.7.0
|
| 31 |
+
distro-info==1.1+ubuntu0.2
|
| 32 |
+
dnspython==2.6.1
|
| 33 |
+
email_validator==2.1.1
|
| 34 |
+
eventlet==0.34.2
|
| 35 |
+
Flask==3.0.3
|
| 36 |
+
flask-babel==4.0.0
|
| 37 |
+
Flask-Compress==1.15
|
| 38 |
+
Flask-Login==0.6.3
|
| 39 |
+
Flask-Mail==0.10.0
|
| 40 |
+
Flask-Migrate==4.0.7
|
| 41 |
+
Flask-Paranoid==0.3.0
|
| 42 |
+
Flask-Principal==0.4.0
|
| 43 |
+
Flask-Security-Too==5.4.3
|
| 44 |
+
Flask-SocketIO==5.3.6
|
| 45 |
+
Flask-SQLAlchemy==3.1.1
|
| 46 |
+
Flask-WTF==1.2.1
|
| 47 |
+
google-api-core==2.19.0
|
| 48 |
+
google-api-python-client==2.130.0
|
| 49 |
+
google-auth==2.29.0
|
| 50 |
+
google-auth-httplib2==0.2.0
|
| 51 |
+
google-auth-oauthlib==1.2.0
|
| 52 |
+
googleapis-common-protos==1.63.0
|
| 53 |
+
greenlet==1.1.2
|
| 54 |
+
h11==0.14.0
|
| 55 |
+
httpagentparser==1.9.5
|
| 56 |
+
httplib2==0.20.2
|
| 57 |
+
idna==3.3
|
| 58 |
+
importlib_metadata==7.1.0
|
| 59 |
+
importlib_resources==6.4.0
|
| 60 |
+
isodate==0.6.1
|
| 61 |
+
itsdangerous==2.2.0
|
| 62 |
+
jaraco.classes==3.4.0
|
| 63 |
+
jeepney==0.7.1
|
| 64 |
+
Jinja2==3.1.4
|
| 65 |
+
jmespath==1.0.1
|
| 66 |
+
jsonformatter==0.3.2
|
| 67 |
+
keyring==24.3.1
|
| 68 |
+
language-selector==0.1
|
| 69 |
+
launchpadlib==1.10.16
|
| 70 |
+
lazr.restfulclient==0.14.4
|
| 71 |
+
lazr.uri==1.0.6
|
| 72 |
+
ldap3==2.9.1
|
| 73 |
+
libgravatar==1.0.4
|
| 74 |
+
louis==3.20.0
|
| 75 |
+
macaroonbakery==1.3.1
|
| 76 |
+
Mako==1.3.5
|
| 77 |
+
markdown-it-py==3.0.0
|
| 78 |
+
MarkupSafe==2.1.5
|
| 79 |
+
mdurl==0.1.2
|
| 80 |
+
more-itertools==8.10.0
|
| 81 |
+
msal==1.28.0
|
| 82 |
+
msal-extensions==1.1.0
|
| 83 |
+
msrest==0.7.1
|
| 84 |
+
netifaces==0.11.0
|
| 85 |
+
oauthlib==3.2.0
|
| 86 |
+
olefile==0.46
|
| 87 |
+
packaging==24.0
|
| 88 |
+
paramiko==3.4.0
|
| 89 |
+
passlib==1.7.4
|
| 90 |
+
pexpect==4.8.0
|
| 91 |
+
pgadmin4==8.6
|
| 92 |
+
Pillow==9.0.1
|
| 93 |
+
portalocker==2.8.2
|
| 94 |
+
proto-plus==1.23.0
|
| 95 |
+
protobuf==4.25.3
|
| 96 |
+
psutil==5.9.8
|
| 97 |
+
psycopg==3.1.12
|
| 98 |
+
psycopg-binary==3.1.12
|
| 99 |
+
ptyprocess==0.7.0
|
| 100 |
+
pyasn1==0.6.0
|
| 101 |
+
pyasn1_modules==0.4.0
|
| 102 |
+
pycairo==1.20.1
|
| 103 |
+
pycparser==2.22
|
| 104 |
+
pycups==2.0.1
|
| 105 |
+
Pygments==2.18.0
|
| 106 |
+
PyGObject==3.42.1
|
| 107 |
+
PyJWT==2.3.0
|
| 108 |
+
pymacaroons==0.13.0
|
| 109 |
+
PyNaCl==1.5.0
|
| 110 |
+
pyotp==2.9.0
|
| 111 |
+
pyparsing==2.4.7
|
| 112 |
+
pypng==0.20220715.0
|
| 113 |
+
pyRFC3339==1.1
|
| 114 |
+
python-apt==2.4.0+ubuntu3
|
| 115 |
+
python-dateutil==2.8.1
|
| 116 |
+
python-debian==0.1.43+ubuntu1.1
|
| 117 |
+
python-engineio==4.9.1
|
| 118 |
+
python-socketio==5.11.2
|
| 119 |
+
pytz==2024.1
|
| 120 |
+
pyxdg==0.27
|
| 121 |
+
PyYAML==5.4.1
|
| 122 |
+
qrcode==7.4.2
|
| 123 |
+
reportlab==3.6.8
|
| 124 |
+
requests==2.25.1
|
| 125 |
+
requests-oauthlib==2.0.0
|
| 126 |
+
rich==13.7.1
|
| 127 |
+
rsa==4.9
|
| 128 |
+
s3transfer==0.8.2
|
| 129 |
+
SecretStorage==3.3.1
|
| 130 |
+
shellingham==1.5.4
|
| 131 |
+
simple-websocket==1.0.0
|
| 132 |
+
six==1.16.0
|
| 133 |
+
speaklater3==1.4
|
| 134 |
+
SQLAlchemy==2.0.30
|
| 135 |
+
sqlparse==0.5.0
|
| 136 |
+
sshtunnel==0.4.0
|
| 137 |
+
systemd-python==234
|
| 138 |
+
typer==0.12.3
|
| 139 |
+
typing_extensions==4.12.0
|
| 140 |
+
ua-parser==0.18.0
|
| 141 |
+
ubuntu-advantage-tools==8001
|
| 142 |
+
ubuntu-drivers-common==0.0.0
|
| 143 |
+
ufw==0.36.1
|
| 144 |
+
unattended-upgrades==0.1
|
| 145 |
+
uritemplate==4.1.1
|
| 146 |
+
urllib3==1.26.5
|
| 147 |
+
user-agents==2.2.0
|
| 148 |
+
wadllib==1.3.6
|
| 149 |
+
Werkzeug==3.0.3
|
| 150 |
+
wsproto==1.2.0
|
| 151 |
+
WTForms==3.1.2
|
| 152 |
+
xdg==5
|
| 153 |
+
xkit==0.0.0
|
| 154 |
+
zipp==1.0.0
|
| 155 |
+
zstandard==0.22.0
|