Spaces:
Runtime error
Runtime error
updated code ✅✅
Browse files398 unmodified lines39940040140240340440514 unmodified lines4204214224234244254264274284294304314324334344354364374384394404414424434 unmodified lines44844945045145245313 unmodified lines46746846947047147247347447547647747847948048148248348448577 unmodified lines56356456556656756813 unmodified lines5825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166173 unmodified lines621622623624625626627398 unmodified linesdef create_interface():"""Create and launch the Gradio interface."""app = MediSyncApp()example_report = """14 unmodified linesRECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe."""sample_image_path = Nonetry:sample_images_dir = Path(__file__).parent.parent / "data" / "sample"os.makedirs(sample_images_dir, exist_ok=True)sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))if not sample_images:fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"sample_path = sample_images_dir / "sample_xray.jpg"try:response = requests.get(fallback_url, timeout=10)if response.status_code == 200:with open(sample_path, 'wb') as f:f.write(response.content)sample_image_path = str(sample_path)logger.info("Downloaded fallback sample image")except Exception as e:logger.warning(f"Could not download sample image: {str(e)}")else:sample_image_path = str(sample_images[0])except Exception as e:4 unmodified linespadding: 15px;margin: 10px 0;border-radius: 5px;}.alert-error {background-color: #ffebee;13 unmodified linescss=custom_css) as interface:appointment_id = gr.Textbox(visible=False)interface.load(None,None,None,_js="""function getAppointmentId() {const urlParams = new URLSearchParams(window.location.search);return urlParams.get('appointment_id') || '';}document.getElementById('appointment-id').value = getAppointmentId();""")gr.Markdown("""77 unmodified linesend_consultation_btn = gr.Button("End Consultation", variant="stop", size="lg")completion_status = gr.HTML()multi_img_enhance.click(app.enhance_image, inputs=multi_img_input, outputs=multi_img_input)multi_analyze_btn.click(app.analyze_multimodal,13 unmodified lines)def complete_consultation(appointment_id):if not appointment_id:return """<div class="alert-box alert-error">No appointment ID found. Please contact support.</div>"""try:response = requests.post("http://127.0.0.1:600/complete_consultation",json={"appointment_id": appointment_id},timeout=10)if response.status_code == 200:return """<div class="alert-box alert-success">Consultation completed successfully. Redirecting...<script>setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 2000);</script></div>"""return f"""<div class="alert-box alert-error">Error completing appointment (Status: {response.status_code}).</div>"""except Exception as e:return f"""<div class="alert-box alert-error">Error: {str(e)}</div>"""3 unmodified linesoutputs=[completion_status])interface.launch()if __name__ == "__main__":create_interface()398 unmodified lines39940040140240340440514 unmodified lines4204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514 unmodified lines45645745845946046146213 unmodified lines47647747847948048148248348448548648748848949049149249349449549649749849977 unmodified lines57757857958058158258313 unmodified lines5975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506513 unmodified lines655656657658659660661662663664665398 unmodified linesdef create_interface():"""Create and launch the Gradio interface with all fixes."""app = MediSyncApp()example_report = """14 unmodified linesRECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe."""# Sample image handling with multiple fallback optionssample_image_path = Nonefallback_urls = ["https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg","https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr1_lrg-a.jpg","https://github.com/ieee8023/covid-chestxray-dataset/raw/master/images/1-s2.0-S0929664620300449-gr3_lrg-a.jpg"]try:sample_images_dir = Path(__file__).parent.parent / "data" / "sample"os.makedirs(sample_images_dir, exist_ok=True)sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))if not sample_images:for i, url in enumerate(fallback_urls):try:sample_path = sample_images_dir / f"sample_xray_{i}.jpg"response = requests.get(url, timeout=10)if response.status_code == 200:with open(sample_path, 'wb') as f:f.write(response.content)sample_image_path = str(sample_path)logger.info(f"Successfully downloaded fallback sample image from {url}")breakexcept Exception as e:logger.warning(f"Could not download sample image from {url}: {str(e)}")else:sample_image_path = str(sample_images[0])except Exception as e:4 unmodified linespadding: 15px;margin: 10px 0;border-radius: 5px;font-family: sans-serif;}.alert-error {background-color: #ffebee;13 unmodified linescss=custom_css) as interface:# Appointment ID handling with both Python and JavaScript fallbackappointment_id = gr.Textbox(visible=False, elem_id="appointment-id")# JavaScript to extract appointment ID from URLjs_code = """function getAppointmentId() {const urlParams = new URLSearchParams(window.location.search);return urlParams.get('appointment_id') || '';}document.getElementById('appointment-id').value = getAppointmentId();"""# Initialize appointment ID from URLinterface.load(None,None,None,_js=js_code)gr.Markdown("""77 unmodified linesend_consultation_btn = gr.Button("End Consultation", variant="stop", size="lg")completion_status = gr.HTML()# Event handlersmulti_img_enhance.click(app.enhance_image, inputs=multi_img_input, outputs=multi_img_input)multi_analyze_btn.click(app.analyze_multimodal,13 unmodified lines)def complete_consultation(appointment_id):"""Handle consultation completion with improved error handling."""if not appointment_id:error_html = """<div class="alert-box alert-error"><strong>No Appointment ID Found</strong><p>Your session may not be properly recorded.</p><script>setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 3000);</script></div>"""logger.error("Completion attempted without appointment ID")return error_htmltry:response = requests.post("http://127.0.0.1:600/complete_consultation",json={"appointment_id": appointment_id},timeout=10)if response.ok:success_html = """<div class="alert-box alert-success"><strong>Consultation Completed</strong><p>You will be redirected shortly.</p><script>setTimeout(() => window.location.href = "http://127.0.0.1:600/complete_appointment", 2000);</script></div>"""logger.info(f"Successfully completed appointment {appointment_id}")return success_htmlerror_msg = f"Server error: {response.status_code}"if response.text:error_msg += f" - {response.text[:200]}"logger.error(error_msg)return f"""<div class="alert-box alert-error"><strong>Error Completing Appointment</strong><p>{error_msg}</p></div>"""except Exception as e:logger.error(f"Completion error: {str(e)}")return f"""<div class="alert-box alert-error"><strong>Connection Error</strong><p>{str(e)}</p></div>"""3 unmodified linesoutputs=[completion_status])try:interface.launch()except Exception as e:logger.error(f"Failed to launch interface: {str(e)}")raiseif __name__ == "__main__":create_interface()
mediSync/app.py+69-31