Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -48,7 +48,6 @@ gmaps = googlemaps.Client(key=os.getenv("GOOGLE_API_KEY"))
|
|
48 |
|
49 |
# Helper Functions
|
50 |
def bag_of_words(s, words):
|
51 |
-
"""Convert user input to bag-of-words vector."""
|
52 |
bag = [0] * len(words)
|
53 |
s_words = word_tokenize(s)
|
54 |
s_words = [stemmer.stem(word.lower()) for word in s_words if word.isalnum()]
|
@@ -59,7 +58,6 @@ def bag_of_words(s, words):
|
|
59 |
return np.array(bag)
|
60 |
|
61 |
def generate_chatbot_response(message, history):
|
62 |
-
"""Generate chatbot response and maintain conversation history."""
|
63 |
history = history or []
|
64 |
try:
|
65 |
result = chatbot_model.predict([bag_of_words(message, words)])
|
@@ -75,7 +73,6 @@ def generate_chatbot_response(message, history):
|
|
75 |
return history, response
|
76 |
|
77 |
def analyze_sentiment(user_input):
|
78 |
-
"""Analyze sentiment and map to emojis."""
|
79 |
inputs = tokenizer_sentiment(user_input, return_tensors="pt")
|
80 |
with torch.no_grad():
|
81 |
outputs = model_sentiment(**inputs)
|
@@ -84,7 +81,6 @@ def analyze_sentiment(user_input):
|
|
84 |
return f"Sentiment: {sentiment_map[sentiment_class]}"
|
85 |
|
86 |
def detect_emotion(user_input):
|
87 |
-
"""Detect emotions based on input."""
|
88 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
89 |
result = pipe(user_input)
|
90 |
emotion = result[0]["label"].lower().strip()
|
@@ -96,10 +92,9 @@ def detect_emotion(user_input):
|
|
96 |
"surprise": "Surprise π²",
|
97 |
"neutral": "Neutral π",
|
98 |
}
|
99 |
-
return emotion_map.get(emotion, "Unknown π€"), emotion
|
100 |
|
101 |
def generate_suggestions(emotion):
|
102 |
-
"""Return relevant suggestions based on detected emotions."""
|
103 |
emotion_key = emotion.lower()
|
104 |
suggestions = {
|
105 |
"joy": [
|
@@ -129,7 +124,6 @@ def generate_suggestions(emotion):
|
|
129 |
return suggestions.get(emotion_key, [["No specific suggestions available.", ""]])
|
130 |
|
131 |
def get_health_professionals_and_map(location, query):
|
132 |
-
"""Search nearby healthcare professionals using Google Maps API."""
|
133 |
try:
|
134 |
if not location or not query:
|
135 |
return ["Please provide both location and query."], ""
|
@@ -154,11 +148,11 @@ def get_health_professionals_and_map(location, query):
|
|
154 |
|
155 |
# Main Application Logic
|
156 |
def app_function(user_input, location, query, history):
|
157 |
-
chatbot_history, _ = generate_chatbot_response(user_input, history)
|
158 |
-
sentiment_result = analyze_sentiment(user_input)
|
159 |
-
emotion_result, cleaned_emotion = detect_emotion(user_input)
|
160 |
-
suggestions = generate_suggestions(cleaned_emotion)
|
161 |
-
professionals, map_html = get_health_professionals_and_map(location, query)
|
162 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
163 |
|
164 |
# Gradio Interface
|
@@ -186,6 +180,14 @@ textarea, input {
|
|
186 |
font-size: 1rem;
|
187 |
padding: 10px;
|
188 |
border-radius: 8px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
}
|
190 |
|
191 |
button {
|
@@ -202,12 +204,6 @@ button {
|
|
202 |
button:hover {
|
203 |
box-shadow: 0px 4px 8px rgba(255, 165, 0, 0.5);
|
204 |
}
|
205 |
-
|
206 |
-
.chat-container .human {
|
207 |
-
background: linear-gradient(to right, #ff7f50, orange);
|
208 |
-
color: white;
|
209 |
-
padding: 8px;
|
210 |
-
}
|
211 |
"""
|
212 |
|
213 |
with gr.Blocks(css=custom_css) as app:
|
|
|
48 |
|
49 |
# Helper Functions
|
50 |
def bag_of_words(s, words):
|
|
|
51 |
bag = [0] * len(words)
|
52 |
s_words = word_tokenize(s)
|
53 |
s_words = [stemmer.stem(word.lower()) for word in s_words if word.isalnum()]
|
|
|
58 |
return np.array(bag)
|
59 |
|
60 |
def generate_chatbot_response(message, history):
|
|
|
61 |
history = history or []
|
62 |
try:
|
63 |
result = chatbot_model.predict([bag_of_words(message, words)])
|
|
|
73 |
return history, response
|
74 |
|
75 |
def analyze_sentiment(user_input):
|
|
|
76 |
inputs = tokenizer_sentiment(user_input, return_tensors="pt")
|
77 |
with torch.no_grad():
|
78 |
outputs = model_sentiment(**inputs)
|
|
|
81 |
return f"Sentiment: {sentiment_map[sentiment_class]}"
|
82 |
|
83 |
def detect_emotion(user_input):
|
|
|
84 |
pipe = pipeline("text-classification", model=model_emotion, tokenizer=tokenizer_emotion)
|
85 |
result = pipe(user_input)
|
86 |
emotion = result[0]["label"].lower().strip()
|
|
|
92 |
"surprise": "Surprise π²",
|
93 |
"neutral": "Neutral π",
|
94 |
}
|
95 |
+
return emotion_map.get(emotion, "Unknown π€"), emotion
|
96 |
|
97 |
def generate_suggestions(emotion):
|
|
|
98 |
emotion_key = emotion.lower()
|
99 |
suggestions = {
|
100 |
"joy": [
|
|
|
124 |
return suggestions.get(emotion_key, [["No specific suggestions available.", ""]])
|
125 |
|
126 |
def get_health_professionals_and_map(location, query):
|
|
|
127 |
try:
|
128 |
if not location or not query:
|
129 |
return ["Please provide both location and query."], ""
|
|
|
148 |
|
149 |
# Main Application Logic
|
150 |
def app_function(user_input, location, query, history):
|
151 |
+
chatbot_history, _ = generate_chatbot_response(user_input, history)
|
152 |
+
sentiment_result = analyze_sentiment(user_input)
|
153 |
+
emotion_result, cleaned_emotion = detect_emotion(user_input)
|
154 |
+
suggestions = generate_suggestions(cleaned_emotion)
|
155 |
+
professionals, map_html = get_health_professionals_and_map(location, query)
|
156 |
return chatbot_history, sentiment_result, emotion_result, suggestions, professionals, map_html
|
157 |
|
158 |
# Gradio Interface
|
|
|
180 |
font-size: 1rem;
|
181 |
padding: 10px;
|
182 |
border-radius: 8px;
|
183 |
+
caret-color: white; /* Ensures white cursor when typing */
|
184 |
+
}
|
185 |
+
|
186 |
+
/* Style for focused state to ensure black background and white font */
|
187 |
+
textarea:focus, input:focus {
|
188 |
+
background: black; /* Black background when focused or typing */
|
189 |
+
color: white; /* White text */
|
190 |
+
border: 2px solid orange; /* Reinforce border for focus */
|
191 |
}
|
192 |
|
193 |
button {
|
|
|
204 |
button:hover {
|
205 |
box-shadow: 0px 4px 8px rgba(255, 165, 0, 0.5);
|
206 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
"""
|
208 |
|
209 |
with gr.Blocks(css=custom_css) as app:
|