tahirsher commited on
Commit
7f5a352
·
verified ·
1 Parent(s): a8c5797

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -108
app.py CHANGED
@@ -1,91 +1,24 @@
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
- # URL for the background image
16
- flower_image_url = "https://i.postimg.cc/hG2FG85D/2.png"
17
-
18
- # Combined CSS for styling and blurred background
19
- css = f"""
20
- body {{
21
- margin: 0;
22
- padding: 0;
23
- overflow: hidden;
24
- }}
25
- .gradio-container {{
26
- position: relative;
27
- z-index: 1; /* Ensure UI elements are above the background */
28
- }}
29
- /* Blurred background image */
30
- .blurred-background {{
31
- position: fixed;
32
- top: 0;
33
- left: 0;
34
- width: 100%;
35
- height: 100%;
36
- z-index: -1; /* Send background image behind all UI elements */
37
- background-image: url("{flower_image_url}");
38
- background-size: cover;
39
- background-position: center;
40
- filter: blur(10px); /* Adjust blur ratio here */
41
- opacity: 0.8; /* Optional: Add slight transparency for a subtle effect */
42
- }}
43
- footer {{
44
- display: none !important;
45
- }}
46
- div[data-testid="user"] {{
47
- background-color: #253885 !important;
48
- }}
49
- .h-\[40vh\] {{
50
- height: 70vh !important;
51
- }}
52
- .gr-button-primary {{
53
- z-index: 14;
54
- height: 43px;
55
- width: 130px;
56
- padding: 0px;
57
- cursor: pointer !important;
58
- background: none rgb(17, 20, 45) !important;
59
- border: none !important;
60
- text-align: center !important;
61
- font-family: Poppins !important;
62
- font-size: 14px !important;
63
- font-weight: 500 !important;
64
- color: rgb(255, 255, 255) !important;
65
- border-radius: 12px !important;
66
- transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
67
- }}
68
- .gr-button-primary:hover {{
69
- background: none rgb(37, 56, 133) !important;
70
- }}
71
- """
72
-
73
- #""""""""""""""""""""""""" Application Code Starts Here """""""""""""""""""""""""""""""""""""""""""""
74
-
75
- # Load data and handle errors
76
- try:
77
- with open("intents.json") as file:
78
- data = json.load(file)
79
- except FileNotFoundError:
80
- raise FileNotFoundError("The file 'intents.json' was not found.")
81
 
82
- try:
83
- with open("data.pickle", "rb") as f:
84
- words, labels, training, output = pickle.load(f)
85
- except FileNotFoundError:
86
- raise FileNotFoundError("The file 'data.pickle' was not found.")
87
 
88
- # Build the model
89
  net = tflearn.input_data(shape=[None, len(training[0])])
90
  net = tflearn.fully_connected(net, 8)
91
  net = tflearn.fully_connected(net, 8)
@@ -93,17 +26,13 @@ net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
93
  net = tflearn.regression(net)
94
 
95
  model = tflearn.DNN(net)
96
- try:
97
- model.load("MentalHealthChatBotmodel.tflearn")
98
- except Exception as e:
99
- raise FileNotFoundError("Model file 'MentalHealthChatBotmodel.tflearn' could not be loaded.") from e
100
 
101
 
102
  def bag_of_words(s, words):
103
- """
104
- Convert a user input sentence into a bag-of-words representation.
105
- """
106
  bag = [0 for _ in range(len(words))]
 
107
  s_words = nltk.word_tokenize(s)
108
  s_words = [stemmer.stem(word.lower()) for word in s_words]
109
 
@@ -111,46 +40,89 @@ def bag_of_words(s, words):
111
  for i, w in enumerate(words):
112
  if w == se:
113
  bag[i] = 1
 
114
  return np.array(bag)
115
 
116
 
117
  def chat(message, history):
118
- """
119
- Handle chat interaction.
120
- """
121
  history = history or []
122
  message = message.lower()
123
-
124
- try:
125
- results = model.predict([bag_of_words(message, words)])
126
- results_index = np.argmax(results)
127
- tag = labels[results_index]
128
- except Exception as e:
129
- response = "I'm sorry, I couldn't understand your message."
130
- history.append((message, response))
131
- return history, history
132
 
133
  for tg in data["intents"]:
134
- if tg['tag'] == tag:
135
- responses = tg['responses']
136
- response = random.choice(responses)
137
- break
138
- else:
139
- response = "I'm sorry, I don't have a response for that."
140
 
 
 
 
141
  history.append((message, response))
142
  return history, history
143
 
144
-
145
- # Initialize Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  demo = gr.Interface(
147
- fn=chat,
148
- inputs=[gr.Textbox(lines=1, label="Message"), gr.State()],
149
- outputs=[gr.Chatbot(label="Chat"), gr.State()],
150
  allow_flagging="never",
151
- title="Wellbeing for All | Generative AI Enthusiasts",
152
  css=css
153
  )
154
-
155
  if __name__ == "__main__":
156
  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()