Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -109,19 +109,36 @@ def format_user_summary(analysis_result):
|
|
109 |
return "\n".join(summary)
|
110 |
|
111 |
def process_request(name, employee_id, email, department, other_dept, request_details, frequency, urgency):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
# Validate inputs
|
113 |
-
if not all([name, employee_id, email, request_details]):
|
|
|
114 |
return "Please fill in all required fields.", None
|
115 |
|
116 |
# Combine department info
|
117 |
final_department = other_dept if department == "Other" else department
|
|
|
118 |
|
119 |
try:
|
120 |
# Process the request through GPT
|
|
|
121 |
result = service.assess_request(request_details)
|
|
|
122 |
|
123 |
# Create user-friendly summary
|
124 |
user_summary = format_user_summary(result)
|
|
|
125 |
|
126 |
# Prepare data for logging
|
127 |
sheet_data = {
|
@@ -138,12 +155,16 @@ def process_request(name, employee_id, email, department, other_dept, request_de
|
|
138 |
}
|
139 |
|
140 |
# Log to Google Sheets
|
141 |
-
|
|
|
|
|
142 |
|
143 |
return user_summary, result
|
144 |
|
145 |
except Exception as e:
|
146 |
error_message = f"Error processing request: {str(e)}"
|
|
|
|
|
147 |
return error_message, None
|
148 |
|
149 |
# Create Gradio interface
|
@@ -151,19 +172,25 @@ with gr.Blocks() as demo:
|
|
151 |
gr.Markdown("# Hospital Data Request System")
|
152 |
gr.Markdown("Please fill in the following information to request data access.")
|
153 |
|
|
|
|
|
|
|
154 |
with gr.Row():
|
155 |
with gr.Column():
|
156 |
name = gr.Textbox(
|
157 |
label="Full Name*",
|
158 |
-
placeholder="Enter your full name"
|
|
|
159 |
)
|
160 |
employee_id = gr.Textbox(
|
161 |
label="Employee ID*",
|
162 |
-
placeholder="Enter your employee ID"
|
|
|
163 |
)
|
164 |
email = gr.Textbox(
|
165 |
label="Email*",
|
166 |
-
placeholder="Enter your email for contact"
|
|
|
167 |
)
|
168 |
|
169 |
with gr.Row():
|
@@ -171,20 +198,27 @@ with gr.Blocks() as demo:
|
|
171 |
department = gr.Dropdown(
|
172 |
choices=list(DEPARTMENTS.keys()),
|
173 |
label="Department*",
|
174 |
-
info="Select your department"
|
|
|
175 |
)
|
176 |
other_dept = gr.Textbox(
|
177 |
label="Other Department",
|
178 |
placeholder="Specify your department if not in the list",
|
179 |
-
visible=False
|
|
|
180 |
)
|
181 |
|
|
|
|
|
|
|
|
|
182 |
department.change(
|
183 |
-
fn=
|
184 |
-
inputs=
|
185 |
-
outputs=
|
186 |
)
|
187 |
|
|
|
188 |
with gr.Accordion("📝 Click here to see example requests", open=False):
|
189 |
gr.Markdown(EXAMPLE_REQUESTS)
|
190 |
|
@@ -192,32 +226,96 @@ with gr.Blocks() as demo:
|
|
192 |
request_details = gr.Textbox(
|
193 |
label="Request Details*",
|
194 |
placeholder="Please describe in detail what data you need, including time period, specific parameters, etc.",
|
195 |
-
lines=5
|
|
|
196 |
)
|
197 |
|
198 |
with gr.Row():
|
199 |
with gr.Column():
|
200 |
frequency = gr.Dropdown(
|
201 |
choices=FREQUENCIES,
|
202 |
-
label="Request Frequency*"
|
|
|
203 |
)
|
204 |
urgency = gr.Dropdown(
|
205 |
choices=URGENCY,
|
206 |
-
label="Urgency Level*"
|
|
|
207 |
)
|
208 |
|
209 |
-
|
|
|
210 |
|
|
|
211 |
with gr.Row():
|
212 |
-
user_output = gr.Markdown(
|
|
|
|
|
|
|
213 |
|
214 |
with gr.Accordion("Technical Analysis (For Data Team)", open=False):
|
215 |
tech_output = gr.JSON()
|
216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
submit_btn.click(
|
218 |
fn=process_request,
|
219 |
-
inputs=[
|
220 |
-
|
|
|
|
|
221 |
outputs=[user_output, tech_output]
|
222 |
)
|
223 |
|
|
|
109 |
return "\n".join(summary)
|
110 |
|
111 |
def process_request(name, employee_id, email, department, other_dept, request_details, frequency, urgency):
|
112 |
+
"""Process the data request and return both user and technical responses"""
|
113 |
+
# Debug prints
|
114 |
+
print(f"Received inputs:")
|
115 |
+
print(f"Name: {name}")
|
116 |
+
print(f"Employee ID: {employee_id}")
|
117 |
+
print(f"Email: {email}")
|
118 |
+
print(f"Department: {department}")
|
119 |
+
print(f"Other Dept: {other_dept}")
|
120 |
+
print(f"Request Details: {request_details}")
|
121 |
+
print(f"Frequency: {frequency}")
|
122 |
+
print(f"Urgency: {urgency}")
|
123 |
+
|
124 |
# Validate inputs
|
125 |
+
if not all([name, employee_id, email, request_details, department, frequency, urgency]):
|
126 |
+
print("Validation failed - missing required fields")
|
127 |
return "Please fill in all required fields.", None
|
128 |
|
129 |
# Combine department info
|
130 |
final_department = other_dept if department == "Other" else department
|
131 |
+
print(f"Final department: {final_department}")
|
132 |
|
133 |
try:
|
134 |
# Process the request through GPT
|
135 |
+
print("Calling GPT service...")
|
136 |
result = service.assess_request(request_details)
|
137 |
+
print(f"GPT response: {result}")
|
138 |
|
139 |
# Create user-friendly summary
|
140 |
user_summary = format_user_summary(result)
|
141 |
+
print(f"Generated summary: {user_summary}")
|
142 |
|
143 |
# Prepare data for logging
|
144 |
sheet_data = {
|
|
|
155 |
}
|
156 |
|
157 |
# Log to Google Sheets
|
158 |
+
print("Logging to sheets...")
|
159 |
+
success = sheets_logger.log_request(sheet_data)
|
160 |
+
print(f"Sheet logging result: {success}")
|
161 |
|
162 |
return user_summary, result
|
163 |
|
164 |
except Exception as e:
|
165 |
error_message = f"Error processing request: {str(e)}"
|
166 |
+
print(f"Error occurred: {error_message}")
|
167 |
+
print(f"Full exception: {traceback.format_exc()}")
|
168 |
return error_message, None
|
169 |
|
170 |
# Create Gradio interface
|
|
|
172 |
gr.Markdown("# Hospital Data Request System")
|
173 |
gr.Markdown("Please fill in the following information to request data access.")
|
174 |
|
175 |
+
# Debug output for monitoring
|
176 |
+
debug_output = gr.Markdown(visible=False)
|
177 |
+
|
178 |
with gr.Row():
|
179 |
with gr.Column():
|
180 |
name = gr.Textbox(
|
181 |
label="Full Name*",
|
182 |
+
placeholder="Enter your full name",
|
183 |
+
value="" # Ensure empty initial value
|
184 |
)
|
185 |
employee_id = gr.Textbox(
|
186 |
label="Employee ID*",
|
187 |
+
placeholder="Enter your employee ID",
|
188 |
+
value=""
|
189 |
)
|
190 |
email = gr.Textbox(
|
191 |
label="Email*",
|
192 |
+
placeholder="Enter your email for contact",
|
193 |
+
value=""
|
194 |
)
|
195 |
|
196 |
with gr.Row():
|
|
|
198 |
department = gr.Dropdown(
|
199 |
choices=list(DEPARTMENTS.keys()),
|
200 |
label="Department*",
|
201 |
+
info="Select your department",
|
202 |
+
value=list(DEPARTMENTS.keys())[0] # Set default value
|
203 |
)
|
204 |
other_dept = gr.Textbox(
|
205 |
label="Other Department",
|
206 |
placeholder="Specify your department if not in the list",
|
207 |
+
visible=False,
|
208 |
+
value=""
|
209 |
)
|
210 |
|
211 |
+
# Handle department change
|
212 |
+
def update_other_dept_visibility(dept):
|
213 |
+
return gr.update(visible=(dept == "Other"))
|
214 |
+
|
215 |
department.change(
|
216 |
+
fn=update_other_dept_visibility,
|
217 |
+
inputs=department,
|
218 |
+
outputs=other_dept
|
219 |
)
|
220 |
|
221 |
+
# Example requests section
|
222 |
with gr.Accordion("📝 Click here to see example requests", open=False):
|
223 |
gr.Markdown(EXAMPLE_REQUESTS)
|
224 |
|
|
|
226 |
request_details = gr.Textbox(
|
227 |
label="Request Details*",
|
228 |
placeholder="Please describe in detail what data you need, including time period, specific parameters, etc.",
|
229 |
+
lines=5,
|
230 |
+
value=""
|
231 |
)
|
232 |
|
233 |
with gr.Row():
|
234 |
with gr.Column():
|
235 |
frequency = gr.Dropdown(
|
236 |
choices=FREQUENCIES,
|
237 |
+
label="Request Frequency*",
|
238 |
+
value=FREQUENCIES[0] # Set default value
|
239 |
)
|
240 |
urgency = gr.Dropdown(
|
241 |
choices=URGENCY,
|
242 |
+
label="Urgency Level*",
|
243 |
+
value=URGENCY[0] # Set default value
|
244 |
)
|
245 |
|
246 |
+
# Submit button and processing
|
247 |
+
submit_btn = gr.Button("Submit Request", variant="primary")
|
248 |
|
249 |
+
# Output sections
|
250 |
with gr.Row():
|
251 |
+
user_output = gr.Markdown(
|
252 |
+
label="Request Summary",
|
253 |
+
value=""
|
254 |
+
)
|
255 |
|
256 |
with gr.Accordion("Technical Analysis (For Data Team)", open=False):
|
257 |
tech_output = gr.JSON()
|
258 |
|
259 |
+
def process_request(name, employee_id, email, department, other_dept, request_details, frequency, urgency):
|
260 |
+
print(f"Processing request with inputs:")
|
261 |
+
print(f"Name: {name}")
|
262 |
+
print(f"Employee ID: {employee_id}")
|
263 |
+
print(f"Email: {email}")
|
264 |
+
print(f"Department: {department}")
|
265 |
+
print(f"Other Dept: {other_dept}")
|
266 |
+
print(f"Request Details: {request_details}")
|
267 |
+
print(f"Frequency: {frequency}")
|
268 |
+
print(f"Urgency: {urgency}")
|
269 |
+
|
270 |
+
# Validate inputs
|
271 |
+
if not all([name, employee_id, email, request_details, department, frequency, urgency]):
|
272 |
+
print("Validation failed - missing required fields")
|
273 |
+
return "Please fill in all required fields.", None
|
274 |
+
|
275 |
+
# Combine department info
|
276 |
+
final_department = other_dept if department == "Other" else department
|
277 |
+
|
278 |
+
try:
|
279 |
+
# Process the request through GPT
|
280 |
+
print("Calling GPT service...")
|
281 |
+
result = service.assess_request(request_details)
|
282 |
+
print(f"GPT response received: {result}")
|
283 |
+
|
284 |
+
# Create user-friendly summary
|
285 |
+
user_summary = format_user_summary(result)
|
286 |
+
|
287 |
+
# Prepare data for logging
|
288 |
+
sheet_data = {
|
289 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
290 |
+
"name": name,
|
291 |
+
"employee_id": employee_id,
|
292 |
+
"email": email,
|
293 |
+
"department": final_department,
|
294 |
+
"request_details": request_details,
|
295 |
+
"frequency": frequency,
|
296 |
+
"urgency": urgency,
|
297 |
+
"user_summary": user_summary,
|
298 |
+
"system_analysis": json.dumps(result, ensure_ascii=False)
|
299 |
+
}
|
300 |
+
|
301 |
+
# Log to Google Sheets
|
302 |
+
print("Logging to sheets...")
|
303 |
+
sheets_logger.log_request(sheet_data)
|
304 |
+
|
305 |
+
return user_summary, result
|
306 |
+
|
307 |
+
except Exception as e:
|
308 |
+
error_msg = f"Error processing request: {str(e)}"
|
309 |
+
print(error_msg)
|
310 |
+
return error_msg, None
|
311 |
+
|
312 |
+
# Connect the submit button to the process function
|
313 |
submit_btn.click(
|
314 |
fn=process_request,
|
315 |
+
inputs=[
|
316 |
+
name, employee_id, email, department, other_dept,
|
317 |
+
request_details, frequency, urgency
|
318 |
+
],
|
319 |
outputs=[user_output, tech_output]
|
320 |
)
|
321 |
|