Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,7 +30,7 @@ with open("intents.json") as file:
|
|
| 30 |
with open("data.pickle", "rb") as f:
|
| 31 |
words, labels, training, output = pickle.load(f)
|
| 32 |
|
| 33 |
-
# Build
|
| 34 |
net = tflearn.input_data(shape=[None, len(training[0])])
|
| 35 |
net = tflearn.fully_connected(net, 8)
|
| 36 |
net = tflearn.fully_connected(net, 8)
|
|
@@ -39,18 +39,18 @@ net = tflearn.regression(net)
|
|
| 39 |
chatbot_model = tflearn.DNN(net)
|
| 40 |
chatbot_model.load("MentalHealthChatBotmodel.tflearn")
|
| 41 |
|
| 42 |
-
#
|
| 43 |
tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 44 |
model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 45 |
|
| 46 |
-
#
|
| 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 |
|
| 50 |
-
# Google Maps API
|
| 51 |
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY'))
|
| 52 |
|
| 53 |
-
# Chatbot
|
| 54 |
def bag_of_words(s, words):
|
| 55 |
bag = [0] * len(words)
|
| 56 |
s_words = word_tokenize(s)
|
|
@@ -77,7 +77,7 @@ def chatbot(message, history):
|
|
| 77 |
history.append((message, response))
|
| 78 |
return history, response
|
| 79 |
|
| 80 |
-
# Sentiment
|
| 81 |
def analyze_sentiment(user_input):
|
| 82 |
inputs = tokenizer_sentiment(user_input, return_tensors="pt")
|
| 83 |
with torch.no_grad():
|
|
@@ -86,14 +86,14 @@ def analyze_sentiment(user_input):
|
|
| 86 |
sentiment_map = ["Negative π", "Neutral π", "Positive π"]
|
| 87 |
return sentiment_map[sentiment_class]
|
| 88 |
|
| 89 |
-
# Emotion
|
| 90 |
def detect_emotion(user_input):
|
| 91 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
| 92 |
result = pipe(user_input)
|
| 93 |
emotion = result[0]["label"]
|
| 94 |
return emotion
|
| 95 |
|
| 96 |
-
# Generate
|
| 97 |
def generate_suggestions(emotion):
|
| 98 |
suggestions = {
|
| 99 |
"joy": [
|
|
@@ -111,7 +111,7 @@ def generate_suggestions(emotion):
|
|
| 111 |
}
|
| 112 |
return suggestions.get(emotion, [["No suggestions available", ""]])
|
| 113 |
|
| 114 |
-
#
|
| 115 |
def get_health_professionals_and_map(location, query):
|
| 116 |
try:
|
| 117 |
geo_location = gmaps.geocode(location)
|
|
@@ -130,16 +130,16 @@ def get_health_professionals_and_map(location, query):
|
|
| 130 |
except Exception as e:
|
| 131 |
return [f"Error: {e}"], ""
|
| 132 |
|
| 133 |
-
# Main
|
| 134 |
def app_function(message, location, query, history):
|
| 135 |
-
chatbot_history, _ = chatbot(message, history)
|
| 136 |
-
sentiment = analyze_sentiment(message)
|
| 137 |
-
emotion = detect_emotion(message.lower())
|
| 138 |
-
suggestions = generate_suggestions(emotion)
|
| 139 |
-
professionals, map_html = get_health_professionals_and_map(location, query)
|
| 140 |
return chatbot_history, sentiment, emotion, suggestions, professionals, map_html
|
| 141 |
|
| 142 |
-
# Enhanced CSS for Black-Themed
|
| 143 |
custom_css = """
|
| 144 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
|
| 145 |
body {
|
|
@@ -181,11 +181,14 @@ textarea, input[type="text"], .gr-chatbot {
|
|
| 181 |
}
|
| 182 |
.gr-dataframe {
|
| 183 |
font-size: 14px;
|
| 184 |
-
height:
|
| 185 |
-
overflow-y: scroll; /*
|
|
|
|
|
|
|
|
|
|
| 186 |
}
|
| 187 |
h1 {
|
| 188 |
-
font-size: 3.5rem;
|
| 189 |
font-weight: bold;
|
| 190 |
margin-bottom: 10px;
|
| 191 |
color: white;
|
|
@@ -207,13 +210,13 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 207 |
with gr.Row():
|
| 208 |
user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
|
| 209 |
user_location = gr.Textbox(label="Your Location", placeholder="Enter your location...")
|
| 210 |
-
search_query = gr.Textbox(label="
|
| 211 |
submit_btn = gr.Button("Submit")
|
| 212 |
|
| 213 |
-
chatbot_box = gr.Chatbot(label="Chat History")
|
| 214 |
emotion_output = gr.Textbox(label="Detected Emotion")
|
| 215 |
sentiment_output = gr.Textbox(label="Detected Sentiment")
|
| 216 |
-
suggestions_output = gr.DataFrame(headers=["Title", "Links"], label="Suggestions") #
|
| 217 |
map_output = gr.HTML(label="Nearby Professionals Map")
|
| 218 |
professional_display = gr.Textbox(label="Nearby Professionals", lines=5)
|
| 219 |
|
|
|
|
| 30 |
with open("data.pickle", "rb") as f:
|
| 31 |
words, labels, training, output = pickle.load(f)
|
| 32 |
|
| 33 |
+
# Build Chatbot Model
|
| 34 |
net = tflearn.input_data(shape=[None, len(training[0])])
|
| 35 |
net = tflearn.fully_connected(net, 8)
|
| 36 |
net = tflearn.fully_connected(net, 8)
|
|
|
|
| 39 |
chatbot_model = tflearn.DNN(net)
|
| 40 |
chatbot_model.load("MentalHealthChatBotmodel.tflearn")
|
| 41 |
|
| 42 |
+
# Sentiment Analysis with Hugging Face
|
| 43 |
tokenizer_sentiment = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 44 |
model_sentiment = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 45 |
|
| 46 |
+
# 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 |
|
| 50 |
+
# Google Maps API Client
|
| 51 |
gmaps = googlemaps.Client(key=os.getenv('GOOGLE_API_KEY'))
|
| 52 |
|
| 53 |
+
# Chatbot Logic
|
| 54 |
def bag_of_words(s, words):
|
| 55 |
bag = [0] * len(words)
|
| 56 |
s_words = word_tokenize(s)
|
|
|
|
| 77 |
history.append((message, response))
|
| 78 |
return history, response
|
| 79 |
|
| 80 |
+
# Sentiment Analysis
|
| 81 |
def analyze_sentiment(user_input):
|
| 82 |
inputs = tokenizer_sentiment(user_input, return_tensors="pt")
|
| 83 |
with torch.no_grad():
|
|
|
|
| 86 |
sentiment_map = ["Negative π", "Neutral π", "Positive π"]
|
| 87 |
return sentiment_map[sentiment_class]
|
| 88 |
|
| 89 |
+
# Emotion Detection
|
| 90 |
def detect_emotion(user_input):
|
| 91 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
| 92 |
result = pipe(user_input)
|
| 93 |
emotion = result[0]["label"]
|
| 94 |
return emotion
|
| 95 |
|
| 96 |
+
# Generate Suggestions
|
| 97 |
def generate_suggestions(emotion):
|
| 98 |
suggestions = {
|
| 99 |
"joy": [
|
|
|
|
| 111 |
}
|
| 112 |
return suggestions.get(emotion, [["No suggestions available", ""]])
|
| 113 |
|
| 114 |
+
# Get Nearby Professionals and Generate Map
|
| 115 |
def get_health_professionals_and_map(location, query):
|
| 116 |
try:
|
| 117 |
geo_location = gmaps.geocode(location)
|
|
|
|
| 130 |
except Exception as e:
|
| 131 |
return [f"Error: {e}"], ""
|
| 132 |
|
| 133 |
+
# App Main Function
|
| 134 |
def app_function(message, location, query, history):
|
| 135 |
+
chatbot_history, _ = chatbot(message, history)
|
| 136 |
+
sentiment = analyze_sentiment(message)
|
| 137 |
+
emotion = detect_emotion(message.lower())
|
| 138 |
+
suggestions = generate_suggestions(emotion)
|
| 139 |
+
professionals, map_html = get_health_professionals_and_map(location, query)
|
| 140 |
return chatbot_history, sentiment, emotion, suggestions, professionals, map_html
|
| 141 |
|
| 142 |
+
# Enhanced CSS for Black-Themed Table and UI
|
| 143 |
custom_css = """
|
| 144 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
|
| 145 |
body {
|
|
|
|
| 181 |
}
|
| 182 |
.gr-dataframe {
|
| 183 |
font-size: 14px;
|
| 184 |
+
height: 400px; /* Larger table */
|
| 185 |
+
overflow-y: scroll; /* Scroll if content exceeds table height */
|
| 186 |
+
background: #000000 !important;
|
| 187 |
+
color: white !important;
|
| 188 |
+
border: 2px solid #ff5722;
|
| 189 |
}
|
| 190 |
h1 {
|
| 191 |
+
font-size: 3.5rem;
|
| 192 |
font-weight: bold;
|
| 193 |
margin-bottom: 10px;
|
| 194 |
color: white;
|
|
|
|
| 210 |
with gr.Row():
|
| 211 |
user_message = gr.Textbox(label="Your Message", placeholder="Enter your message...")
|
| 212 |
user_location = gr.Textbox(label="Your Location", placeholder="Enter your location...")
|
| 213 |
+
search_query = gr.Textbox(label="Query", placeholder="Search for professionals...")
|
| 214 |
submit_btn = gr.Button("Submit")
|
| 215 |
|
| 216 |
+
chatbot_box = gr.Chatbot(label="Chat History")
|
| 217 |
emotion_output = gr.Textbox(label="Detected Emotion")
|
| 218 |
sentiment_output = gr.Textbox(label="Detected Sentiment")
|
| 219 |
+
suggestions_output = gr.DataFrame(headers=["Title", "Links"], label="Suggestions") # Enlarged table
|
| 220 |
map_output = gr.HTML(label="Nearby Professionals Map")
|
| 221 |
professional_display = gr.Textbox(label="Nearby Professionals", lines=5)
|
| 222 |
|