Spaces:
Sleeping

Sumayyea commited on
Commit
198687b
·
verified ·
1 Parent(s): cb0bd6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -68,7 +68,7 @@ if st.sidebar.button('Submit'):
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
@@ -87,24 +87,20 @@ if st.sidebar.button('Submit'):
87
  "personalized mental health diagnoses and treatment plans. " + prompt
88
  }
89
  ],
90
- "max_tokens": 1000,
91
  "temperature": 0.7
92
- }
 
93
  )
94
 
95
  # Handle the response
96
- if response.status_code == 200:
97
  data = response.json()
98
  if 'choices' in data and len(data['choices']) > 0:
99
  diagnosis_and_treatment = data['choices'][0]['message']['content']
100
 
101
- # Display the response with better formatting
102
- st.subheader("Diagnosis")
103
- st.write(diagnosis_and_treatment.split("Treatment Plan:")[0].replace("Diagnosis:", "").strip())
104
-
105
- if "Treatment Plan:" in diagnosis_and_treatment:
106
- st.subheader("Treatment Plan")
107
- st.write(diagnosis_and_treatment.split("Treatment Plan:")[1].strip())
108
 
109
  # Inform the patient that a call will be placed soon
110
  if communication_pref == 'Text and Call':
@@ -113,13 +109,20 @@ if st.sidebar.button('Submit'):
113
  else:
114
  st.error("No response received from the AI model.")
115
  else:
116
- st.error(f"API request failed with status code {response.status_code}")
117
- st.write(response.text)
118
-
 
 
 
 
 
 
 
119
  except Exception as e:
120
- st.error(f"An error occurred: {str(e)}")
121
  else:
122
- st.write("Please complete the form in the sidebar and click 'Submit' to receive your personalized plan.")
123
 
124
  # Additional UI/UX features for a calming experience
125
  st.markdown("""
@@ -134,16 +137,23 @@ body {
134
  .stButton > button {
135
  background-color: #4CAF50;
136
  color: white;
 
 
137
  }
138
- .stMarkdown h2 {
139
  color: #2e86ab;
140
- border-bottom: 2px solid #2e86ab;
141
- padding-bottom: 5px;
 
142
  }
143
  </style>
144
  """, unsafe_allow_html=True)
145
 
146
  # Footer with copyright and year
147
  st.markdown("---")
148
- st.markdown('<div style="text-align: center;">© 2024 PsychGuide by Engr. Sumayyea Salahuddin. All rights reserved.</div>',
149
- unsafe_allow_html=True)
 
 
 
 
 
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 using markdown headers."
72
  )
73
 
74
  # Call AIML API
 
87
  "personalized mental health diagnoses and treatment plans. " + prompt
88
  }
89
  ],
90
+ "max_tokens": 1500, # Increased slightly to allow for comprehensive responses
91
  "temperature": 0.7
92
+ },
93
+ timeout=30 # Added timeout to prevent hanging
94
  )
95
 
96
  # Handle the response
97
+ if response.status_code in (200, 201): # Accept both success status codes
98
  data = response.json()
99
  if 'choices' in data and len(data['choices']) > 0:
100
  diagnosis_and_treatment = data['choices'][0]['message']['content']
101
 
102
+ # Display the formatted response
103
+ st.markdown(diagnosis_and_treatment)
 
 
 
 
 
104
 
105
  # Inform the patient that a call will be placed soon
106
  if communication_pref == 'Text and Call':
 
109
  else:
110
  st.error("No response received from the AI model.")
111
  else:
112
+ error_msg = f"API request failed with status code {response.status_code}"
113
+ try:
114
+ error_details = response.json()
115
+ error_msg += f"\nDetails: {error_details.get('message', 'No additional details')}"
116
+ except:
117
+ error_msg += f"\nResponse: {response.text}"
118
+ st.error(error_msg)
119
+
120
+ except requests.exceptions.Timeout:
121
+ st.error("The request timed out. Please try again.")
122
  except Exception as e:
123
+ st.error(f"An unexpected error occurred: {str(e)}")
124
  else:
125
+ st.info("Please complete the form in the sidebar and click 'Submit' to receive your personalized plan.")
126
 
127
  # Additional UI/UX features for a calming experience
128
  st.markdown("""
 
137
  .stButton > button {
138
  background-color: #4CAF50;
139
  color: white;
140
+ border-radius: 5px;
141
+ padding: 0.5rem 1rem;
142
  }
143
+ .stMarkdown h1, .stMarkdown h2, .stMarkdown h3 {
144
  color: #2e86ab;
145
+ }
146
+ .stTextArea textarea, .stTextInput input {
147
+ border-radius: 5px;
148
  }
149
  </style>
150
  """, unsafe_allow_html=True)
151
 
152
  # Footer with copyright and year
153
  st.markdown("---")
154
+ st.markdown("""
155
+ <div style="text-align: center; padding: 1rem; color: #666;">
156
+ © 2024 PsychGuide by Engr. Sumayyea Salahuddin. All rights reserved.<br>
157
+ <small>This app provides general information only and is not a substitute for professional medical advice.</small>
158
+ </div>
159
+ """, unsafe_allow_html=True)