File size: 1,554 Bytes
5b53652
 
 
 
 
 
 
 
77a1b68
85a00f5
5b53652
 
 
 
5a0b3dd
5b53652
 
 
 
 
 
f98b4df
5b53652
 
 
 
f98b4df
85a00f5
a2afc78
85a00f5
 
5b53652
 
 
 
 
 
 
 
 
 
 
 
 
4d2674e
f98b4df
 
 
b0be802
fa9404a
ad4e03a
 
 
 
 
 
 
cbdeb59
4360a86
ad4e03a
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
import random
import json

import torch

from model import NeuralNet
from nltk_utils import bag_of_words, tokenize

device = torch.device("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 predict(message, history):
    history = history or []
    sentence = tokenize(message)
    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"]:
                reply = [random.choice(intent['responses'])]
    else:
        reply = ["Sorry I do not understand :-("]
        
    history.append((message, reply))
    return history, history

import gradio as gr

gr.Interface(fn=predict,
             theme="default",
             css=".footer {display:none !important}",
             inputs=["text", "state"],
             outputs=["chatbot", "state"],
             title="Coffee Shop Bot").launch(share=True)