mohitrajdeo
commited on
Commit
Β·
2f83400
1
Parent(s):
e3681ad
Implement Medical Consultant feature with health-related queries and suggestions
Browse files
app.py
CHANGED
|
@@ -456,100 +456,100 @@ if selected == 'Data Visualization':
|
|
| 456 |
st.pyplot(fig)
|
| 457 |
|
| 458 |
|
| 459 |
-
if selected == 'Medical Consultant':
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
|
| 551 |
-
|
| 552 |
-
|
| 553 |
|
| 554 |
|
| 555 |
# if selected == 'Checkbox-to-disease-predictor':
|
|
@@ -957,4 +957,198 @@ if selected=='Hypertension Prediction':
|
|
| 957 |
|
| 958 |
except Exception as e:
|
| 959 |
st.error(f"Error during prediction: {e}")
|
| 960 |
-
# st.info("Please check that all inputs are valid and within the expected ranges.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
st.pyplot(fig)
|
| 457 |
|
| 458 |
|
| 459 |
+
# if selected == 'Medical Consultant':
|
| 460 |
+
# st.title("π©Ί Medical Consultant Chatbot")
|
| 461 |
+
# st.markdown("### Discuss Your Health Concerns with Our AI-powered Chatbot")
|
| 462 |
+
# st.write("Ask about **Diabetes, Asthma, Stroke, Cardiovascular Disease, or Mental Health.**")
|
| 463 |
+
|
| 464 |
+
# genai.configure(api_key="AIzaSyAwyi9c5OdvLoWrv5lFi1jZDEYwuprQAKE")
|
| 465 |
+
|
| 466 |
+
# # Custom Styling
|
| 467 |
+
# st.markdown("""
|
| 468 |
+
# <style>
|
| 469 |
+
# .prompt-box {
|
| 470 |
+
# background-color: #000000;
|
| 471 |
+
# padding: 12px;
|
| 472 |
+
# border-radius: 8px;
|
| 473 |
+
# font-size: 14px;
|
| 474 |
+
# font-family: sans-serif;
|
| 475 |
+
# margin-bottom: 10px;
|
| 476 |
+
# border: 1px solid #dee2e6;
|
| 477 |
+
# text-align: center;
|
| 478 |
+
# }
|
| 479 |
+
# </style>
|
| 480 |
+
# """, unsafe_allow_html=True)
|
| 481 |
+
|
| 482 |
+
# st.markdown("#### π‘ Common Health Queries")
|
| 483 |
+
|
| 484 |
+
# prompt_options = [
|
| 485 |
+
# ("Diabetes β Diet", "What foods should I eat if I have diabetes?"),
|
| 486 |
+
# ("Diabetes β Exercise", "What type of workouts help control blood sugar levels?"),
|
| 487 |
+
# ("Asthma β Triggers", "What are common asthma triggers?"),
|
| 488 |
+
# ("Asthma β Treatment", "What are the best medications for asthma?"),
|
| 489 |
+
# ("Stroke β Symptoms", "What are the early warning signs of a stroke?"),
|
| 490 |
+
# ("Stroke β Prevention", "How can I reduce my risk of stroke?"),
|
| 491 |
+
# ("Cardiovascular β Heart Health", "How can I reduce my risk of heart disease?"),
|
| 492 |
+
# ("Cardiovascular β Blood Pressure", "What lifestyle changes can lower high blood pressure?"),
|
| 493 |
+
# ("Mental Health β Stress Management", "How can I manage stress effectively?"),
|
| 494 |
+
# ("Mental Health β Sleep Disorders", "What are the causes and treatments for sleep disorders?")
|
| 495 |
+
# ]
|
| 496 |
+
|
| 497 |
+
# # Display prompts in two columns (2 prompts per row)
|
| 498 |
+
# cols = st.columns(2)
|
| 499 |
+
# for i in range(0, len(prompt_options), 2):
|
| 500 |
+
# with cols[0]:
|
| 501 |
+
# if i < len(prompt_options):
|
| 502 |
+
# label, prompt = prompt_options[i]
|
| 503 |
+
# st.markdown(f"""<div class="prompt-box"><strong>{label}</strong><br>{prompt}</div>""", unsafe_allow_html=True)
|
| 504 |
+
|
| 505 |
+
# with cols[1]:
|
| 506 |
+
# if i+1 < len(prompt_options):
|
| 507 |
+
# label, prompt = prompt_options[i+1]
|
| 508 |
+
# st.markdown(f"""<div class="prompt-box"><strong>{label}</strong><br>{prompt}</div>""", unsafe_allow_html=True)
|
| 509 |
+
|
| 510 |
+
# # Initialize chat history if not present
|
| 511 |
+
# if "chat_history" not in st.session_state:
|
| 512 |
+
# st.session_state.chat_history = []
|
| 513 |
+
|
| 514 |
+
# # Display previous chat history
|
| 515 |
+
# for message in st.session_state.chat_history:
|
| 516 |
+
# with st.chat_message(message["role"]):
|
| 517 |
+
# st.markdown(message["content"])
|
| 518 |
+
|
| 519 |
+
# # User input field
|
| 520 |
+
# user_prompt = st.chat_input("Ask about Diabetes, Asthma, Stroke, Cardiovascular Disease, or Mental Health...")
|
| 521 |
+
|
| 522 |
+
# # List of allowed topics
|
| 523 |
+
# allowed_keywords = ["diabetes", "asthma", "stroke", "cardiovascular", "heart", "blood pressure",
|
| 524 |
+
# "mental health", "depression", "stress", "cholesterol", "sleep disorders"]
|
| 525 |
+
|
| 526 |
+
# if user_prompt:
|
| 527 |
+
# # Display user message
|
| 528 |
+
# st.chat_message("user").markdown(user_prompt)
|
| 529 |
+
# st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 530 |
+
|
| 531 |
+
# # Restriction: Only process if related to health topics
|
| 532 |
+
# if any(keyword in user_prompt.lower() for keyword in allowed_keywords):
|
| 533 |
+
# model = genai.GenerativeModel("gemini-2.0-flash")
|
| 534 |
+
# response = model.generate_content(user_prompt)
|
| 535 |
+
|
| 536 |
+
# if response and hasattr(response, "text"):
|
| 537 |
+
# assistant_response = response.text
|
| 538 |
+
# else:
|
| 539 |
+
# assistant_response = "I'm sorry, I couldn't generate a response."
|
| 540 |
+
|
| 541 |
+
# st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 542 |
+
|
| 543 |
+
# # Display assistant's response
|
| 544 |
+
# with st.chat_message("assistant"):
|
| 545 |
+
# st.markdown(assistant_response)
|
| 546 |
+
# else:
|
| 547 |
+
# # Restriction message
|
| 548 |
+
# restriction_msg = "**β οΈ This chatbot only responds to health-related topics.**\nPlease ask about Diabetes, Asthma, Stroke, Cardiovascular Disease, or Mental Health."
|
| 549 |
+
# st.session_state.chat_history.append({"role": "assistant", "content": restriction_msg})
|
| 550 |
|
| 551 |
+
# with st.chat_message("assistant"):
|
| 552 |
+
# st.markdown(restriction_msg)
|
| 553 |
|
| 554 |
|
| 555 |
# if selected == 'Checkbox-to-disease-predictor':
|
|
|
|
| 957 |
|
| 958 |
except Exception as e:
|
| 959 |
st.error(f"Error during prediction: {e}")
|
| 960 |
+
# st.info("Please check that all inputs are valid and within the expected ranges.")
|
| 961 |
+
|
| 962 |
+
|
| 963 |
+
if selected == 'Medical Consultant':
|
| 964 |
+
st.title("π©Ί Medical Consultant Chatbot")
|
| 965 |
+
st.markdown("### Discuss Your Health Concerns with Our AI-powered Chatbot")
|
| 966 |
+
st.write("Our AI can help with **medical questions, symptom analysis, and health recommendations**.")
|
| 967 |
+
|
| 968 |
+
# Initialize API
|
| 969 |
+
genai.configure(api_key="AIzaSyAcXexC7cNXrRTCYj6Dg7ZFYVQZH8a5PMw") # Replace with your actual API key
|
| 970 |
+
|
| 971 |
+
# Custom Styling for suggestions
|
| 972 |
+
st.markdown("""
|
| 973 |
+
<style>
|
| 974 |
+
.prompt-box {
|
| 975 |
+
background-color: #222222;
|
| 976 |
+
padding: 12px;
|
| 977 |
+
border-radius: 8px;
|
| 978 |
+
font-size: 14px;
|
| 979 |
+
font-family: sans-serif;
|
| 980 |
+
margin-bottom: 10px;
|
| 981 |
+
border: 1px solid #444444;
|
| 982 |
+
text-align: center;
|
| 983 |
+
cursor: pointer;
|
| 984 |
+
}
|
| 985 |
+
.prompt-box:hover {
|
| 986 |
+
background-color: #333333;
|
| 987 |
+
}
|
| 988 |
+
</style>
|
| 989 |
+
""", unsafe_allow_html=True)
|
| 990 |
+
|
| 991 |
+
# Common medical questions as suggestions
|
| 992 |
+
st.markdown("#### π‘ Common Health Questions")
|
| 993 |
+
|
| 994 |
+
prompt_options = [
|
| 995 |
+
("Diabetes", "What are early warning signs of diabetes?"),
|
| 996 |
+
("Hypertension", "How can I manage my blood pressure naturally?"),
|
| 997 |
+
("Heart Health", "What lifestyle changes help reduce cardiovascular risk?"),
|
| 998 |
+
("Asthma", "What triggers asthma attacks and how can I prevent them?"),
|
| 999 |
+
("Stroke", "What are the warning signs of a stroke?"),
|
| 1000 |
+
("Sleep Health", "How does poor sleep affect my overall health?"),
|
| 1001 |
+
("Mental Health", "What are common symptoms of anxiety?"),
|
| 1002 |
+
("Preventive Care", "What preventive screenings should I get at my age?"),
|
| 1003 |
+
("Exercise", "How much exercise do I need for good health?"),
|
| 1004 |
+
("Nutrition", "What diet changes can improve my heart health?")
|
| 1005 |
+
]
|
| 1006 |
+
|
| 1007 |
+
# Display prompts in two columns
|
| 1008 |
+
cols = st.columns(2)
|
| 1009 |
+
for i in range(0, len(prompt_options), 2):
|
| 1010 |
+
with cols[0]:
|
| 1011 |
+
if i < len(prompt_options):
|
| 1012 |
+
label, prompt = prompt_options[i]
|
| 1013 |
+
st.markdown(f"""<div class="prompt-box" onclick="document.querySelector('#medical-chat-input').value='{prompt}';"><strong>{label}</strong><br>{prompt}</div>""", unsafe_allow_html=True)
|
| 1014 |
+
|
| 1015 |
+
with cols[1]:
|
| 1016 |
+
if i+1 < len(prompt_options):
|
| 1017 |
+
label, prompt = prompt_options[i+1]
|
| 1018 |
+
st.markdown(f"""<div class="prompt-box" onclick="document.querySelector('#medical-chat-input').value='{prompt}';"><strong>{label}</strong><br>{prompt}</div>""", unsafe_allow_html=True)
|
| 1019 |
+
|
| 1020 |
+
# Initialize chat history if not present
|
| 1021 |
+
if "medical_chat_history" not in st.session_state:
|
| 1022 |
+
st.session_state.medical_chat_history = []
|
| 1023 |
+
# Add welcome message
|
| 1024 |
+
welcome_msg = {
|
| 1025 |
+
"role": "assistant",
|
| 1026 |
+
"content": """π Welcome to your Medical Consultant! I can help answer questions about:
|
| 1027 |
+
|
| 1028 |
+
- Health concerns and symptoms
|
| 1029 |
+
- Disease prevention and management
|
| 1030 |
+
- Lifestyle recommendations
|
| 1031 |
+
- Understanding medical conditions
|
| 1032 |
+
|
| 1033 |
+
How can I assist with your health questions today?"""
|
| 1034 |
+
}
|
| 1035 |
+
st.session_state.medical_chat_history.append(welcome_msg)
|
| 1036 |
+
|
| 1037 |
+
# Chat container
|
| 1038 |
+
chat_container = st.container()
|
| 1039 |
+
with chat_container:
|
| 1040 |
+
# Display previous chat history
|
| 1041 |
+
for message in st.session_state.medical_chat_history:
|
| 1042 |
+
with st.chat_message(message["role"]):
|
| 1043 |
+
st.markdown(message["content"])
|
| 1044 |
+
|
| 1045 |
+
# User input field
|
| 1046 |
+
user_prompt = st.chat_input("Ask about health concerns, symptoms, or lifestyle questions...", key="medical-chat-input")
|
| 1047 |
+
|
| 1048 |
+
# Define medical topics for feature recommendations
|
| 1049 |
+
medical_topics = {
|
| 1050 |
+
"diabetes": "Diabetes Prediction",
|
| 1051 |
+
"blood sugar": "Diabetes Prediction",
|
| 1052 |
+
"hypertension": "Hypertension Prediction",
|
| 1053 |
+
"blood pressure": "Hypertension Prediction",
|
| 1054 |
+
"heart": "Cardiovascular Disease Prediction",
|
| 1055 |
+
"cardiovascular": "Cardiovascular Disease Prediction",
|
| 1056 |
+
"asthma": "Asthma Prediction",
|
| 1057 |
+
"breathing": "Asthma Prediction",
|
| 1058 |
+
"stroke": "Stroke Prediction",
|
| 1059 |
+
"sleep": "Sleep Health Analysis",
|
| 1060 |
+
"insomnia": "Sleep Health Analysis",
|
| 1061 |
+
"mental health": "Mental-Analysis",
|
| 1062 |
+
"depression": "Mental-Analysis",
|
| 1063 |
+
"anxiety": "Mental-Analysis",
|
| 1064 |
+
"stress": "Mental-Analysis"
|
| 1065 |
+
}
|
| 1066 |
+
|
| 1067 |
+
if user_prompt:
|
| 1068 |
+
# Add user message to chat
|
| 1069 |
+
st.session_state.medical_chat_history.append({"role": "user", "content": user_prompt})
|
| 1070 |
+
|
| 1071 |
+
# Display user message in chat
|
| 1072 |
+
with st.chat_message("user"):
|
| 1073 |
+
st.markdown(user_prompt)
|
| 1074 |
+
|
| 1075 |
+
try:
|
| 1076 |
+
# Create system instruction
|
| 1077 |
+
system_instruction = """You are a medical consultant chatbot designed to provide helpful health information.
|
| 1078 |
+
|
| 1079 |
+
RULES:
|
| 1080 |
+
- Provide accurate, concise medical information based on current scientific understanding
|
| 1081 |
+
- Answer questions about symptoms, diseases, prevention, and health management
|
| 1082 |
+
- Keep responses informative but brief (under 150 words)
|
| 1083 |
+
- When uncertain, acknowledge limitations and recommend consulting a healthcare professional
|
| 1084 |
+
- Avoid making definitive diagnoses or treatment recommendations
|
| 1085 |
+
- Never claim to be an AI or language model - respond directly as a medical consultant
|
| 1086 |
+
- Always clarify that your advice is informational and not a substitute for professional medical care
|
| 1087 |
+
- When describing medical conditions, focus on factual information about symptoms, risk factors, and prevention
|
| 1088 |
+
- Maintain a professional, empathetic tone
|
| 1089 |
+
- If the user mentions specific symptoms, acknowledge them and provide information about possible causes
|
| 1090 |
+
- Respond in a doctor-like manner when assessing symptoms or risk factors
|
| 1091 |
+
- Use your knowledge to identify if the user's query relates to any specific medical conditions
|
| 1092 |
+
- Do not suggest our prediction tools in every response - only when truly relevant
|
| 1093 |
+
|
| 1094 |
+
The user is interacting with a health prediction platform that offers the following tools:
|
| 1095 |
+
- Diabetes Prediction
|
| 1096 |
+
- Hypertension Prediction
|
| 1097 |
+
- Cardiovascular Disease Prediction
|
| 1098 |
+
- Asthma Prediction
|
| 1099 |
+
- Stroke Prediction
|
| 1100 |
+
- Sleep Health Analysis
|
| 1101 |
+
- Mental Health Analysis
|
| 1102 |
+
|
| 1103 |
+
TASK: First, determine if the user's query contains symptoms or mentions specific health conditions. If so, provide a doctor-like assessment. Only if appropriate, subtly suggest one of our health prediction tools at the end of your response.
|
| 1104 |
+
"""
|
| 1105 |
+
|
| 1106 |
+
# Generate a response using Gemini
|
| 1107 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 1108 |
+
|
| 1109 |
+
# Prepare chat context
|
| 1110 |
+
chat_context = []
|
| 1111 |
+
for msg in st.session_state.medical_chat_history[-5:]: # Last 5 messages for context
|
| 1112 |
+
if msg["role"] == "user":
|
| 1113 |
+
chat_context.append(f"User: {msg['content']}")
|
| 1114 |
+
else:
|
| 1115 |
+
chat_context.append(f"Medical Consultant: {msg['content']}")
|
| 1116 |
+
|
| 1117 |
+
# Add current query with additional analysis request
|
| 1118 |
+
full_prompt = f"""{system_instruction}
|
| 1119 |
+
|
| 1120 |
+
CONVERSATION HISTORY:
|
| 1121 |
+
{chr(10).join(chat_context)}
|
| 1122 |
+
|
| 1123 |
+
USER QUERY: {user_prompt}
|
| 1124 |
+
|
| 1125 |
+
ANALYSIS INSTRUCTIONS:
|
| 1126 |
+
1. First, determine if this query relates to any specific health conditions or symptoms
|
| 1127 |
+
2. Provide a helpful medical response addressing the user's concerns
|
| 1128 |
+
3. If appropriate, subtly suggest one relevant prediction tool at the end of your response (only if truly related)
|
| 1129 |
+
4. Remember to be professional and avoid making definitive diagnoses
|
| 1130 |
+
"""
|
| 1131 |
+
|
| 1132 |
+
# Generate response
|
| 1133 |
+
response = model.generate_content(full_prompt)
|
| 1134 |
+
|
| 1135 |
+
if response and hasattr(response, "text"):
|
| 1136 |
+
assistant_response = response.text
|
| 1137 |
+
else:
|
| 1138 |
+
assistant_response = "I'm sorry, I couldn't generate a response. Please try asking a different health-related question."
|
| 1139 |
+
|
| 1140 |
+
# Save and display response
|
| 1141 |
+
st.session_state.medical_chat_history.append({"role": "assistant", "content": assistant_response})
|
| 1142 |
+
|
| 1143 |
+
with st.chat_message("assistant"):
|
| 1144 |
+
st.markdown(assistant_response)
|
| 1145 |
+
|
| 1146 |
+
except Exception as e:
|
| 1147 |
+
error_msg = f"I apologize, but I'm having trouble processing your request right now. Please try again with a different question."
|
| 1148 |
+
st.session_state.medical_chat_history.append({"role": "assistant", "content": error_msg})
|
| 1149 |
+
|
| 1150 |
+
with st.chat_message("assistant"):
|
| 1151 |
+
st.markdown(error_msg)
|
| 1152 |
+
|
| 1153 |
+
# Force refresh to update the chat
|
| 1154 |
+
st.rerun()
|