Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,10 @@ from nltk.stem.lancaster import LancasterStemmer
|
|
14 |
import os
|
15 |
from functools import lru_cache
|
16 |
import pandas as pd
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Ensure necessary NLTK resources are downloaded
|
19 |
nltk.download('punkt')
|
@@ -213,77 +217,22 @@ def generate_map(wellness_data):
|
|
213 |
|
214 |
for place in wellness_data:
|
215 |
name, address, lat, lon = place
|
216 |
-
folium.Marker(
|
217 |
-
location=[lat, lon],
|
218 |
-
popup=f"<b>{name}</b><br>{address}",
|
219 |
-
icon=folium.Icon(color='blue', icon='info-sign')
|
220 |
-
).add_to(m)
|
221 |
|
222 |
-
|
223 |
-
map_html = m._repr_html_()
|
224 |
-
|
225 |
-
return map_html
|
226 |
|
227 |
-
#
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
# Sentiment analysis
|
232 |
-
inputs = tokenizer_sentiment(message, return_tensors="pt")
|
233 |
-
outputs = model_sentiment(**inputs)
|
234 |
-
sentiment = ["Negative", "Neutral", "Positive"][torch.argmax(outputs.logits, dim=1).item()]
|
235 |
-
|
236 |
-
# Emotion detection
|
237 |
-
emotion, resources, video_link = detect_emotion(message)
|
238 |
-
|
239 |
-
# Get wellness professionals
|
240 |
-
wellness_data = get_wellness_professionals(location, api_key)
|
241 |
-
|
242 |
-
# Generate the map
|
243 |
-
map_html = generate_map(wellness_data)
|
244 |
-
|
245 |
-
# Create a DataFrame for the suggestions
|
246 |
-
suggestions_df = pd.DataFrame(resources, columns=["Resource", "Link"])
|
247 |
-
|
248 |
-
# Format the final output
|
249 |
-
output = f"**Chat Response:** {history[-1][1]}\n\n"
|
250 |
-
output += f"**Sentiment:** {sentiment}\n\n"
|
251 |
-
output += f"**Detected Emotion:** {emotion}\n\n"
|
252 |
-
output += "**Suggestions based on Emotion:**\n"
|
253 |
-
for resource in resources:
|
254 |
-
output += f"- [{resource[0]}]({resource[1]})\n"
|
255 |
-
output += f"\n**Video Suggestion:** {video_link}\n\n"
|
256 |
-
output += "**Wellness Professionals near you:**\n"
|
257 |
-
output += map_html
|
258 |
-
|
259 |
-
return output, history
|
260 |
|
261 |
-
#
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
# Build and load the model
|
266 |
-
net = build_model(words, labels, training, output)
|
267 |
-
model = load_model('model.tflearn', net)
|
268 |
|
269 |
# Gradio interface
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
gr.State([]), # History
|
276 |
-
gr.State(os.getenv("GOOGLE_API_KEY")), # API Key
|
277 |
-
gr.State(words),
|
278 |
-
gr.State(labels),
|
279 |
-
gr.State(model)
|
280 |
-
],
|
281 |
-
outputs=[
|
282 |
-
gr.outputs.Markdown(label="Response"),
|
283 |
-
gr.outputs.Chatbot(label="Chat History")
|
284 |
-
],
|
285 |
-
title="Wellness Chatbot",
|
286 |
-
description="A chatbot to provide wellness support and locate professionals near you."
|
287 |
-
)
|
288 |
|
289 |
-
iface.launch(debug=True)
|
|
|
14 |
import os
|
15 |
from functools import lru_cache
|
16 |
import pandas as pd
|
17 |
+
import tensorflow as tf # Added to enable resource variables
|
18 |
+
|
19 |
+
# Enable resource variables in TensorFlow to avoid deprecated warnings
|
20 |
+
tf.compat.v1.enable_resource_variables()
|
21 |
|
22 |
# Ensure necessary NLTK resources are downloaded
|
23 |
nltk.download('punkt')
|
|
|
217 |
|
218 |
for place in wellness_data:
|
219 |
name, address, lat, lon = place
|
220 |
+
folium.Marker([lat, lon], popup=f"{name}\n{address}").add_to(m)
|
|
|
|
|
|
|
|
|
221 |
|
222 |
+
return m
|
|
|
|
|
|
|
223 |
|
224 |
+
# Initialize the necessary files
|
225 |
+
data = load_intents("intents.json")
|
226 |
+
words, labels, training, output = load_preprocessed_data("data.pickle")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
+
# Build the model
|
229 |
+
model = build_model(words, labels, training, output)
|
230 |
+
model = load_model("model.tflearn", model)
|
|
|
|
|
|
|
|
|
231 |
|
232 |
# Gradio interface
|
233 |
+
def chatbot_interface(message, history):
|
234 |
+
return chat(message, history, words, labels, model)
|
235 |
+
|
236 |
+
# Example usage with Gradio UI
|
237 |
+
gr.Interface(fn=chatbot_interface, inputs=["text", "state"], outputs=["chatbot", "state"]).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
|
|