Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- api.py +63 -0
- requirements.txt +258 -0
- sentiment_classifier.pkl +3 -0
- vectorizer.pkl +3 -0
api.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Develop an API server on python using Fast API for the model created in the previous step.
|
2 |
+
|
3 |
+
from string import punctuation
|
4 |
+
from nltk.tokenize import word_tokenize
|
5 |
+
import nltk
|
6 |
+
from nltk.corpus import stopwords
|
7 |
+
from nltk.stem import WordNetLemmatizer
|
8 |
+
from os.path import dirname, join, realpath
|
9 |
+
import joblib
|
10 |
+
import uvicorn
|
11 |
+
from fastapi import FastAPI
|
12 |
+
import requests as r
|
13 |
+
from pyramid.config import Configurator
|
14 |
+
import transformers
|
15 |
+
#from pyramid_swagger import add_swagger_view
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
app = FastAPI(
|
23 |
+
title="Sentiment Analysis API",
|
24 |
+
description="A simple API that use NLP model to predict the sentiment of the airline reviews",
|
25 |
+
version="0.1",
|
26 |
+
)
|
27 |
+
|
28 |
+
# Load the model
|
29 |
+
model = joblib.load('sentiment_classifier.pkl')
|
30 |
+
vectorizer = joblib.load('vectorizer.pkl')
|
31 |
+
|
32 |
+
class Inference:
|
33 |
+
def __init__(self, model, vectorizer):
|
34 |
+
self.model = model
|
35 |
+
self.vectorizer = vectorizer
|
36 |
+
|
37 |
+
def get_sentiment(self, review):
|
38 |
+
new_review = [review]
|
39 |
+
new_review = self.vectorizer.transform(new_review)
|
40 |
+
pred = self.model.predict(new_review)
|
41 |
+
if pred == 1:
|
42 |
+
return 'Positive'
|
43 |
+
else:
|
44 |
+
return 'Negative'
|
45 |
+
|
46 |
+
inference = Inference(model, vectorizer)
|
47 |
+
|
48 |
+
@app.get("/")
|
49 |
+
def home():
|
50 |
+
return {"message": "Welcome to Sentiment Analysis API"}
|
51 |
+
|
52 |
+
@app.get("/predict-review/{review}")
|
53 |
+
def predict_sentiment(review: str):
|
54 |
+
return {"sentiment": inference.get_sentiment(review)}
|
55 |
+
|
56 |
+
#app.include_router(swagger_ui_bundle, tags=["Swagger UI"])
|
57 |
+
#app.include_router(swagger_ui_expose, tags=["Swagger UI"])
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,258 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
absl-py==1.0.0
|
2 |
+
aggdraw==1.3.15
|
3 |
+
altair==4.1.0
|
4 |
+
aniso8601==9.0.1
|
5 |
+
ansicon==1.89.0
|
6 |
+
anyio==3.6.2
|
7 |
+
argon2-cffi==21.1.0
|
8 |
+
arrow==1.2.3
|
9 |
+
asgiref==3.4.1
|
10 |
+
astor==0.8.1
|
11 |
+
astunparse==1.6.3
|
12 |
+
async-generator==1.10
|
13 |
+
attrs==21.2.0
|
14 |
+
autopep8==1.5.7
|
15 |
+
backcall==0.2.0
|
16 |
+
base58==2.1.0
|
17 |
+
beautifulsoup4==4.9.3
|
18 |
+
bleach==4.1.0
|
19 |
+
blessed==1.18.1
|
20 |
+
blinker==1.4
|
21 |
+
blis==0.7.8
|
22 |
+
bravado-core==5.17.1
|
23 |
+
bs4==0.0.1
|
24 |
+
cachetools==4.2.4
|
25 |
+
cap==0.0.114
|
26 |
+
catalogue==2.0.7
|
27 |
+
certifi==2021.5.30
|
28 |
+
cffi==1.14.6
|
29 |
+
chardet==3.0.4
|
30 |
+
charset-normalizer==2.0.4
|
31 |
+
click==7.1.2
|
32 |
+
colorama==0.4.4
|
33 |
+
cryptocmd==0.6.0
|
34 |
+
cryptography==37.0.4
|
35 |
+
cycler==0.10.0
|
36 |
+
cymem==2.0.6
|
37 |
+
debugpy==1.3.0
|
38 |
+
decorator==5.0.9
|
39 |
+
defusedxml==0.7.1
|
40 |
+
Django==3.2.5
|
41 |
+
docker-pycreds==0.4.0
|
42 |
+
easyocr==1.4.1
|
43 |
+
entrypoints==0.3
|
44 |
+
enum34==1.1.10
|
45 |
+
et-xmlfile==1.1.0
|
46 |
+
fastapi==0.88.0
|
47 |
+
filelock==3.8.2
|
48 |
+
Flask==2.0.1
|
49 |
+
Flask-Cors==3.0.10
|
50 |
+
flask-restplus==0.13.0
|
51 |
+
Flask-SQLAlchemy==2.5.1
|
52 |
+
flatbuffers==2.0
|
53 |
+
fqdn==1.5.1
|
54 |
+
gast==0.5.3
|
55 |
+
gitdb==4.0.9
|
56 |
+
GitPython==3.1.24
|
57 |
+
google-auth==2.6.6
|
58 |
+
google-auth-oauthlib==0.4.6
|
59 |
+
google-pasta==0.2.0
|
60 |
+
googletrans==3.0.0
|
61 |
+
greenlet==1.1.1
|
62 |
+
grpcio==1.46.0
|
63 |
+
gTTS==2.2.3
|
64 |
+
gunicorn==20.1.0
|
65 |
+
h11==0.9.0
|
66 |
+
h2==3.2.0
|
67 |
+
h5py==3.6.0
|
68 |
+
hpack==3.0.0
|
69 |
+
hstspreload==2021.12.1
|
70 |
+
httpcore==0.9.1
|
71 |
+
httpx==0.13.3
|
72 |
+
huggingface-hub==0.11.1
|
73 |
+
hupper==1.10.3
|
74 |
+
hyperframe==5.2.0
|
75 |
+
idna==2.10
|
76 |
+
imageio==2.9.0
|
77 |
+
importlib-metadata==4.11.3
|
78 |
+
insta-scrape==2.1.2
|
79 |
+
instagram-scraper==1.11.0
|
80 |
+
ipykernel==6.0.1
|
81 |
+
ipython==7.25.0
|
82 |
+
ipython-genutils==0.2.0
|
83 |
+
ipywidgets==7.6.5
|
84 |
+
isoduration==20.11.0
|
85 |
+
itsdangerous==2.0.1
|
86 |
+
jedi==0.18.0
|
87 |
+
Jinja2==3.0.1
|
88 |
+
jinxed==1.1.0
|
89 |
+
joblib==1.0.1
|
90 |
+
jsonpointer==2.3
|
91 |
+
jsonref==1.0.1
|
92 |
+
jsonschema==4.1.0
|
93 |
+
jupyter-client==6.1.12
|
94 |
+
jupyter-core==4.7.1
|
95 |
+
jupyterlab-pygments==0.1.2
|
96 |
+
jupyterlab-widgets==1.0.2
|
97 |
+
jupyterthemes==0.20.0
|
98 |
+
keras==2.8.0
|
99 |
+
Keras-Preprocessing==1.1.2
|
100 |
+
kiwisolver==1.3.2
|
101 |
+
langcodes==3.3.0
|
102 |
+
lesscpy==0.15.0
|
103 |
+
libclang==14.0.1
|
104 |
+
libretranslatepy==2.1.1
|
105 |
+
lxml==4.6.3
|
106 |
+
Markdown==3.3.7
|
107 |
+
MarkupSafe==2.0.1
|
108 |
+
matplotlib==3.4.3
|
109 |
+
matplotlib-inline==0.1.2
|
110 |
+
mistune==0.8.4
|
111 |
+
msgpack==1.0.4
|
112 |
+
murmurhash==1.0.7
|
113 |
+
nbclient==0.5.4
|
114 |
+
nbconvert==6.2.0
|
115 |
+
nbformat==5.1.3
|
116 |
+
nest-asyncio==1.5.1
|
117 |
+
networkx==2.6.3
|
118 |
+
nicer==0.0.36
|
119 |
+
nltk==3.8
|
120 |
+
notebook==6.4.4
|
121 |
+
numpy==1.21.2
|
122 |
+
oauthlib==3.2.0
|
123 |
+
opencv-python==4.5.5.64
|
124 |
+
opencv-python-headless==4.5.4.60
|
125 |
+
openpyxl==3.0.10
|
126 |
+
opt-einsum==3.3.0
|
127 |
+
outcome==1.2.0
|
128 |
+
packaging==21.0
|
129 |
+
pandas==1.3.2
|
130 |
+
pandocfilters==1.5.0
|
131 |
+
parso==0.8.2
|
132 |
+
PasteDeploy==3.0.1
|
133 |
+
pathtools==0.1.2
|
134 |
+
pathy==0.6.2
|
135 |
+
pickleshare==0.7.5
|
136 |
+
Pillow==8.2.0
|
137 |
+
pkt==0.0.21
|
138 |
+
plaster==1.1.2
|
139 |
+
plaster-pastedeploy==1.0.1
|
140 |
+
ply==3.11
|
141 |
+
preshed==3.0.6
|
142 |
+
prometheus-client==0.11.0
|
143 |
+
promise==2.3
|
144 |
+
prompt-toolkit==3.0.19
|
145 |
+
protobuf==3.19.0
|
146 |
+
psutil==5.9.0
|
147 |
+
pyarrow==6.0.0
|
148 |
+
pyasn1==0.4.8
|
149 |
+
pyasn1-modules==0.2.8
|
150 |
+
pycodestyle==2.7.0
|
151 |
+
pycparser==2.20
|
152 |
+
pydantic==1.8.2
|
153 |
+
pydeck==0.7.1
|
154 |
+
Pygments==2.9.0
|
155 |
+
pyOpenSSL==22.0.0
|
156 |
+
pyparsing==2.4.7
|
157 |
+
PyPDF2==1.26.0
|
158 |
+
pyramid==2.0
|
159 |
+
pyramid-swagger==2.7.0
|
160 |
+
pyrsistent==0.18.0
|
161 |
+
PySocks==1.7.1
|
162 |
+
python-bidi==0.4.2
|
163 |
+
python-dateutil==2.8.2
|
164 |
+
pytz==2021.1
|
165 |
+
pytz-deprecation-shim==0.1.0.post0
|
166 |
+
PyWavelets==1.2.0
|
167 |
+
pywin32==301
|
168 |
+
pywinpty==1.1.4
|
169 |
+
PyYAML==6.0
|
170 |
+
pyzmq==22.1.0
|
171 |
+
regex==2022.10.31
|
172 |
+
requests==2.26.0
|
173 |
+
requests-oauthlib==1.3.1
|
174 |
+
rfc3339-validator==0.1.4
|
175 |
+
rfc3986==1.5.0
|
176 |
+
rfc3987==1.3.8
|
177 |
+
rsa==4.8
|
178 |
+
scikit-image==0.19.0
|
179 |
+
scikit-learn==1.0.1
|
180 |
+
scipy==1.7.1
|
181 |
+
seaborn==0.11.2
|
182 |
+
selenium==4.3.0
|
183 |
+
Send2Trash==1.8.0
|
184 |
+
sentry-sdk==1.5.8
|
185 |
+
setproctitle==1.2.2
|
186 |
+
shortuuid==1.0.8
|
187 |
+
simplejson==3.18.0
|
188 |
+
six==1.16.0
|
189 |
+
sklearn==0.0
|
190 |
+
smart-open==5.2.1
|
191 |
+
smmap==5.0.0
|
192 |
+
sniffio==1.2.0
|
193 |
+
sortedcontainers==2.4.0
|
194 |
+
soupsieve==2.2.1
|
195 |
+
spacy==3.3.1
|
196 |
+
spacy-legacy==3.0.9
|
197 |
+
spacy-loggers==1.0.2
|
198 |
+
SQLAlchemy==1.4.22
|
199 |
+
sqlparse==0.4.1
|
200 |
+
srsly==2.4.3
|
201 |
+
starlette==0.22.0
|
202 |
+
streamlit==1.1.0
|
203 |
+
swagger-spec-validator==3.0.3
|
204 |
+
swagger-ui==0.1.2
|
205 |
+
swagger-ui-bundle==0.0.9
|
206 |
+
tablib==3.1.0
|
207 |
+
tensorboard==2.8.0
|
208 |
+
tensorboard-data-server==0.6.1
|
209 |
+
tensorboard-plugin-wit==1.8.1
|
210 |
+
tensorflow==2.8.0
|
211 |
+
tensorflow-io-gcs-filesystem==0.25.0
|
212 |
+
termcolor==1.1.0
|
213 |
+
terminado==0.12.1
|
214 |
+
testpath==0.5.0
|
215 |
+
tf-estimator-nightly==2.8.0.dev2021122109
|
216 |
+
thinc==8.0.17
|
217 |
+
threadpoolctl==2.2.0
|
218 |
+
tifffile==2021.11.2
|
219 |
+
tokenizers==0.13.2
|
220 |
+
toml==0.10.2
|
221 |
+
toolz==0.11.1
|
222 |
+
torch==1.10.0
|
223 |
+
torchvision==0.11.1
|
224 |
+
tornado==6.1
|
225 |
+
tqdm==4.64.0
|
226 |
+
traitlets==5.0.5
|
227 |
+
transformers==4.25.1
|
228 |
+
translate==3.6.1
|
229 |
+
translationstring==1.4
|
230 |
+
trio==0.21.0
|
231 |
+
trio-websocket==0.9.2
|
232 |
+
typer==0.4.1
|
233 |
+
typing==3.7.4.3
|
234 |
+
typing-extensions==3.10.0.2
|
235 |
+
tzdata==2021.5
|
236 |
+
tzlocal==4.0.2
|
237 |
+
uri-template==1.2.0
|
238 |
+
urllib3==1.26.6
|
239 |
+
uvicorn==0.20.0
|
240 |
+
validators==0.18.2
|
241 |
+
venusian==3.0.0
|
242 |
+
visualkeras==0.0.2
|
243 |
+
wandb==0.12.11
|
244 |
+
wasabi==0.9.1
|
245 |
+
watchdog==2.1.6
|
246 |
+
wcwidth==0.2.5
|
247 |
+
webcolors==1.12
|
248 |
+
webencodings==0.5.1
|
249 |
+
WebOb==1.8.7
|
250 |
+
Werkzeug==2.0.1
|
251 |
+
widgetsnbextension==3.5.1
|
252 |
+
wordcloud==1.8.2.2
|
253 |
+
wrapt==1.14.1
|
254 |
+
wsproto==1.1.0
|
255 |
+
yaspin==2.1.0
|
256 |
+
zipp==3.8.0
|
257 |
+
zope.deprecation==4.4.0
|
258 |
+
zope.interface==5.5.2
|
sentiment_classifier.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:19a95433516e7a8d4128ecd1fdec6c565d20f56f3b30ab73808180b839147092
|
3 |
+
size 70427
|
vectorizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:094050ba18d676fe0d51b1b71360d16851d1f3cbb78da9dc065368c9debe4346
|
3 |
+
size 112012
|