Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,78 @@
|
|
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
|
13 |
|
|
|
|
|
14 |
stemmer = LancasterStemmer()
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
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,47 +80,35 @@ net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
|
|
26 |
net = tflearn.regression(net)
|
27 |
|
28 |
model = tflearn.DNN(net)
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
|
|
|
33 |
def bag_of_words(s, words):
|
34 |
bag = [0 for _ in range(len(words))]
|
35 |
s_words = nltk.word_tokenize(s)
|
36 |
-
print("Tokenized words:", s_words) # Debugging
|
37 |
s_words = [stemmer.stem(word.lower()) for word in s_words]
|
38 |
-
print("Stemmed words:", s_words) # Debugging
|
39 |
|
40 |
for se in s_words:
|
41 |
for i, w in enumerate(words):
|
42 |
if w == se:
|
43 |
bag[i] = 1
|
44 |
-
|
45 |
-
if not any(bag): # Check if bag is all zeros
|
46 |
-
print("Warning: No matching words found in vocabulary.")
|
47 |
return np.array(bag)
|
48 |
|
49 |
-
|
50 |
def chat(message, history=None):
|
51 |
history = history or []
|
52 |
message = message.lower()
|
53 |
|
54 |
try:
|
55 |
-
print("User message:", message) # Debugging
|
56 |
bag = bag_of_words(message, words)
|
57 |
-
print("Bag representation:", bag) # Debugging
|
58 |
results = model.predict([bag])
|
59 |
-
print("Prediction results:", results) # Debugging
|
60 |
-
|
61 |
-
if not results.any(): # Handle empty or invalid predictions
|
62 |
-
raise ValueError("Model returned no valid predictions.")
|
63 |
-
|
64 |
results_index = np.argmax(results)
|
65 |
tag = labels[results_index]
|
66 |
-
print("Predicted tag:", tag) # Debugging
|
67 |
-
|
68 |
except Exception as e:
|
69 |
-
print(f"Error during processing: {e}") #
|
70 |
response = "I'm sorry, I couldn't understand your message."
|
71 |
history.append((message, response))
|
72 |
return history, history
|
@@ -82,70 +124,17 @@ def chat(message, history=None):
|
|
82 |
history.append((message, response))
|
83 |
return history, history
|
84 |
|
85 |
-
|
86 |
css = """
|
87 |
footer {display:none !important}
|
88 |
-
|
89 |
-
.gr-button-primary {
|
90 |
-
z-index: 14;
|
91 |
-
height: 43px;
|
92 |
-
width: 130px;
|
93 |
-
left: 0px;
|
94 |
-
top: 0px;
|
95 |
-
padding: 0px;
|
96 |
-
cursor: pointer !important;
|
97 |
-
background: none rgb(17, 20, 45) !important;
|
98 |
-
border: none !important;
|
99 |
-
text-align: center !important;
|
100 |
-
font-family: Poppins !important;
|
101 |
-
font-size: 14px !important;
|
102 |
-
font-weight: 500 !important;
|
103 |
-
color: rgb(255, 255, 255) !important;
|
104 |
-
line-height: 1 !important;
|
105 |
-
border-radius: 12px !important;
|
106 |
-
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
107 |
-
box-shadow: none !important;
|
108 |
-
}
|
109 |
-
.gr-button-primary:hover{
|
110 |
-
z-index: 14;
|
111 |
-
height: 43px;
|
112 |
-
width: 130px;
|
113 |
-
left: 0px;
|
114 |
-
top: 0px;
|
115 |
-
padding: 0px;
|
116 |
-
cursor: pointer !important;
|
117 |
-
background: none rgb(37, 56, 133) !important;
|
118 |
-
border: none !important;
|
119 |
-
text-align: center !important;
|
120 |
-
font-family: Poppins !important;
|
121 |
-
font-size: 14px !important;
|
122 |
-
font-weight: 500 !important;
|
123 |
-
color: rgb(255, 255, 255) !important;
|
124 |
-
line-height: 1 !important;
|
125 |
-
border-radius: 12px !important;
|
126 |
-
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
127 |
-
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
128 |
-
}
|
129 |
-
.hover\:bg-orange-50:hover {
|
130 |
-
--tw-bg-opacity: 1 !important;
|
131 |
-
background-color: rgb(229,225,255) !important;
|
132 |
-
}
|
133 |
-
|
134 |
-
div[data-testid="user"] {
|
135 |
-
background-color: #253885 !important;
|
136 |
-
}
|
137 |
-
|
138 |
-
.h-\[40vh\]{
|
139 |
-
height: 70vh !important;
|
140 |
-
}
|
141 |
-
|
142 |
"""
|
143 |
demo = gr.Interface(
|
144 |
fn=chat,
|
145 |
inputs=[gr.Textbox(lines=1, label="Message"), gr.State([])],
|
146 |
outputs=[gr.Chatbot(label="Chat"), gr.State()],
|
147 |
allow_flagging="never",
|
148 |
-
title="Wellbeing
|
149 |
css=css
|
150 |
)
|
151 |
|
|
|
1 |
import nltk
|
|
|
|
|
2 |
import numpy as np
|
3 |
import tflearn
|
4 |
+
import tensorflow as tf
|
5 |
import random
|
6 |
import json
|
|
|
7 |
import pickle
|
8 |
import gradio as gr
|
9 |
+
from nltk.stem.lancaster import LancasterStemmer
|
10 |
|
11 |
+
# Ensure nltk downloads
|
12 |
+
nltk.download('punkt')
|
13 |
stemmer = LancasterStemmer()
|
14 |
|
15 |
+
# Load intents and check for errors
|
16 |
+
try:
|
17 |
+
with open("intents.json") as file:
|
18 |
+
data = json.load(file)
|
19 |
+
except FileNotFoundError:
|
20 |
+
raise FileNotFoundError("The file 'intents.json' was not found.")
|
21 |
+
|
22 |
+
# Load or regenerate the data
|
23 |
+
try:
|
24 |
+
with open("data.pickle", "rb") as f:
|
25 |
+
words, labels, training, output = pickle.load(f)
|
26 |
+
except FileNotFoundError:
|
27 |
+
# Regenerate the data.pickle if not found
|
28 |
+
words = []
|
29 |
+
labels = []
|
30 |
+
docs_x = []
|
31 |
+
docs_y = []
|
32 |
+
|
33 |
+
for intent in data["intents"]:
|
34 |
+
for pattern in intent["patterns"]:
|
35 |
+
wrds = nltk.word_tokenize(pattern)
|
36 |
+
words.extend(wrds)
|
37 |
+
docs_x.append(wrds)
|
38 |
+
docs_y.append(intent["tag"])
|
39 |
+
|
40 |
+
if intent["tag"] not in labels:
|
41 |
+
labels.append(intent["tag"])
|
42 |
+
|
43 |
+
words = [stemmer.stem(w.lower()) for w in words if w not in ["?", "!", ".", ","]]
|
44 |
+
words = sorted(list(set(words)))
|
45 |
+
labels = sorted(labels)
|
46 |
+
|
47 |
+
training = []
|
48 |
+
output = []
|
49 |
+
|
50 |
+
out_empty = [0 for _ in range(len(labels))]
|
51 |
+
|
52 |
+
for x, doc in enumerate(docs_x):
|
53 |
+
bag = []
|
54 |
+
|
55 |
+
wrds = [stemmer.stem(w.lower()) for w in doc]
|
56 |
|
57 |
+
for w in words:
|
58 |
+
if w in wrds:
|
59 |
+
bag.append(1)
|
60 |
+
else:
|
61 |
+
bag.append(0)
|
62 |
|
63 |
+
output_row = out_empty[:]
|
64 |
+
output_row[labels.index(docs_y[x])] = 1
|
65 |
+
|
66 |
+
training.append(bag)
|
67 |
+
output.append(output_row)
|
68 |
+
|
69 |
+
training = np.array(training)
|
70 |
+
output = np.array(output)
|
71 |
+
|
72 |
+
with open("data.pickle", "wb") as f:
|
73 |
+
pickle.dump((words, labels, training, output), f)
|
74 |
+
|
75 |
+
# Build the model
|
76 |
net = tflearn.input_data(shape=[None, len(training[0])])
|
77 |
net = tflearn.fully_connected(net, 8)
|
78 |
net = tflearn.fully_connected(net, 8)
|
|
|
80 |
net = tflearn.regression(net)
|
81 |
|
82 |
model = tflearn.DNN(net)
|
83 |
+
try:
|
84 |
+
model.load("MentalHealthChatBotmodel.tflearn")
|
85 |
+
except Exception as e:
|
86 |
+
raise FileNotFoundError("Model file 'MentalHealthChatBotmodel.tflearn' could not be loaded.") from e
|
87 |
|
88 |
+
# Function to convert user input into a bag-of-words representation
|
89 |
def bag_of_words(s, words):
|
90 |
bag = [0 for _ in range(len(words))]
|
91 |
s_words = nltk.word_tokenize(s)
|
|
|
92 |
s_words = [stemmer.stem(word.lower()) for word in s_words]
|
|
|
93 |
|
94 |
for se in s_words:
|
95 |
for i, w in enumerate(words):
|
96 |
if w == se:
|
97 |
bag[i] = 1
|
|
|
|
|
|
|
98 |
return np.array(bag)
|
99 |
|
100 |
+
# Chat function
|
101 |
def chat(message, history=None):
|
102 |
history = history or []
|
103 |
message = message.lower()
|
104 |
|
105 |
try:
|
|
|
106 |
bag = bag_of_words(message, words)
|
|
|
107 |
results = model.predict([bag])
|
|
|
|
|
|
|
|
|
|
|
108 |
results_index = np.argmax(results)
|
109 |
tag = labels[results_index]
|
|
|
|
|
110 |
except Exception as e:
|
111 |
+
print(f"Error during processing: {e}") # Debugging
|
112 |
response = "I'm sorry, I couldn't understand your message."
|
113 |
history.append((message, response))
|
114 |
return history, history
|
|
|
124 |
history.append((message, response))
|
125 |
return history, history
|
126 |
|
127 |
+
# Gradio Interface
|
128 |
css = """
|
129 |
footer {display:none !important}
|
130 |
+
div[data-testid="user"] {background-color: #253885 !important;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
"""
|
132 |
demo = gr.Interface(
|
133 |
fn=chat,
|
134 |
inputs=[gr.Textbox(lines=1, label="Message"), gr.State([])],
|
135 |
outputs=[gr.Chatbot(label="Chat"), gr.State()],
|
136 |
allow_flagging="never",
|
137 |
+
title="Wellbeing Chatbot",
|
138 |
css=css
|
139 |
)
|
140 |
|