Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,11 +12,11 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipe
|
|
12 |
import pandas as pd
|
13 |
import torch
|
14 |
|
15 |
-
# Disable GPU usage for TensorFlow
|
16 |
-
os.environ[
|
17 |
|
18 |
-
# Download
|
19 |
-
nltk.download(
|
20 |
|
21 |
# Initialize Lancaster Stemmer
|
22 |
stemmer = LancasterStemmer()
|
@@ -55,12 +55,13 @@ def bag_of_words(s, words):
|
|
55 |
|
56 |
# Chatbot response generator
|
57 |
def chatbot_response(message, history):
|
|
|
58 |
history = history or []
|
59 |
try:
|
60 |
result = chatbot_model.predict([bag_of_words(message, words)])
|
61 |
idx = np.argmax(result)
|
62 |
tag = labels[idx]
|
63 |
-
response = "I'm not sure how to respond to that π€"
|
64 |
for intent in intents_data["intents"]:
|
65 |
if intent["tag"] == tag:
|
66 |
response = random.choice(intent["responses"])
|
@@ -68,8 +69,8 @@ def chatbot_response(message, history):
|
|
68 |
except Exception as e:
|
69 |
response = f"Error generating response: {str(e)} π₯"
|
70 |
|
71 |
-
|
72 |
-
history.append(
|
73 |
return history, response
|
74 |
|
75 |
# Hugging Face transformers model for emotion detection
|
@@ -150,43 +151,49 @@ def well_being_app(user_input, history):
|
|
150 |
# Custom CSS for Beautification
|
151 |
custom_css = """
|
152 |
body {
|
153 |
-
background: linear-gradient(135deg, #
|
154 |
font-family: 'Arial', sans-serif;
|
155 |
-
color:
|
156 |
-
text-align: center;
|
157 |
}
|
158 |
#component-0 span {
|
159 |
-
color:
|
160 |
}
|
161 |
button {
|
162 |
-
background-color: #
|
163 |
-
border: none;
|
164 |
color: white;
|
165 |
-
padding: 12px
|
166 |
-
text-align: center;
|
167 |
font-size: 16px;
|
168 |
-
border-radius:
|
169 |
cursor: pointer;
|
170 |
}
|
171 |
button:hover {
|
172 |
-
background-color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
}
|
174 |
"""
|
175 |
|
176 |
# Gradio UI
|
177 |
with gr.Blocks(css=custom_css) as interface:
|
178 |
-
gr.Markdown("#
|
179 |
-
gr.Markdown("###
|
180 |
|
181 |
with gr.Row():
|
182 |
user_input = gr.Textbox(lines=2, placeholder="How can I support you today?", label="Your Input")
|
183 |
-
|
184 |
with gr.Row():
|
185 |
submit_button = gr.Button("Submit", elem_id="submit")
|
186 |
|
187 |
with gr.Row():
|
188 |
chatbot_out = gr.Chatbot(label="Chat History")
|
189 |
-
sentiment_out = gr.Textbox(label="Sentiment")
|
190 |
emotion_out = gr.Textbox(label="Detected Emotion")
|
191 |
|
192 |
with gr.Row():
|
|
|
12 |
import pandas as pd
|
13 |
import torch
|
14 |
|
15 |
+
# Disable GPU usage for TensorFlow compatibility
|
16 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
17 |
|
18 |
+
# Download necessary NLTK resources
|
19 |
+
nltk.download("punkt")
|
20 |
|
21 |
# Initialize Lancaster Stemmer
|
22 |
stemmer = LancasterStemmer()
|
|
|
55 |
|
56 |
# Chatbot response generator
|
57 |
def chatbot_response(message, history):
|
58 |
+
"""Generates a response from the chatbot and appends it to the history."""
|
59 |
history = history or []
|
60 |
try:
|
61 |
result = chatbot_model.predict([bag_of_words(message, words)])
|
62 |
idx = np.argmax(result)
|
63 |
tag = labels[idx]
|
64 |
+
response = "I'm not sure how to respond to that. π€"
|
65 |
for intent in intents_data["intents"]:
|
66 |
if intent["tag"] == tag:
|
67 |
response = random.choice(intent["responses"])
|
|
|
69 |
except Exception as e:
|
70 |
response = f"Error generating response: {str(e)} π₯"
|
71 |
|
72 |
+
# Format output as tuples for Gradio Chatbot compatibility
|
73 |
+
history.append((message, response))
|
74 |
return history, response
|
75 |
|
76 |
# Hugging Face transformers model for emotion detection
|
|
|
151 |
# Custom CSS for Beautification
|
152 |
custom_css = """
|
153 |
body {
|
154 |
+
background: linear-gradient(135deg, #28a745, #218838);
|
155 |
font-family: 'Arial', sans-serif;
|
156 |
+
color: black;
|
|
|
157 |
}
|
158 |
#component-0 span {
|
159 |
+
color: white;
|
160 |
}
|
161 |
button {
|
162 |
+
background-color: #20c997;
|
|
|
163 |
color: white;
|
164 |
+
padding: 12px 20px;
|
|
|
165 |
font-size: 16px;
|
166 |
+
border-radius: 12px;
|
167 |
cursor: pointer;
|
168 |
}
|
169 |
button:hover {
|
170 |
+
background-color: #17a2b8;
|
171 |
+
}
|
172 |
+
input[type="text"],
|
173 |
+
textarea {
|
174 |
+
background: #ffffff;
|
175 |
+
color: #000000;
|
176 |
+
border: solid 1px #ced4da;
|
177 |
+
padding: 10px;
|
178 |
+
font-size: 14px;
|
179 |
+
border-radius: 6px;
|
180 |
}
|
181 |
"""
|
182 |
|
183 |
# Gradio UI
|
184 |
with gr.Blocks(css=custom_css) as interface:
|
185 |
+
gr.Markdown("# π± **Well-being Companion**")
|
186 |
+
gr.Markdown("### Empowering your well-being journey with AI π")
|
187 |
|
188 |
with gr.Row():
|
189 |
user_input = gr.Textbox(lines=2, placeholder="How can I support you today?", label="Your Input")
|
190 |
+
|
191 |
with gr.Row():
|
192 |
submit_button = gr.Button("Submit", elem_id="submit")
|
193 |
|
194 |
with gr.Row():
|
195 |
chatbot_out = gr.Chatbot(label="Chat History")
|
196 |
+
sentiment_out = gr.Textbox(label="Sentiment Analysis")
|
197 |
emotion_out = gr.Textbox(label="Detected Emotion")
|
198 |
|
199 |
with gr.Row():
|