Spaces:
Build error
Build error
ShAnSantosh
commited on
Commit
•
5b53652
1
Parent(s):
8138fa1
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,35 @@ import torch
|
|
4 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
5 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
6 |
"""
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
"""
|
9 |
# tokenize the new input sentence
|
10 |
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
@@ -19,7 +47,25 @@ def predict(input, history=[]):
|
|
19 |
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
20 |
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
21 |
"""
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
import gradio as gr
|
25 |
|
|
|
4 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
5 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
6 |
"""
|
7 |
+
|
8 |
+
import random
|
9 |
+
import json
|
10 |
+
|
11 |
+
import torch
|
12 |
+
|
13 |
+
from model import NeuralNet
|
14 |
+
from nltk_utils import bag_of_words, tokenize
|
15 |
+
|
16 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
17 |
+
|
18 |
+
with open('./intents.json', 'r') as json_data:
|
19 |
+
intents = json.load(json_data)
|
20 |
+
|
21 |
+
FILE = "./data.pth"
|
22 |
+
data = torch.load(FILE)
|
23 |
+
|
24 |
+
input_size = data["input_size"]
|
25 |
+
hidden_size = data["hidden_size"]
|
26 |
+
output_size = data["output_size"]
|
27 |
+
all_words = data['all_words']
|
28 |
+
tags = data['tags']
|
29 |
+
model_state = data["model_state"]
|
30 |
+
|
31 |
+
model = NeuralNet(input_size, hidden_size, output_size).to(device)
|
32 |
+
model.load_state_dict(model_state)
|
33 |
+
model.eval()
|
34 |
+
|
35 |
+
def predict(sentence, history=[]):
|
36 |
"""
|
37 |
# tokenize the new input sentence
|
38 |
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
|
|
47 |
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
48 |
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
49 |
"""
|
50 |
+
|
51 |
+
sentence = tokenize(sentence)
|
52 |
+
X = bag_of_words(sentence, all_words)
|
53 |
+
X = X.reshape(1, X.shape[0])
|
54 |
+
X = torch.from_numpy(X).to(device)
|
55 |
+
|
56 |
+
output = model(X)
|
57 |
+
_, predicted = torch.max(output, dim=1)
|
58 |
+
|
59 |
+
tag = tags[predicted.item()]
|
60 |
+
|
61 |
+
probs = torch.softmax(output, dim=1)
|
62 |
+
prob = probs[0][predicted.item()]
|
63 |
+
if prob.item() > 0.75:
|
64 |
+
for intent in intents['intents']:
|
65 |
+
if tag == intent["tag"]:
|
66 |
+
reply = random.choice(intent['responses'])
|
67 |
+
|
68 |
+
return reply, history
|
69 |
|
70 |
import gradio as gr
|
71 |
|