Commit
·
d2c421c
1
Parent(s):
3dfb1d5
updated code to resolve import error
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import nltk
|
2 |
nltk.download('punkt')
|
3 |
|
4 |
-
import nltk
|
5 |
from nltk.stem.lancaster import LancasterStemmer
|
6 |
import numpy as np
|
7 |
-
import
|
8 |
-
import
|
9 |
import random
|
10 |
import json
|
11 |
import pandas as pd
|
@@ -18,18 +17,9 @@ with open("intents.json") as file:
|
|
18 |
data = json.load(file)
|
19 |
|
20 |
with open("data.pickle", "rb") as f:
|
21 |
-
|
22 |
-
|
23 |
-
net = tflearn.input_data(shape=[None, len(training[0])])
|
24 |
-
net = tflearn.fully_connected(net, 8)
|
25 |
-
net = tflearn.fully_connected(net, 8)
|
26 |
-
net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
|
27 |
-
net = tflearn.regression(net)
|
28 |
-
|
29 |
-
model = tflearn.DNN(net)
|
30 |
-
model.load("MentalHealthChatBotmodel.tflearn")
|
31 |
-
# print('model loaded successfully')
|
32 |
|
|
|
33 |
|
34 |
def bag_of_words(s, words):
|
35 |
bag = [0 for _ in range(len(words))]
|
@@ -44,25 +34,22 @@ def bag_of_words(s, words):
|
|
44 |
|
45 |
return np.array(bag)
|
46 |
|
47 |
-
|
48 |
def chat(message, history):
|
49 |
history = history or []
|
50 |
message = message.lower()
|
51 |
-
results = model.predict([bag_of_words(message, words)])
|
52 |
results_index = np.argmax(results)
|
53 |
tag = labels[results_index]
|
54 |
|
55 |
for tg in data["intents"]:
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
-
# print(random.choice(responses))
|
60 |
-
response = random.choice(responses)
|
61 |
-
|
62 |
history.append((message, response))
|
63 |
return history, history
|
64 |
|
65 |
-
chatbot = gr.Chatbot(
|
66 |
css = """
|
67 |
footer {display:none !important}
|
68 |
.output-markdown{display:none !important}
|
|
|
1 |
import nltk
|
2 |
nltk.download('punkt')
|
3 |
|
|
|
4 |
from nltk.stem.lancaster import LancasterStemmer
|
5 |
import numpy as np
|
6 |
+
import tensorflow as tf
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
import random
|
9 |
import json
|
10 |
import pandas as pd
|
|
|
17 |
data = json.load(file)
|
18 |
|
19 |
with open("data.pickle", "rb") as f:
|
20 |
+
words, labels, training, output = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
model = load_model("MentalHealthChatBotmodel.tflearn")
|
23 |
|
24 |
def bag_of_words(s, words):
|
25 |
bag = [0 for _ in range(len(words))]
|
|
|
34 |
|
35 |
return np.array(bag)
|
36 |
|
|
|
37 |
def chat(message, history):
|
38 |
history = history or []
|
39 |
message = message.lower()
|
40 |
+
results = model.predict(np.array([bag_of_words(message, words)]))
|
41 |
results_index = np.argmax(results)
|
42 |
tag = labels[results_index]
|
43 |
|
44 |
for tg in data["intents"]:
|
45 |
+
if tg['tag'] == tag:
|
46 |
+
responses = tg['responses']
|
47 |
+
response = random.choice(responses)
|
48 |
|
|
|
|
|
|
|
49 |
history.append((message, response))
|
50 |
return history, history
|
51 |
|
52 |
+
chatbot = gr.Chatbot(chat)
|
53 |
css = """
|
54 |
footer {display:none !important}
|
55 |
.output-markdown{display:none !important}
|