nexus-ai-clf / main.py
suneeln-duke's picture
Added API code
1c6eb74
raw
history blame contribute delete
753 Bytes
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]