Sumayyea commited on
Commit
0547d20
·
verified ·
1 Parent(s): bd2ab43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -125
app.py CHANGED
@@ -1,125 +1,153 @@
1
- import streamlit as st
2
- import anthropic
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', 'Difficulty Concentrating', 'Panic Attacks', 'Other']
23
- )
24
-
25
- # Input for Medication Preferences
26
- medication_pref = st.sidebar.selectbox(
27
- 'Medication Preferences',
28
- ['No Preference', 'Prefer Natural Remedies', 'Prefer Prescription Medication', 'Prefer to Avoid Medication']
29
- )
30
-
31
- # Input for Treatment Preferences
32
- treatment_pref = st.sidebar.radio(
33
- 'Treatment Preferences',
34
- ['Cognitive Behavioral Therapy (CBT)', 'Medication Management', 'Therapy & Medication', 'Mindfulness-Based Therapy', 'Other']
35
- )
36
-
37
- # Input for Preferred Communication Method
38
- communication_pref = st.sidebar.selectbox(
39
- 'Preferred Communication Method',
40
- ['Text Only', 'Text and Call']
41
- )
42
-
43
- # Show additional fields for Text and Call option
44
- patient_name = None
45
- patient_contact_number = None
46
-
47
- if communication_pref == 'Text and Call':
48
- patient_name = st.sidebar.text_input('Patient Name')
49
- patient_contact_number = st.sidebar.text_input('Contact Number', help="Please enter your phone number")
50
-
51
- # Input for Claude AI API key
52
- api_key = st.secrets["claude_api_key"]
53
-
54
- # Main section for displaying results
55
- st.header('Personalized Diagnosis and Treatment Plan')
56
-
57
- # Button to submit the form
58
- if st.sidebar.button('Submit'):
59
- if api_key: # Ensure the API key is provided
60
- if communication_pref == 'Text and Call' and (not patient_name or not patient_contact_number):
61
- st.error("Please provide both name and contact number to receive the call.")
62
- else:
63
- # Initialize the Claude AI client with the provided API key
64
- client = anthropic.Anthropic(api_key=api_key)
65
-
66
- # Prepare the prompt to send to Claude AI
67
- prompt = (
68
- f"Mental Health History: {mental_health_history}\n"
69
- f"Current Symptoms: {', '.join(current_symptoms)}\n"
70
- f"Medication Preferences: {medication_pref}\n"
71
- f"Treatment Preferences: {treatment_pref}\n"
72
- f"Preferred Communication Method: {communication_pref}\n\n"
73
- "Please provide a personalized diagnosis and treatment plan based on the above information."
74
- )
75
-
76
- # Call Claude AI API
77
- message = client.messages.create(
78
- model="claude-3-5-sonnet-20240620",
79
- max_tokens=280,
80
- temperature=0.7, # Adjust temperature for balanced creativity
81
- system="You are a world-class psychiatrist who specializes in providing personalized mental health diagnoses and treatment plans.",
82
- messages=[
83
- {
84
- "role": "user",
85
- "content": prompt
86
- }
87
- ]
88
- )
89
-
90
- # Extract the response content
91
- raw_context = message.content
92
- diagnosis_and_treatment = raw_context[0].text
93
-
94
- # Display the response in the main section
95
- st.write(diagnosis_and_treatment)
96
-
97
- # Inform the patient that a call will be placed soon
98
- if communication_pref == 'Text and Call':
99
- st.success(f"Thank you, {patient_name}. A call will be placed to you at {patient_contact_number} within the next 24 hours.")
100
- else:
101
- st.error("Please enter your Claude API key to receive a diagnosis and treatment plan.")
102
- else:
103
- st.write("Please complete the form in the sidebar and click 'Submit' to receive your personalized plan.")
104
-
105
- # Additional UI/UX features for a calming experience
106
- st.markdown("""
107
- <style>
108
- body {
109
- background-color: #f4f7f8;
110
- font-family: 'Arial', sans-serif;
111
- }
112
- .stSidebar {
113
- background-color: #f0f2f6;
114
- }
115
- .stButton > button {
116
- background-color: #4CAF50;
117
- color: white;
118
- }
119
- </style>
120
- """, unsafe_allow_html=True)
121
-
122
- # Footer with copyright and year
123
- st.markdown("---")
124
- st.markdown('<div style="text-align: center;">© 2024 PsychGuide by Engr. Sumayyea Salahuddin. All rights reserved.</div>', unsafe_allow_html=True)
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)