DreamStream-1 commited on
Commit
11851f1
Β·
verified Β·
1 Parent(s): d30f6a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -13,7 +13,7 @@ import googlemaps
13
  import folium
14
  import torch
15
 
16
- # Disable GPU usage for TensorFlow and suppress TensorFlow warnings
17
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
18
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
19
 
@@ -23,7 +23,7 @@ nltk.download("punkt")
23
  # Initialize Lancaster Stemmer
24
  stemmer = LancasterStemmer()
25
 
26
- # Load intents.json and training data for the chatbot
27
  with open("intents.json") as file:
28
  intents_data = json.load(file)
29
 
@@ -39,9 +39,11 @@ net = tflearn.regression(net)
39
  chatbot_model = tflearn.DNN(net)
40
  chatbot_model.load("MentalHealthChatBotmodel.tflearn")
41
 
42
- # Hugging Face models for sentiment and emotion detection
43
  tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
44
  model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
 
 
45
  tokenizer_emotion = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
46
  model_emotion = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
47
 
@@ -60,6 +62,7 @@ def bag_of_words(s, words):
60
  return np.array(bag)
61
 
62
  def chatbot(message, history):
 
63
  history = history or []
64
  try:
65
  results = chatbot_model.predict([bag_of_words(message, words)])
@@ -71,8 +74,9 @@ def chatbot(message, history):
71
  break
72
  except Exception as e:
73
  response = f"Error: {str(e)} πŸ’₯"
74
- history.append({"role": "user", "content": message})
75
- history.append({"role": "assistant", "content": response})
 
76
  return history, response
77
 
78
  # Sentiment analysis
@@ -107,7 +111,7 @@ def generate_suggestions(emotion):
107
  ["Relaxation Video", '<a href="https://youtu.be/MIc299Flibs" target="_blank">Watch</a>'],
108
  ],
109
  }
110
- return suggestions.get(emotion, [["No suggestions available", "", ""]])
111
 
112
  # Search professionals and generate map
113
  def get_health_professionals_and_map(location, query):
@@ -130,17 +134,17 @@ def get_health_professionals_and_map(location, query):
130
 
131
  # Main app function
132
  def app_function(message, location, query, history):
133
- chatbot_history, _ = chatbot(message, history)
134
- sentiment = analyze_sentiment(message)
135
- emotion = detect_emotion(message.lower())
136
- suggestions = generate_suggestions(emotion)
137
- professionals, map_html = get_health_professionals_and_map(location, query)
138
  return chatbot_history, sentiment, emotion, suggestions, professionals, map_html
139
 
140
  # Gradio app interface
141
  with gr.Blocks() as app:
142
  gr.Markdown("# 🌟 Well-Being Companion")
143
- gr.Markdown("Empowering your Well-Being journey πŸ’š")
144
 
145
  with gr.Row():
146
  user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
@@ -148,19 +152,19 @@ with gr.Blocks() as app:
148
  search_query = gr.Textbox(label="Query", placeholder="Search for professionals...")
149
  submit_btn = gr.Button("Submit")
150
 
151
- chatbot_box = gr.Chatbot(label="Chat History")
152
  emotion_output = gr.Textbox(label="Detected Emotion")
153
  sentiment_output = gr.Textbox(label="Detected Sentiment")
154
  suggestions_output = gr.DataFrame(headers=["Title", "Links"], label="Suggestions")
155
  map_output = gr.HTML(label="Nearby Professionals Map")
156
- professional_list = gr.Textbox(label="Nearby Professionals", lines=5)
157
 
158
  submit_btn.click(
159
  app_function,
160
  inputs=[user_message, user_location, search_query, chatbot_box],
161
  outputs=[
162
  chatbot_box, sentiment_output, emotion_output,
163
- suggestions_output, professional_list, map_output,
164
  ],
165
  )
166
 
 
13
  import folium
14
  import torch
15
 
16
+ # Disable GPU usage for TensorFlow
17
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
18
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
19
 
 
23
  # Initialize Lancaster Stemmer
24
  stemmer = LancasterStemmer()
25
 
26
+ # Load chatbot intents and training data
27
  with open("intents.json") as file:
28
  intents_data = json.load(file)
29
 
 
39
  chatbot_model = tflearn.DNN(net)
40
  chatbot_model.load("MentalHealthChatBotmodel.tflearn")
41
 
42
+ # Model for sentiment detection
43
  tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
44
  model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
45
+
46
+ # Model for emotion detection
47
  tokenizer_emotion = AutoTokenizer.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
48
  model_emotion = AutoModelForSequenceClassification.from_pretrained("j-hartmann/emotion-english-distilroberta-base")
49
 
 
62
  return np.array(bag)
63
 
64
  def chatbot(message, history):
65
+ """Generate chatbot response and append to history (as tuples)."""
66
  history = history or []
67
  try:
68
  results = chatbot_model.predict([bag_of_words(message, words)])
 
74
  break
75
  except Exception as e:
76
  response = f"Error: {str(e)} πŸ’₯"
77
+
78
+ # Append the message and response as a tuple
79
+ history.append((message, response))
80
  return history, response
81
 
82
  # Sentiment analysis
 
111
  ["Relaxation Video", '<a href="https://youtu.be/MIc299Flibs" target="_blank">Watch</a>'],
112
  ],
113
  }
114
+ return suggestions.get(emotion, [["No suggestions available", ""]])
115
 
116
  # Search professionals and generate map
117
  def get_health_professionals_and_map(location, query):
 
134
 
135
  # Main app function
136
  def app_function(message, location, query, history):
137
+ chatbot_history, _ = chatbot(message, history) # Generate chatbot response
138
+ sentiment = analyze_sentiment(message) # Detect sentiment
139
+ emotion = detect_emotion(message.lower()) # Detect emotion
140
+ suggestions = generate_suggestions(emotion) # Generate suggestions
141
+ professionals, map_html = get_health_professionals_and_map(location, query) # Find professionals & map
142
  return chatbot_history, sentiment, emotion, suggestions, professionals, map_html
143
 
144
  # Gradio app interface
145
  with gr.Blocks() as app:
146
  gr.Markdown("# 🌟 Well-Being Companion")
147
+ gr.Markdown("Empowering Your Mental Health Journey πŸ’š")
148
 
149
  with gr.Row():
150
  user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
 
152
  search_query = gr.Textbox(label="Query", placeholder="Search for professionals...")
153
  submit_btn = gr.Button("Submit")
154
 
155
+ chatbot_box = gr.Chatbot(label="Chat History") # Corrected history format (list of tuples)
156
  emotion_output = gr.Textbox(label="Detected Emotion")
157
  sentiment_output = gr.Textbox(label="Detected Sentiment")
158
  suggestions_output = gr.DataFrame(headers=["Title", "Links"], label="Suggestions")
159
  map_output = gr.HTML(label="Nearby Professionals Map")
160
+ professional_display = gr.Textbox(label="Nearby Professionals", lines=5)
161
 
162
  submit_btn.click(
163
  app_function,
164
  inputs=[user_message, user_location, search_query, chatbot_box],
165
  outputs=[
166
  chatbot_box, sentiment_output, emotion_output,
167
+ suggestions_output, professional_display, map_output,
168
  ],
169
  )
170