Skip to content

Instantly share code, notes, and snippets.

@bumie-e
Last active February 7, 2021 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bumie-e/66c47110cc9c8fa5db0258d0e96a690a to your computer and use it in GitHub Desktop.
Save bumie-e/66c47110cc9c8fa5db0258d0e96a690a to your computer and use it in GitHub Desktop.
import random
import json
import torch
from model import NeuralNet
from fastapi import FastAPI
from nltk_utils import bag_of_words, tokenize
app = FastAPI()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open('intents.json', 'r') as json_data:
intents = json.load(json_data)
FILE = "data.pth"
data = torch.load(FILE)
input_size = data["input_size"]
hidden_size = data["hidden_size"]
output_size = data["output_size"]
all_words = data["all_words"]
tags = data["tags"]
model_state = data["model_state"]
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
def prediction(sentence):
sentence = tokenize(sentence)
X = bag_of_words(sentence, all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X).to(device)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
response = random.choice(intent['responses'])
else:
response = "I do not understand..."
return response
@app.get("/chat")
def ping():
return {"message": "'Let's chat!"}
@app.post("/predict/{sentence}")
def predict(sentence: str):
pred = prediction(sentence)
return {"message": str(pred)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment