HudsonArauj commited on
Commit
e5aa1e6
·
1 Parent(s): d45489d

Resolve problems

Browse files
app/language_detection.ipynb CHANGED
@@ -14,7 +14,7 @@
14
  },
15
  {
16
  "cell_type": "code",
17
- "execution_count": 33,
18
  "metadata": {},
19
  "outputs": [],
20
  "source": [
@@ -51,7 +51,7 @@
51
  },
52
  {
53
  "cell_type": "code",
54
- "execution_count": 35,
55
  "metadata": {},
56
  "outputs": [],
57
  "source": [
@@ -60,7 +60,7 @@
60
  },
61
  {
62
  "cell_type": "code",
63
- "execution_count": 36,
64
  "metadata": {},
65
  "outputs": [],
66
  "source": [
@@ -70,7 +70,7 @@
70
  },
71
  {
72
  "cell_type": "code",
73
- "execution_count": 37,
74
  "metadata": {},
75
  "outputs": [],
76
  "source": [
@@ -103,7 +103,7 @@
103
  },
104
  {
105
  "cell_type": "code",
106
- "execution_count": 39,
107
  "metadata": {},
108
  "outputs": [],
109
  "source": [
@@ -117,7 +117,7 @@
117
  },
118
  {
119
  "cell_type": "code",
120
- "execution_count": 40,
121
  "metadata": {},
122
  "outputs": [],
123
  "source": [
@@ -128,7 +128,7 @@
128
  },
129
  {
130
  "cell_type": "code",
131
- "execution_count": 42,
132
  "metadata": {},
133
  "outputs": [],
134
  "source": [
@@ -140,7 +140,7 @@
140
  },
141
  {
142
  "cell_type": "code",
143
- "execution_count": 43,
144
  "metadata": {},
145
  "outputs": [
146
  {
@@ -152,7 +152,7 @@
152
  "MultinomialNB()"
153
  ]
154
  },
155
- "execution_count": 43,
156
  "metadata": {},
157
  "output_type": "execute_result"
158
  }
@@ -166,7 +166,7 @@
166
  },
167
  {
168
  "cell_type": "code",
169
- "execution_count": 44,
170
  "metadata": {},
171
  "outputs": [],
172
  "source": [
@@ -175,7 +175,7 @@
175
  },
176
  {
177
  "cell_type": "code",
178
- "execution_count": 45,
179
  "metadata": {},
180
  "outputs": [
181
  {
@@ -192,12 +192,13 @@
192
  "ac = accuracy_score(y_test, y_predict)\n",
193
  "cm = confusion_matrix(y_test, y_predict)\n",
194
  "cr = classification_report(y_test, y_predict)\n",
 
195
  "print('Accuracy is:', ac)"
196
  ]
197
  },
198
  {
199
  "cell_type": "code",
200
- "execution_count": 32,
201
  "metadata": {},
202
  "outputs": [
203
  {
@@ -237,7 +238,7 @@
237
  },
238
  {
239
  "cell_type": "code",
240
- "execution_count": 33,
241
  "metadata": {},
242
  "outputs": [
243
  {
@@ -262,20 +263,20 @@
262
  },
263
  {
264
  "cell_type": "code",
265
- "execution_count": 48,
266
  "metadata": {},
267
  "outputs": [],
268
  "source": [
269
- "# # Save the model\n",
270
- "# import pickle\n",
271
  "\n",
272
- "# with open('trained-01.pkl', 'wb') as file:\n",
273
- "# pickle.dump(model, file)\n"
274
  ]
275
  },
276
  {
277
  "cell_type": "code",
278
- "execution_count": 46,
279
  "metadata": {},
280
  "outputs": [
281
  {
@@ -284,7 +285,7 @@
284
  "array(['Portugeese', 'English'], dtype=object)"
285
  ]
286
  },
287
- "execution_count": 46,
288
  "metadata": {},
289
  "output_type": "execute_result"
290
  }
@@ -301,16 +302,16 @@
301
  },
302
  {
303
  "cell_type": "code",
304
- "execution_count": 50,
305
  "metadata": {},
306
  "outputs": [
307
  {
308
  "data": {
309
  "text/plain": [
310
- "'Portugeese'"
311
  ]
312
  },
313
- "execution_count": 50,
314
  "metadata": {},
315
  "output_type": "execute_result"
316
  }
@@ -331,28 +332,39 @@
331
  " return classes[pred[0]]\n",
332
  "\n",
333
  "\n",
334
- "predict_language('Oi tudo bem?')"
335
  ]
336
  },
337
  {
338
  "cell_type": "code",
339
- "execution_count": 30,
340
  "metadata": {},
341
- "outputs": [],
 
 
 
 
 
 
 
 
342
  "source": [
343
  "import requests\n",
344
- "# teste api local \n",
345
- "url = 'http://localhost:8080/predict'\n",
346
  "\n",
 
347
  "text1 = 'Oi tudo bem, como voce vai?'\n",
348
  "\n",
349
  "response = requests.post(url, json={'text': text1})\n",
350
- "\n"
 
 
 
 
351
  ]
352
  },
353
  {
354
  "cell_type": "code",
355
- "execution_count": 31,
356
  "metadata": {},
357
  "outputs": [
358
  {
@@ -361,7 +373,7 @@
361
  "<Response [500]>"
362
  ]
363
  },
364
- "execution_count": 31,
365
  "metadata": {},
366
  "output_type": "execute_result"
367
  }
@@ -370,6 +382,36 @@
370
  "response"
371
  ]
372
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  {
374
  "cell_type": "code",
375
  "execution_count": null,
 
14
  },
15
  {
16
  "cell_type": "code",
17
+ "execution_count": 2,
18
  "metadata": {},
19
  "outputs": [],
20
  "source": [
 
51
  },
52
  {
53
  "cell_type": "code",
54
+ "execution_count": 6,
55
  "metadata": {},
56
  "outputs": [],
57
  "source": [
 
60
  },
61
  {
62
  "cell_type": "code",
63
+ "execution_count": 7,
64
  "metadata": {},
65
  "outputs": [],
66
  "source": [
 
70
  },
71
  {
72
  "cell_type": "code",
73
+ "execution_count": 8,
74
  "metadata": {},
75
  "outputs": [],
76
  "source": [
 
103
  },
104
  {
105
  "cell_type": "code",
106
+ "execution_count": 9,
107
  "metadata": {},
108
  "outputs": [],
109
  "source": [
 
117
  },
118
  {
119
  "cell_type": "code",
120
+ "execution_count": 10,
121
  "metadata": {},
122
  "outputs": [],
123
  "source": [
 
128
  },
129
  {
130
  "cell_type": "code",
131
+ "execution_count": 17,
132
  "metadata": {},
133
  "outputs": [],
134
  "source": [
 
140
  },
141
  {
142
  "cell_type": "code",
143
+ "execution_count": 18,
144
  "metadata": {},
145
  "outputs": [
146
  {
 
152
  "MultinomialNB()"
153
  ]
154
  },
155
+ "execution_count": 18,
156
  "metadata": {},
157
  "output_type": "execute_result"
158
  }
 
166
  },
167
  {
168
  "cell_type": "code",
169
+ "execution_count": 19,
170
  "metadata": {},
171
  "outputs": [],
172
  "source": [
 
175
  },
176
  {
177
  "cell_type": "code",
178
+ "execution_count": null,
179
  "metadata": {},
180
  "outputs": [
181
  {
 
192
  "ac = accuracy_score(y_test, y_predict)\n",
193
  "cm = confusion_matrix(y_test, y_predict)\n",
194
  "cr = classification_report(y_test, y_predict)\n",
195
+ "\n",
196
  "print('Accuracy is:', ac)"
197
  ]
198
  },
199
  {
200
  "cell_type": "code",
201
+ "execution_count": null,
202
  "metadata": {},
203
  "outputs": [
204
  {
 
238
  },
239
  {
240
  "cell_type": "code",
241
+ "execution_count": null,
242
  "metadata": {},
243
  "outputs": [
244
  {
 
263
  },
264
  {
265
  "cell_type": "code",
266
+ "execution_count": 49,
267
  "metadata": {},
268
  "outputs": [],
269
  "source": [
270
+ "# Salvar o modelo e o vocabulário\n",
271
+ "import pickle\n",
272
  "\n",
273
+ "with open('filename.pkl', 'wb') as f:\n",
274
+ " pickle.dump({'model': model, 'vocab': cv}, f)\n"
275
  ]
276
  },
277
  {
278
  "cell_type": "code",
279
+ "execution_count": 48,
280
  "metadata": {},
281
  "outputs": [
282
  {
 
285
  "array(['Portugeese', 'English'], dtype=object)"
286
  ]
287
  },
288
+ "execution_count": 48,
289
  "metadata": {},
290
  "output_type": "execute_result"
291
  }
 
302
  },
303
  {
304
  "cell_type": "code",
305
+ "execution_count": 33,
306
  "metadata": {},
307
  "outputs": [
308
  {
309
  "data": {
310
  "text/plain": [
311
+ "'English'"
312
  ]
313
  },
314
+ "execution_count": 33,
315
  "metadata": {},
316
  "output_type": "execute_result"
317
  }
 
332
  " return classes[pred[0]]\n",
333
  "\n",
334
  "\n",
335
+ "predict_language('How are?')"
336
  ]
337
  },
338
  {
339
  "cell_type": "code",
340
+ "execution_count": 50,
341
  "metadata": {},
342
+ "outputs": [
343
+ {
344
+ "name": "stdout",
345
+ "output_type": "stream",
346
+ "text": [
347
+ "{'language': 'Portugeese'}\n"
348
+ ]
349
+ }
350
+ ],
351
  "source": [
352
  "import requests\n",
 
 
353
  "\n",
354
+ "url = 'http://localhost:7860/predict'\n",
355
  "text1 = 'Oi tudo bem, como voce vai?'\n",
356
  "\n",
357
  "response = requests.post(url, json={'text': text1})\n",
358
+ "\n",
359
+ "if response.status_code == 200:\n",
360
+ " print(response.json())\n",
361
+ "else:\n",
362
+ " print(f\"Error: {response.status_code}, Detail: {response.text}\")\n"
363
  ]
364
  },
365
  {
366
  "cell_type": "code",
367
+ "execution_count": 30,
368
  "metadata": {},
369
  "outputs": [
370
  {
 
373
  "<Response [500]>"
374
  ]
375
  },
376
+ "execution_count": 30,
377
  "metadata": {},
378
  "output_type": "execute_result"
379
  }
 
382
  "response"
383
  ]
384
  },
385
+ {
386
+ "cell_type": "code",
387
+ "execution_count": null,
388
+ "metadata": {},
389
+ "outputs": [],
390
+ "source": []
391
+ },
392
+ {
393
+ "cell_type": "code",
394
+ "execution_count": 39,
395
+ "metadata": {},
396
+ "outputs": [
397
+ {
398
+ "data": {
399
+ "text/html": [
400
+ "<style>#sk-container-id-2 {color: black;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>MultinomialNB()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">MultinomialNB</label><div class=\"sk-toggleable__content\"><pre>MultinomialNB()</pre></div></div></div></div></div>"
401
+ ],
402
+ "text/plain": [
403
+ "MultinomialNB()"
404
+ ]
405
+ },
406
+ "execution_count": 39,
407
+ "metadata": {},
408
+ "output_type": "execute_result"
409
+ }
410
+ ],
411
+ "source": [
412
+ "model"
413
+ ]
414
+ },
415
  {
416
  "cell_type": "code",
417
  "execution_count": null,
app/main.py CHANGED
@@ -1,22 +1,17 @@
1
- from fastapi import FastAPI,HTTPException
2
- from app.model.model import predict_language
3
- from app.model.model import __version__
4
  from app.schemas import Prediction, TextIn
5
 
6
-
7
  app = FastAPI()
8
 
9
-
10
  @app.get("/")
11
  def home():
12
  return {"message": "Ok", "model_version": __version__}
13
 
14
-
15
  @app.post("/predict", response_model=Prediction)
16
  def predict(payload: TextIn):
17
  try:
18
  language = predict_language(payload.text)
19
  return {"language": language}
20
  except Exception as e:
21
- raise HTTPException(status_code=500, detail="Internal Server Error")
22
-
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from app.model.model import predict_language, __version__
 
3
  from app.schemas import Prediction, TextIn
4
 
 
5
  app = FastAPI()
6
 
 
7
  @app.get("/")
8
  def home():
9
  return {"message": "Ok", "model_version": __version__}
10
 
 
11
  @app.post("/predict", response_model=Prediction)
12
  def predict(payload: TextIn):
13
  try:
14
  language = predict_language(payload.text)
15
  return {"language": language}
16
  except Exception as e:
17
+ raise HTTPException(status_code=400, detail=str(e))
 
app/model/{trained-01.pkl → filename.pkl} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:4c65a0d9fd8daa233db3aad9bcff1703ab1441e2bded657cc11b6cbea5daa3da
3
- size 9468690
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:63f7dc5c0ca806fda6983b96a7721a8add27e09c140c41d1a339b3fb4d363fb2
3
+ size 9993593
app/model/model.py CHANGED
@@ -1,27 +1,27 @@
 
1
  import pickle
2
  import re
3
  from pathlib import Path
4
  from sklearn.feature_extraction.text import CountVectorizer
5
- cv = CountVectorizer(max_features = 1500)
6
- __version__ = '01'
7
 
 
 
8
 
9
  BASE_DIR = Path(__file__).resolve(strict=True).parent
10
 
 
 
 
 
11
 
12
- with open(f"{BASE_DIR}/trained-{__version__}.pkl", "rb") as f:
13
- model = pickle.load(f)
14
-
15
  classes = ['Arabic', 'Danish', 'Dutch', 'English', 'French', 'German',
16
- 'Greek', 'Hindi', 'Italian', 'Kannada', 'Malayalam', 'Portugeese',
17
- 'Russian', 'Spanish', 'Sweedish', 'Tamil', 'Turkish']
18
-
19
-
20
 
21
  def predict_language(text):
22
  text = re.sub(r'[!@#$(),\n"%^*?\:;~`0-9]', ' ', text)
23
  text = re.sub(r'[\[\]]', ' ', text)
24
  text = text.lower()
25
- text = cv.transform([text]).toarray()
26
- pred = model.predict([text])
27
- return classes[pred[0]]
 
1
+ # app/model/model.py
2
  import pickle
3
  import re
4
  from pathlib import Path
5
  from sklearn.feature_extraction.text import CountVectorizer
 
 
6
 
7
+ cv = CountVectorizer(max_features=1500)
8
+ __version__ = '01'
9
 
10
  BASE_DIR = Path(__file__).resolve(strict=True).parent
11
 
12
+ with open(f"{BASE_DIR}/filename.pkl", "rb") as f:
13
+ data = pickle.load(f)
14
+ model = data['model']
15
+ vocab = data['vocab']
16
 
 
 
 
17
  classes = ['Arabic', 'Danish', 'Dutch', 'English', 'French', 'German',
18
+ 'Greek', 'Hindi', 'Italian', 'Kannada', 'Malayalam', 'Portugeese',
19
+ 'Russian', 'Spanish', 'Sweedish', 'Tamil', 'Turkish']
 
 
20
 
21
  def predict_language(text):
22
  text = re.sub(r'[!@#$(),\n"%^*?\:;~`0-9]', ' ', text)
23
  text = re.sub(r'[\[\]]', ' ', text)
24
  text = text.lower()
25
+ text = vocab.transform([text]).toarray()
26
+ pred = model.predict(text)
27
+ return classes[pred[0]]
app/schemas.py CHANGED
@@ -1,9 +1,7 @@
1
  from pydantic import BaseModel
2
 
3
-
4
  class TextIn(BaseModel):
5
  text: str
6
-
7
-
8
  class Prediction(BaseModel):
9
  language: str
 
1
  from pydantic import BaseModel
2
 
 
3
  class TextIn(BaseModel):
4
  text: str
5
+
 
6
  class Prediction(BaseModel):
7
  language: str