Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 pickle
|
10 |
+
import gradio as gr
|
11 |
+
from nltk.tokenize import word_tokenize
|
12 |
+
|
13 |
+
# Ensure necessary NLTK resources are downloaded
|
14 |
+
try:
|
15 |
+
nltk.data.find('tokenizers/punkt')
|
16 |
+
except LookupError:
|
17 |
+
nltk.download('punkt')
|
18 |
+
|
19 |
+
# Initialize the stemmer
|
20 |
+
stemmer = LancasterStemmer()
|
21 |
+
|
22 |
+
# Load intents.json
|
23 |
+
try:
|
24 |
+
with open("intents.json") as file:
|
25 |
+
data = json.load(file)
|
26 |
+
except FileNotFoundError:
|
27 |
+
raise FileNotFoundError("Error: 'intents.json' file not found. Ensure it exists in the current directory.")
|
28 |
+
|
29 |
+
# Load preprocessed data from pickle
|
30 |
+
try:
|
31 |
+
with open("data.pickle", "rb") as f:
|
32 |
+
words, labels, training, output = pickle.load(f)
|
33 |
+
except FileNotFoundError:
|
34 |
+
raise FileNotFoundError("Error: 'data.pickle' file not found. Ensure it exists and matches the model.")
|
35 |
+
|
36 |
+
# Build the model structure
|
37 |
+
net = tflearn.input_data(shape=[None, len(training[0])])
|
38 |
+
net = tflearn.fully_connected(net, 8)
|
39 |
+
net = tflearn.fully_connected(net, 8)
|
40 |
+
net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
|
41 |
+
net = tflearn.regression(net)
|
42 |
+
|
43 |
+
# Load the trained model
|
44 |
+
model = tflearn.DNN(net)
|
45 |
+
try:
|
46 |
+
model.load("MentalHealthChatBotmodel.tflearn")
|
47 |
+
except FileNotFoundError:
|
48 |
+
print("Error: Trained model file not found. Ensure 'MentalHealthChatBotmodel.tflearn' exists.")
|
49 |
+
|
50 |
+
# Function to process user input into a bag-of-words format
|
51 |
+
def bag_of_words(s, words):
|
52 |
+
bag = [0 for _ in range(len(words))]
|
53 |
+
s_words = word_tokenize(s) # Replaced nltk.word_tokenize(s)
|
54 |
+
s_words = [stemmer.stem(word.lower()) for word in s_words if word.lower() in words]
|
55 |
+
for se in s_words:
|
56 |
+
for i, w in enumerate(words):
|
57 |
+
if w == se:
|
58 |
+
bag[i] = 1
|
59 |
+
return np.array(bag)
|
60 |
+
|
61 |
+
# Chat function
|
62 |
+
def chat(message, history):
|
63 |
+
history = history or []
|
64 |
+
message = message.lower()
|
65 |
+
|
66 |
+
try:
|
67 |
+
# Predict the tag
|
68 |
+
results = model.predict([bag_of_words(message, words)])
|
69 |
+
results_index = np.argmax(results)
|
70 |
+
tag = labels[results_index]
|
71 |
+
|
72 |
+
# Match tag with intent and choose a random response
|
73 |
+
for tg in data["intents"]:
|
74 |
+
if tg['tag'] == tag:
|
75 |
+
responses = tg['responses']
|
76 |
+
response = random.choice(responses)
|
77 |
+
break
|
78 |
+
else:
|
79 |
+
response = "I'm sorry, I didn't understand that. Could you please rephrase?"
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
response = f"An error occurred: {str(e)}"
|
83 |
+
|
84 |
+
history.append((message, response))
|
85 |
+
return history, history
|
86 |
+
|
87 |
+
# Gradio interface
|
88 |
+
chatbot = gr.Chatbot(label="Chat")
|
89 |
+
css = """
|
90 |
+
footer {display:none !important}
|
91 |
+
.output-markdown{display:none !important}
|
92 |
+
.gr-button-primary {
|
93 |
+
z-index: 14;
|
94 |
+
height: 43px;
|
95 |
+
width: 130px;
|
96 |
+
left: 0px;
|
97 |
+
top: 0px;
|
98 |
+
padding: 0px;
|
99 |
+
cursor: pointer !important;
|
100 |
+
background: none rgb(17, 20, 45) !important;
|
101 |
+
border: none !important;
|
102 |
+
text-align: center !important;
|
103 |
+
font-family: Poppins !important;
|
104 |
+
font-size: 14px !important;
|
105 |
+
font-weight: 500 !important;
|
106 |
+
color: rgb(255, 255, 255) !important;
|
107 |
+
line-height: 1 !important;
|
108 |
+
border-radius: 12px !important;
|
109 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
110 |
+
box-shadow: none !important;
|
111 |
+
}
|
112 |
+
.gr-button-primary:hover{
|
113 |
+
z-index: 14;
|
114 |
+
height: 43px;
|
115 |
+
width: 130px;
|
116 |
+
left: 0px;
|
117 |
+
top: 0px;
|
118 |
+
padding: 0px;
|
119 |
+
cursor: pointer !important;
|
120 |
+
background: none rgb(37, 56, 133) !important;
|
121 |
+
border: none !important;
|
122 |
+
text-align: center !important;
|
123 |
+
font-family: Poppins !important;
|
124 |
+
font-size: 14px !important;
|
125 |
+
font-weight: 500 !important;
|
126 |
+
color: rgb(255, 255, 255) !important;
|
127 |
+
line-height: 1 !important;
|
128 |
+
border-radius: 12px !important;
|
129 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
130 |
+
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
131 |
+
}
|
132 |
+
.hover\:bg-orange-50:hover {
|
133 |
+
--tw-bg-opacity: 1 !important;
|
134 |
+
background-color: rgb(229,225,255) !important;
|
135 |
+
}
|
136 |
+
div[data-testid="user"] {
|
137 |
+
background-color: #253885 !important;
|
138 |
+
}
|
139 |
+
.h-\[40vh\]{
|
140 |
+
height: 70vh !important;
|
141 |
+
}
|
142 |
+
"""
|
143 |
+
demo = gr.Interface(
|
144 |
+
chat,
|
145 |
+
[gr.Textbox(lines=1, label="Message"), "state"],
|
146 |
+
[chatbot, "state"],
|
147 |
+
allow_flagging="never",
|
148 |
+
title="Mental Health Bot | Data Science Dojo",
|
149 |
+
css=css
|
150 |
+
)
|
151 |
+
|
152 |
+
# Launch Gradio interface
|
153 |
+
if __name__ == "__main__":
|
154 |
+
demo.launch()
|
155 |
+
|