Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,24 @@
|
|
1 |
import nltk
|
|
|
|
|
2 |
import numpy as np
|
3 |
import tflearn
|
4 |
-
import tensorflow
|
5 |
import random
|
6 |
import json
|
|
|
|
|
7 |
import gradio as gr
|
8 |
-
from
|
9 |
-
|
10 |
-
# Ensure required NLTK resources are downloaded
|
11 |
-
nltk.download('punkt')
|
12 |
-
nltk.download('punkt_tab')
|
13 |
|
14 |
stemmer = LancasterStemmer()
|
15 |
|
16 |
-
# Load intents file
|
17 |
with open("intents.json") as file:
|
18 |
data = json.load(file)
|
19 |
|
20 |
-
|
21 |
-
words, labels,
|
22 |
-
for intent in data["intents"]:
|
23 |
-
for pattern in intent["patterns"]:
|
24 |
-
wrds = nltk.word_tokenize(pattern)
|
25 |
-
words.extend(wrds)
|
26 |
-
docs_x.append(wrds)
|
27 |
-
docs_y.append(intent["tag"])
|
28 |
-
if intent["tag"] not in labels:
|
29 |
-
labels.append(intent["tag"])
|
30 |
-
|
31 |
-
# Stem and sort words
|
32 |
-
words = sorted(set(stemmer.stem(w.lower()) for w in words if w not in ["?", ".", ",", "!"]))
|
33 |
-
labels = sorted(labels)
|
34 |
-
|
35 |
-
# Create training data
|
36 |
-
training, output = [], []
|
37 |
-
out_empty = [0] * len(labels)
|
38 |
|
39 |
-
for x, doc in enumerate(docs_x):
|
40 |
-
bag = [1 if stemmer.stem(w.lower()) in [stemmer.stem(word) for word in doc] else 0 for w in words]
|
41 |
-
output_row = out_empty[:]
|
42 |
-
output_row[labels.index(docs_y[x])] = 1
|
43 |
-
training.append(bag)
|
44 |
-
output.append(output_row)
|
45 |
-
|
46 |
-
training, output = np.array(training), np.array(output)
|
47 |
-
|
48 |
-
# Build and train the model
|
49 |
-
tf.compat.v1.reset_default_graph()
|
50 |
net = tflearn.input_data(shape=[None, len(training[0])])
|
51 |
net = tflearn.fully_connected(net, 8)
|
52 |
net = tflearn.fully_connected(net, 8)
|
@@ -54,16 +26,13 @@ net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
|
|
54 |
net = tflearn.regression(net)
|
55 |
|
56 |
model = tflearn.DNN(net)
|
|
|
|
|
57 |
|
58 |
-
try:
|
59 |
-
model.load("MentalHealthChatBotmodel.tflearn")
|
60 |
-
except FileNotFoundError:
|
61 |
-
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
|
62 |
-
model.save("MentalHealthChatBotmodel.tflearn")
|
63 |
|
64 |
-
# Function to preprocess user input
|
65 |
def bag_of_words(s, words):
|
66 |
bag = [0 for _ in range(len(words))]
|
|
|
67 |
s_words = nltk.word_tokenize(s)
|
68 |
s_words = [stemmer.stem(word.lower()) for word in s_words]
|
69 |
|
@@ -71,36 +40,89 @@ def bag_of_words(s, words):
|
|
71 |
for i, w in enumerate(words):
|
72 |
if w == se:
|
73 |
bag[i] = 1
|
|
|
74 |
return np.array(bag)
|
75 |
|
76 |
-
# Chat function
|
77 |
-
def chat(message, history=None):
|
78 |
-
history = history or []
|
79 |
-
try:
|
80 |
-
bag = bag_of_words(message, words)
|
81 |
-
results = model.predict([bag])
|
82 |
-
results_index = np.argmax(results)
|
83 |
-
tag = labels[results_index]
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
history.append((message, response))
|
94 |
return history, history
|
95 |
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
demo = gr.Interface(
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
allow_flagging="never",
|
102 |
-
title="
|
|
|
103 |
)
|
104 |
-
|
105 |
if __name__ == "__main__":
|
106 |
-
demo.launch()
|
|
|
1 |
import nltk
|
2 |
+
nltk.download('punkt')
|
3 |
+
from nltk.stem.lancaster import LancasterStemmer
|
4 |
import numpy as np
|
5 |
import tflearn
|
6 |
+
import tensorflow
|
7 |
import random
|
8 |
import json
|
9 |
+
import pandas as pd
|
10 |
+
import pickle
|
11 |
import gradio as gr
|
12 |
+
from tensorflow.python.util.nest import is_sequence_or_composite
|
|
|
|
|
|
|
|
|
13 |
|
14 |
stemmer = LancasterStemmer()
|
15 |
|
|
|
16 |
with open("intents.json") as file:
|
17 |
data = json.load(file)
|
18 |
|
19 |
+
with open("data.pickle", "rb") as f:
|
20 |
+
words, labels, training, output = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
net = tflearn.input_data(shape=[None, len(training[0])])
|
23 |
net = tflearn.fully_connected(net, 8)
|
24 |
net = tflearn.fully_connected(net, 8)
|
|
|
26 |
net = tflearn.regression(net)
|
27 |
|
28 |
model = tflearn.DNN(net)
|
29 |
+
model.load("MentalHealthChatBotmodel.tflearn")
|
30 |
+
# print('model loaded successfully')
|
31 |
|
|
|
|
|
|
|
|
|
|
|
32 |
|
|
|
33 |
def bag_of_words(s, words):
|
34 |
bag = [0 for _ in range(len(words))]
|
35 |
+
|
36 |
s_words = nltk.word_tokenize(s)
|
37 |
s_words = [stemmer.stem(word.lower()) for word in s_words]
|
38 |
|
|
|
40 |
for i, w in enumerate(words):
|
41 |
if w == se:
|
42 |
bag[i] = 1
|
43 |
+
|
44 |
return np.array(bag)
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
def chat(message, history):
|
48 |
+
history = history or []
|
49 |
+
message = message.lower()
|
50 |
+
results = model.predict([bag_of_words(message, words)])
|
51 |
+
results_index = np.argmax(results)
|
52 |
+
tag = labels[results_index]
|
53 |
+
|
54 |
+
for tg in data["intents"]:
|
55 |
+
if tg['tag'] == tag:
|
56 |
+
responses = tg['responses']
|
57 |
+
|
58 |
+
# print(random.choice(responses))
|
59 |
+
response = random.choice(responses)
|
60 |
+
|
61 |
history.append((message, response))
|
62 |
return history, history
|
63 |
|
64 |
+
chatbot = gr.Chatbot(label="Chat")
|
65 |
+
css = """
|
66 |
+
footer {display:none !important}
|
67 |
+
.output-markdown{display:none !important}
|
68 |
+
.gr-button-primary {
|
69 |
+
z-index: 14;
|
70 |
+
height: 43px;
|
71 |
+
width: 130px;
|
72 |
+
left: 0px;
|
73 |
+
top: 0px;
|
74 |
+
padding: 0px;
|
75 |
+
cursor: pointer !important;
|
76 |
+
background: none rgb(17, 20, 45) !important;
|
77 |
+
border: none !important;
|
78 |
+
text-align: center !important;
|
79 |
+
font-family: Poppins !important;
|
80 |
+
font-size: 14px !important;
|
81 |
+
font-weight: 500 !important;
|
82 |
+
color: rgb(255, 255, 255) !important;
|
83 |
+
line-height: 1 !important;
|
84 |
+
border-radius: 12px !important;
|
85 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
86 |
+
box-shadow: none !important;
|
87 |
+
}
|
88 |
+
.gr-button-primary:hover{
|
89 |
+
z-index: 14;
|
90 |
+
height: 43px;
|
91 |
+
width: 130px;
|
92 |
+
left: 0px;
|
93 |
+
top: 0px;
|
94 |
+
padding: 0px;
|
95 |
+
cursor: pointer !important;
|
96 |
+
background: none rgb(37, 56, 133) !important;
|
97 |
+
border: none !important;
|
98 |
+
text-align: center !important;
|
99 |
+
font-family: Poppins !important;
|
100 |
+
font-size: 14px !important;
|
101 |
+
font-weight: 500 !important;
|
102 |
+
color: rgb(255, 255, 255) !important;
|
103 |
+
line-height: 1 !important;
|
104 |
+
border-radius: 12px !important;
|
105 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
106 |
+
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
107 |
+
}
|
108 |
+
.hover\:bg-orange-50:hover {
|
109 |
+
--tw-bg-opacity: 1 !important;
|
110 |
+
background-color: rgb(229,225,255) !important;
|
111 |
+
}
|
112 |
+
div[data-testid="user"] {
|
113 |
+
background-color: #253885 !important;
|
114 |
+
}
|
115 |
+
.h-\[40vh\]{
|
116 |
+
height: 70vh !important;
|
117 |
+
}
|
118 |
+
"""
|
119 |
demo = gr.Interface(
|
120 |
+
chat,
|
121 |
+
[gr.Textbox(lines=1, label="Message"), "state"],
|
122 |
+
[chatbot, "state"],
|
123 |
allow_flagging="never",
|
124 |
+
title="Mental Health Bot | Data Science Dojo",
|
125 |
+
css=css
|
126 |
)
|
|
|
127 |
if __name__ == "__main__":
|
128 |
+
demo.launch()
|