File size: 753 Bytes
c26982a
 
 
 
 
 
 
 
 
 
 
 
 
 
1c6eb74
c26982a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI 

import pickle

from keras.preprocessing.sequence import pad_sequences

import pickle

from keras.models import load_model

import numpy as np

app = FastAPI()

model_path, token_path = 'model/nlp.h5', 'model/tokenizer.pkl'

model = load_model(model_path)

with open(token_path, 'rb') as f:
    tokenizer = pickle.load(f)
  
@app.get("/")
def hello():
    return {"Hello": "World"}

@app.post("/predict")
def predict(text: str):

    sequences = tokenizer.texts_to_sequences([text])
    x_new = pad_sequences(sequences, maxlen=50)
    predictions = model.predict([x_new, x_new])
    
    mapping = {0: 'no', 1: 'yes'}
    
    probs = list(predictions[0])
    
    max_idx = np.argmax(probs)
    
    return mapping[max_idx]