-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
61 lines (49 loc) · 1.49 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import tensorflow as tf
import numpy as np
import pickle
from keras.preprocessing import sequence
from embedding import to_index_array, padding, decompose_string
class Item(BaseModel):
text: str
class ProfanityChecker:
def __init__(self, model_path, dict_path, max_len):
self.model = tf.keras.models.load_model(model_path)
with open(dict_path, 'rb') as f:
self.jamodict = pickle.load(f)
self.max_len = max_len
def encode_review(self, text):
text = decompose_string(text)
text = to_index_array(text, self.jamodict)
return padding(text, self.max_len)
def predict(self, text):
indices = self.encode_review(text)
indices = np.array([indices])
y_prob = self.model.predict(indices)
return bool(y_prob.argmax(axis=-1) == 1)
app = FastAPI()
checker = ProfanityChecker('models/latest-yok-detect-model.h5', 'jamo.pydict', 681)
origins = [
"http://localhost",
"http://localhost:5000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post('/check-profanity')
async def check_slang(item: Item):
is_slang = checker.predict(item.text)
return {
"success": True,
"code": 0,
"message": "string",
"data": {
"isSlang": is_slang
}
}