Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -120,96 +120,214 @@ def calculate_metrics(status, pre_weight, current_weight, height,
|
|
| 120 |
return "Select pregnancy status"
|
| 121 |
|
| 122 |
def chat_function(user_input, image, chat_history):
|
| 123 |
-
"""
|
| 124 |
history_str = format_history(chat_history)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
if any(term in user_input.lower() for term in crisis_terms):
|
| 132 |
-
return chat_history + [(user_input, crisis_response)]
|
| 133 |
-
|
| 134 |
-
# Process image or text input
|
| 135 |
if image:
|
| 136 |
-
|
| 137 |
-
if
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
else:
|
| 140 |
-
|
| 141 |
else:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
{history_str}
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
with gr.Row():
|
| 173 |
-
with gr.Column(scale=2):
|
| 174 |
-
chatbot = gr.Chatbot(height=500)
|
| 175 |
-
input_txt = gr.Textbox(placeholder="Ask about pregnancy health...")
|
| 176 |
-
input_img = gr.Image(type="pil", label="Upload Food Image")
|
| 177 |
-
btn_send = gr.Button("Send", variant="primary")
|
| 178 |
-
|
| 179 |
with gr.Column(scale=1):
|
| 180 |
-
gr.Markdown("##
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
pre_weight = gr.Number(label="Pre-pregnancy Weight (kg)")
|
| 183 |
current_weight = gr.Number(label="Current Weight (kg)")
|
| 184 |
height = gr.Number(label="Height (cm)")
|
| 185 |
-
gest_age = gr.Number(visible=False)
|
| 186 |
-
|
| 187 |
-
breastfeeding = gr.Radio(["Yes", "No"], visible=False)
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
gr.update(visible=
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
calculate_metrics,
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
demo.launch(debug=False)
|
|
|
|
| 120 |
return "Select pregnancy status"
|
| 121 |
|
| 122 |
def chat_function(user_input, image, chat_history):
|
| 123 |
+
"""Generate responses based on user input and chat history."""
|
| 124 |
history_str = format_history(chat_history)
|
| 125 |
+
crisis_keywords = [
|
| 126 |
+
"suicide", "self-harm", "kill myself", "cutting", "hurt myself", "end my life",
|
| 127 |
+
"hopeless", "worthless", "can’t go on", "panic attack", "feel like dying"
|
| 128 |
+
]
|
| 129 |
+
newborn_keywords = ["newborn", "baby", "infant", "feeding", "sleep", "colic"]
|
| 130 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
if image:
|
| 132 |
+
food_name, confidence = classify_food(image)
|
| 133 |
+
if food_name:
|
| 134 |
+
if user_input:
|
| 135 |
+
prompt = f"""
|
| 136 |
+
Previous conversation:
|
| 137 |
+
{history_str}
|
| 138 |
+
|
| 139 |
+
The user uploaded an image of {food_name} and asked: '{user_input}'.
|
| 140 |
+
|
| 141 |
+
Provide a response tailored to pregnancy or postpartum needs.
|
| 142 |
+
"""
|
| 143 |
+
else:
|
| 144 |
+
prompt = f"""
|
| 145 |
+
Previous conversation:
|
| 146 |
+
{history_str}
|
| 147 |
+
|
| 148 |
+
The user uploaded an image of {food_name}.
|
| 149 |
+
|
| 150 |
+
Provide pregnancy-specific nutritional advice.
|
| 151 |
+
"""
|
| 152 |
+
response = llm.invoke(prompt).content
|
| 153 |
else:
|
| 154 |
+
response = "I couldn’t identify a food item in the image. Please upload a clearer picture."
|
| 155 |
else:
|
| 156 |
+
if not user_input.strip():
|
| 157 |
+
response = "Please type a message or upload an image."
|
| 158 |
+
elif any(keyword in user_input.lower() for keyword in crisis_keywords):
|
| 159 |
+
response = """
|
| 160 |
+
I'm really sorry you're feeling this way. You’re not alone, and help is available.
|
| 161 |
+
Please reach out to someone you trust or contact a helpline:
|
| 162 |
+
|
| 163 |
+
- 🇮🇳 India: Vandrevala Foundation - 1860 266 2345
|
| 164 |
+
- 🇺🇸 USA: National Suicide Prevention Lifeline - 988
|
| 165 |
+
- 🇬🇧 UK: Samaritans - 116 123
|
| 166 |
+
- 🌍 International: https://findahelpline.com/
|
| 167 |
|
| 168 |
+
If you’re in immediate danger, call emergency services (911/112).
|
| 169 |
+
"""
|
| 170 |
+
elif any(keyword in user_input.lower() for keyword in newborn_keywords):
|
| 171 |
+
prompt = f"""
|
| 172 |
+
Previous conversation:
|
| 173 |
{history_str}
|
| 174 |
|
| 175 |
+
The user asked: '{user_input}'.
|
| 176 |
+
|
| 177 |
+
Provide basic guidance on newborn care.
|
| 178 |
+
"""
|
| 179 |
+
response = llm.invoke(prompt).content
|
| 180 |
+
else:
|
| 181 |
+
docs = retriever.get_relevant_documents(user_input)
|
| 182 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 183 |
+
prompt = f"""
|
| 184 |
+
Previous conversation:
|
| 185 |
+
{history_str}
|
| 186 |
+
|
| 187 |
+
Context: {context}
|
| 188 |
+
|
| 189 |
+
Current question: {user_input}
|
| 190 |
+
|
| 191 |
+
Assistant:
|
| 192 |
+
"""
|
| 193 |
+
response = llm.invoke(prompt).content
|
| 194 |
+
|
| 195 |
+
chat_history.append((user_input or "[Image Uploaded]", response))
|
| 196 |
+
return chat_history
|
| 197 |
+
|
| 198 |
+
# Custom CSS with specified colors
|
| 199 |
+
custom_css = """
|
| 200 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap');
|
| 201 |
+
|
| 202 |
+
/* General layout */
|
| 203 |
+
.gradio-container {
|
| 204 |
+
background: #F1D3B2; /* Light peach background */
|
| 205 |
+
font-family: 'Roboto', sans-serif; /* Modern, readable font */
|
| 206 |
+
padding: 20px; /* Add breathing room */
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
/* Chatbot bubble styling */
|
| 210 |
+
.chatbot .bubble {
|
| 211 |
+
border-radius: 15px;
|
| 212 |
+
padding: 10px 15px;
|
| 213 |
+
margin: 8px;
|
| 214 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1); /* Subtle shadow */
|
| 215 |
+
}
|
| 216 |
+
.chatbot .bubble:nth-child(odd) {
|
| 217 |
+
background: #F1D3B2; /* Light peach for assistant */
|
| 218 |
+
color: #46211A; /* Dark brown text for contrast */
|
| 219 |
+
}
|
| 220 |
+
.chatbot .bubble:nth-child(even) {
|
| 221 |
+
background: #D9B08C; /* Slightly darker peach for user */
|
| 222 |
+
color: #46211A;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
/* Buttons */
|
| 226 |
+
button {
|
| 227 |
+
border-radius: 10px !important;
|
| 228 |
+
padding: 10px 20px !important;
|
| 229 |
+
font-size: 16px !important;
|
| 230 |
+
transition: all 0.3s ease !important;
|
| 231 |
+
}
|
| 232 |
+
button.primary {
|
| 233 |
+
background: #A43820 !important; /* Rusty orange for primary actions */
|
| 234 |
+
color: #F1D3B2 !important; /* Light peach text */
|
| 235 |
+
}
|
| 236 |
+
button.primary:hover {
|
| 237 |
+
background: #8B2E18 !important; /* Darker orange on hover */
|
| 238 |
+
}
|
| 239 |
+
button.secondary {
|
| 240 |
+
background: #46211A !important; /* Dark brown for secondary actions */
|
| 241 |
+
color: #F1D3B2 !important;
|
| 242 |
+
}
|
| 243 |
+
button.secondary:hover {
|
| 244 |
+
background: #301510 !important; /* Darker brown on hover */
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
/* Textbox */
|
| 248 |
+
textarea {
|
| 249 |
+
border-radius: 10px !important;
|
| 250 |
+
border: 1px solid #46211A !important; /* Dark brown border */
|
| 251 |
+
padding: 10px !important;
|
| 252 |
+
background: #FFFFFF !important; /* White background for clarity */
|
| 253 |
+
color: #46211A !important;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
/* Input fields */
|
| 257 |
+
.number-input, .radio {
|
| 258 |
+
background: #FFFFFF !important;
|
| 259 |
+
border-radius: 10px !important;
|
| 260 |
+
border: 1px solid #46211A !important;
|
| 261 |
+
padding: 10px !important;
|
| 262 |
+
color: #46211A !important;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
/* Disclaimer styling */
|
| 266 |
+
.disclaimer {
|
| 267 |
+
font-size: 12px;
|
| 268 |
+
color: #46211A; /* Dark brown for readability */
|
| 269 |
+
text-align: center;
|
| 270 |
+
}
|
| 271 |
+
"""
|
| 272 |
+
|
| 273 |
+
# Gradio interface
|
| 274 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 275 |
+
gr.Markdown("# 🌸 Maternal Wellness Companion 🌸")
|
| 276 |
+
gr.Markdown("""Welcome! I'm here to support you through pregnancy and postpartum with advice on mental health, nutrition, fitness, and newborn care. Ask me anything or upload a food image!""")
|
| 277 |
+
|
| 278 |
+
chatbot = gr.Chatbot(
|
| 279 |
+
height=600,
|
| 280 |
+
label="Conversation",
|
| 281 |
+
value=[[None, "Welcome! I'm here to support you through pregnancy and postpartum. Ask me anything or upload a food image for nutritional advice."]]
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
with gr.Column(scale=1):
|
| 286 |
+
gr.Markdown("## Chat with Me")
|
| 287 |
+
msg = gr.Textbox(label="Your Message", placeholder="Type your question here...")
|
| 288 |
+
img = gr.Image(label="Upload Food Image", type="pil")
|
| 289 |
+
send_btn = gr.Button("Send")
|
| 290 |
+
|
| 291 |
+
with gr.Column(scale=1):
|
| 292 |
+
gr.Markdown("## Pregnancy Metrics")
|
| 293 |
+
status = gr.Radio(["Pregnant", "Postpartum"], label="Your Status")
|
| 294 |
pre_weight = gr.Number(label="Pre-pregnancy Weight (kg)")
|
| 295 |
current_weight = gr.Number(label="Current Weight (kg)")
|
| 296 |
height = gr.Number(label="Height (cm)")
|
| 297 |
+
gest_age = gr.Number(label="Gestational Age (weeks)", visible=False)
|
| 298 |
+
time_since_delivery = gr.Number(label="Time Since Delivery (weeks)", visible=False)
|
| 299 |
+
breastfeeding = gr.Radio(["Yes", "No"], label="Breastfeeding?", visible=False)
|
| 300 |
+
calc_btn = gr.Button("Calculate Metrics")
|
| 301 |
+
|
| 302 |
+
with gr.Row():
|
| 303 |
+
clear_btn = gr.Button("Clear Chat")
|
| 304 |
+
|
| 305 |
+
def update_visibility(status):
|
| 306 |
+
if status == "Pregnant":
|
| 307 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 308 |
+
elif status == "Postpartum":
|
| 309 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
| 310 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 311 |
+
|
| 312 |
+
status.change(update_visibility, inputs=status, outputs=[gest_age, time_since_delivery, breastfeeding])
|
| 313 |
+
|
| 314 |
+
def handle_send(msg, img, chat_history):
|
| 315 |
+
chat_history = chat_function(msg, img, chat_history)
|
| 316 |
+
return "", None, chat_history
|
| 317 |
+
|
| 318 |
+
send_btn.click(handle_send, inputs=[msg, img, chatbot], outputs=[msg, img, chatbot])
|
| 319 |
+
|
| 320 |
+
def handle_calc(status, pre_weight, current_weight, height, gest_age, time_since_delivery, breastfeeding, chat_history):
|
| 321 |
+
metrics_response = calculate_metrics(status, pre_weight, current_weight, height, gest_age, time_since_delivery, breastfeeding)
|
| 322 |
+
chat_history.append(("Pregnancy Metrics Calculation", metrics_response))
|
| 323 |
+
return chat_history
|
| 324 |
+
|
| 325 |
+
calc_btn.click(handle_calc,
|
| 326 |
+
inputs=[status, pre_weight, current_weight, height, gest_age, time_since_delivery, breastfeeding, chatbot],
|
| 327 |
+
outputs=chatbot)
|
| 328 |
+
|
| 329 |
+
clear_btn.click(lambda: [], outputs=chatbot)
|
| 330 |
+
|
| 331 |
+
gr.HTML('<div class="disclaimer">**Disclaimer**: This app offers general guidance and is not a substitute for professional medical advice. Consult your healthcare provider for personalized recommendations.</div>')
|
| 332 |
|
| 333 |
demo.launch(debug=False)
|