DreamStream-1 commited on
Commit
d7c7798
·
verified ·
1 Parent(s): e859494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -39
app.py CHANGED
@@ -163,6 +163,23 @@ def get_lat_lon(location, api_key):
163
  print(f"Error fetching coordinates: {e}")
164
  return None, None
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  # Get wellness professionals
167
  def get_wellness_professionals(location, api_key):
168
  query = "therapist OR counselor OR mental health professional OR marriage and family therapist OR psychotherapist OR psychiatrist OR psychologist OR nutritionist OR wellness doctor OR holistic practitioner OR integrative medicine OR chiropractor OR naturopath"
@@ -226,53 +243,47 @@ def user_interface(message, location, history, api_key, words, labels, model):
226
  map_html = generate_map(wellness_data)
227
 
228
  # Create a DataFrame for the suggestions
229
- suggestions_df = pd.DataFrame(resources, columns=["Subject", "Article URL"])
230
- suggestions_df["Video URL"] = video_link # Add video URL column
231
- suggestions_html = suggestions_df.to_html(escape=False)
232
 
233
- return history, history, sentiment, emotion, suggestions_html, map_html
234
-
235
- # Load data and model
236
- try:
237
- data = load_intents("intents.json")
238
- except FileNotFoundError:
239
- raise FileNotFoundError("Error: 'intents.json' file not found. Ensure it exists in the current directory.")
 
 
 
 
 
240
 
241
- try:
242
- words, labels, training, output = load_preprocessed_data("data.pickle")
243
- except FileNotFoundError:
244
- raise FileNotFoundError("Error: 'data.pickle' file not found. Ensure it exists and matches the model.")
245
 
 
246
  net = build_model(words, labels, training, output)
247
- try:
248
- model = load_model("MentalHealthChatBotmodel.tflearn", net)
249
- except FileNotFoundError:
250
- raise FileNotFoundError("Error: Trained model file 'MentalHealthChatBotmodel.tflearn' not found.")
251
-
252
- # Gradio chatbot interface
253
- chatbot = gr.Chatbot(label="Mental Health Chatbot")
254
- location_input = gr.Textbox(label="Enter your location (latitude,longitude)", placeholder="e.g., 21.3,-157.8")
255
 
256
- # Gradio interface definition
257
- demo = gr.Interface(
258
- fn=lambda message, location, history: user_interface(message, location, history, os.getenv("GOOGLE_API_KEY"), words, labels, model),
259
  inputs=[
260
- gr.Textbox(label="Message"),
261
- location_input,
262
- "state",
 
 
 
 
263
  ],
264
  outputs=[
265
- chatbot,
266
- "state",
267
- gr.Textbox(label="Sentiment"),
268
- gr.Textbox(label="Emotion"),
269
- gr.HTML(label="Resources"),
270
- gr.HTML(label="Map")
271
  ],
272
- allow_flagging="never",
273
- title="Mental Health & Well-being Assistant"
274
  )
275
 
276
- # Launch Gradio interface
277
- if __name__ == "__main__":
278
- demo.launch(debug=True)
 
163
  print(f"Error fetching coordinates: {e}")
164
  return None, None
165
 
166
+ # Function to fetch places data using Google Places API
167
+ def get_places_data(query, location, radius, api_key):
168
+ places_url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
169
+ params = {
170
+ "query": query,
171
+ "location": location,
172
+ "radius": radius,
173
+ "key": api_key
174
+ }
175
+ try:
176
+ response = requests.get(places_url, params=params)
177
+ response.raise_for_status()
178
+ return response.json()
179
+ except requests.RequestException as e:
180
+ print(f"Error fetching places data: {e}")
181
+ return None
182
+
183
  # Get wellness professionals
184
  def get_wellness_professionals(location, api_key):
185
  query = "therapist OR counselor OR mental health professional OR marriage and family therapist OR psychotherapist OR psychiatrist OR psychologist OR nutritionist OR wellness doctor OR holistic practitioner OR integrative medicine OR chiropractor OR naturopath"
 
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
+ # Load intents and preprocessed data
262
+ data = load_intents('intents.json')
263
+ words, labels, training, output = load_preprocessed_data('data.pkl')
 
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
+ iface = gr.Interface(
271
+ fn=user_interface,
272
  inputs=[
273
+ gr.inputs.Textbox(label="Message"),
274
+ gr.inputs.Textbox(label="Location"),
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)