Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,153 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
# Set the title and introduction
|
| 5 |
-
st.title('Psychiatric Patient Diagnosis and Treatment App')
|
| 6 |
-
st.write("""
|
| 7 |
-
Welcome to the Psychiatric Patient Diagnosis and Treatment App.
|
| 8 |
-
Please provide the following information to receive a personalized diagnosis and treatment recommendation.
|
| 9 |
-
Your responses will remain confidential.
|
| 10 |
-
""")
|
| 11 |
-
|
| 12 |
-
# Sidebar for inputs
|
| 13 |
-
st.sidebar.header('Patient Information')
|
| 14 |
-
|
| 15 |
-
# Input for Mental Health History
|
| 16 |
-
mental_health_history = st.sidebar.text_area('Mental Health History',
|
| 17 |
-
placeholder="Please describe your past psychiatric history...")
|
| 18 |
-
|
| 19 |
-
# Input for Current Symptoms
|
| 20 |
-
current_symptoms = st.sidebar.multiselect(
|
| 21 |
-
'Current Symptoms',
|
| 22 |
-
['Anxiety', 'Depression', 'Insomnia', 'Mood Swings', 'Irritability', 'Fatigue',
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
if
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
#
|
| 106 |
-
st.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
.
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
"
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
st.
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Set the title and introduction
|
| 5 |
+
st.title('Psychiatric Patient Diagnosis and Treatment App')
|
| 6 |
+
st.write("""
|
| 7 |
+
Welcome to the Psychiatric Patient Diagnosis and Treatment App.
|
| 8 |
+
Please provide the following information to receive a personalized diagnosis and treatment recommendation.
|
| 9 |
+
Your responses will remain confidential.
|
| 10 |
+
""")
|
| 11 |
+
|
| 12 |
+
# Sidebar for inputs
|
| 13 |
+
st.sidebar.header('Patient Information')
|
| 14 |
+
|
| 15 |
+
# Input for Mental Health History
|
| 16 |
+
mental_health_history = st.sidebar.text_area('Mental Health History',
|
| 17 |
+
placeholder="Please describe your past psychiatric history...")
|
| 18 |
+
|
| 19 |
+
# Input for Current Symptoms
|
| 20 |
+
current_symptoms = st.sidebar.multiselect(
|
| 21 |
+
'Current Symptoms',
|
| 22 |
+
['Anxiety', 'Depression', 'Insomnia', 'Mood Swings', 'Irritability', 'Fatigue',
|
| 23 |
+
'Difficulty Concentrating', 'Panic Attacks', 'Other']
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Input for Medication Preferences
|
| 27 |
+
medication_pref = st.sidebar.selectbox(
|
| 28 |
+
'Medication Preferences',
|
| 29 |
+
['No Preference', 'Prefer Natural Remedies', 'Prefer Prescription Medication', 'Prefer to Avoid Medication']
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Input for Treatment Preferences
|
| 33 |
+
treatment_pref = st.sidebar.radio(
|
| 34 |
+
'Treatment Preferences',
|
| 35 |
+
['Cognitive Behavioral Therapy (CBT)', 'Medication Management', 'Therapy & Medication',
|
| 36 |
+
'Mindfulness-Based Therapy', 'Other']
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Input for Preferred Communication Method
|
| 40 |
+
communication_pref = st.sidebar.selectbox(
|
| 41 |
+
'Preferred Communication Method',
|
| 42 |
+
['Text Only', 'Text and Call']
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Show additional fields for Text and Call option
|
| 46 |
+
patient_name = None
|
| 47 |
+
patient_contact_number = None
|
| 48 |
+
|
| 49 |
+
if communication_pref == 'Text and Call':
|
| 50 |
+
patient_name = st.sidebar.text_input('Patient Name')
|
| 51 |
+
patient_contact_number = st.sidebar.text_input('Contact Number', help="Please enter your phone number")
|
| 52 |
+
|
| 53 |
+
# Main section for displaying results
|
| 54 |
+
st.header('Personalized Diagnosis and Treatment Plan')
|
| 55 |
+
|
| 56 |
+
# Button to submit the form
|
| 57 |
+
if st.sidebar.button('Submit'):
|
| 58 |
+
if communication_pref == 'Text and Call' and (not patient_name or not patient_contact_number):
|
| 59 |
+
st.error("Please provide both name and contact number to receive the call.")
|
| 60 |
+
else:
|
| 61 |
+
with st.spinner('Generating your personalized diagnosis...'):
|
| 62 |
+
try:
|
| 63 |
+
# Prepare the prompt
|
| 64 |
+
prompt = (
|
| 65 |
+
f"Mental Health History: {mental_health_history}\n"
|
| 66 |
+
f"Current Symptoms: {', '.join(current_symptoms)}\n"
|
| 67 |
+
f"Medication Preferences: {medication_pref}\n"
|
| 68 |
+
f"Treatment Preferences: {treatment_pref}\n\n"
|
| 69 |
+
"Please provide a detailed psychiatric diagnosis and comprehensive treatment plan "
|
| 70 |
+
"including therapy recommendations and medication options (if applicable). "
|
| 71 |
+
"Format your response with clear sections for Diagnosis and Treatment Plan."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Call AIML API
|
| 75 |
+
response = requests.post(
|
| 76 |
+
"https://api.aimlapi.com/v1/chat/completions",
|
| 77 |
+
headers={
|
| 78 |
+
"Content-Type": "application/json",
|
| 79 |
+
"Authorization": f"Bearer {st.secrets['aimlapi_key']}"
|
| 80 |
+
},
|
| 81 |
+
json={
|
| 82 |
+
"model": "anthropic/claude-3.7-sonnet",
|
| 83 |
+
"messages": [
|
| 84 |
+
{
|
| 85 |
+
"role": "system",
|
| 86 |
+
"content": "You are a world-class psychiatrist who specializes in providing "
|
| 87 |
+
"personalized mental health diagnoses and treatment plans."
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"role": "user",
|
| 91 |
+
"content": prompt
|
| 92 |
+
}
|
| 93 |
+
],
|
| 94 |
+
"max_tokens": 1000,
|
| 95 |
+
"temperature": 0.7
|
| 96 |
+
}
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Handle the response
|
| 100 |
+
if response.status_code == 200:
|
| 101 |
+
data = response.json()
|
| 102 |
+
if 'choices' in data and len(data['choices']) > 0:
|
| 103 |
+
diagnosis_and_treatment = data['choices'][0]['message']['content']
|
| 104 |
+
|
| 105 |
+
# Display the response with better formatting
|
| 106 |
+
st.subheader("Diagnosis")
|
| 107 |
+
st.write(diagnosis_and_treatment.split("Treatment Plan:")[0].replace("Diagnosis:", "").strip())
|
| 108 |
+
|
| 109 |
+
if "Treatment Plan:" in diagnosis_and_treatment:
|
| 110 |
+
st.subheader("Treatment Plan")
|
| 111 |
+
st.write(diagnosis_and_treatment.split("Treatment Plan:")[1].strip())
|
| 112 |
+
|
| 113 |
+
# Inform the patient that a call will be placed soon
|
| 114 |
+
if communication_pref == 'Text and Call':
|
| 115 |
+
st.success(f"Thank you, {patient_name}. A call will be placed to you at "
|
| 116 |
+
f"{patient_contact_number} within the next 24 hours.")
|
| 117 |
+
else:
|
| 118 |
+
st.error("No response received from the AI model.")
|
| 119 |
+
else:
|
| 120 |
+
st.error(f"API request failed with status code {response.status_code}")
|
| 121 |
+
st.write(response.text)
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
st.error(f"An error occurred: {str(e)}")
|
| 125 |
+
else:
|
| 126 |
+
st.write("Please complete the form in the sidebar and click 'Submit' to receive your personalized plan.")
|
| 127 |
+
|
| 128 |
+
# Additional UI/UX features for a calming experience
|
| 129 |
+
st.markdown("""
|
| 130 |
+
<style>
|
| 131 |
+
body {
|
| 132 |
+
background-color: #f4f7f8;
|
| 133 |
+
font-family: 'Arial', sans-serif;
|
| 134 |
+
}
|
| 135 |
+
.stSidebar {
|
| 136 |
+
background-color: #f0f2f6;
|
| 137 |
+
}
|
| 138 |
+
.stButton > button {
|
| 139 |
+
background-color: #4CAF50;
|
| 140 |
+
color: white;
|
| 141 |
+
}
|
| 142 |
+
.stMarkdown h2 {
|
| 143 |
+
color: #2e86ab;
|
| 144 |
+
border-bottom: 2px solid #2e86ab;
|
| 145 |
+
padding-bottom: 5px;
|
| 146 |
+
}
|
| 147 |
+
</style>
|
| 148 |
+
""", unsafe_allow_html=True)
|
| 149 |
+
|
| 150 |
+
# Footer with copyright and year
|
| 151 |
+
st.markdown("---")
|
| 152 |
+
st.markdown('<div style="text-align: center;">© 2024 PsychGuide by Engr. Sumayyea Salahuddin. All rights reserved.</div>',
|
| 153 |
+
unsafe_allow_html=True)
|