dvtiendat/Lungs-Radiography-Analysis
1
1import gradio as gr2import torch.nn.functional as F3import albumentations as A4from pipeline import *5 6def get_css(css_path):7 with open(css_path, 'r') as f:8 custom = f.read()9 10 return custom11 12def create_interface():13 custom = get_css('design/design.css')14 processor = Pipeline()15 16 with gr.Blocks(css=custom, theme=gr.themes.Soft(primary_hue='teal', secondary_hue='blue')) as interface:17 with gr.Column(variant="compact"):18 gr.Markdown("# Lungs Radiography Analysis", elem_classes='heading')19 gr.Markdown("""20 Upload/ Drop a chest X-ray image for COVID-19 diagnosis and analysis. 21 """)22 with gr.Row(equal_height=True):23 # [UPLOAD IMAGE SECTION]24 with gr.Column():25 input_image = gr.Image(26 label="Upload Chest X-ray",27 height=400,28 elem_classes="upload-image"29 )30 31 # [BUTTON]32 with gr.Row():33 submit_btn = gr.Button("Analyze Image", variant="primary", elem_classes='primary-button', scale=2)34 clear_btn = gr.Button('Clear', variant='secondary', scale=1)35 36 with gr.Column():37 with gr.Group(elem_classes='results-container'): 38 output_image = gr.Image(39 label="COVID-19 Analysis",40 visible=False,41 height=40042 )43 44 with gr.Row(equal_height=True):45 diagnosis_label = gr.Label(label="Diagnosis Conclusion", elem_classes='results-container')46 confidence_label = gr.Label(label="Confidence Score", elem_classes='results-container')47 48 with gr.Row():49 diagnosis_text = gr.Textbox(50 label="Diagnosis Details",51 visible=False,52 container=False53 )54 55 # [HELP SECTION] 56 with gr.Accordion("Information", open=False):57 gr.Markdown("""58 ### Tutorial59 1. Click the upload button/ Drag and drop a chest X-ray image.60 2. Choose 'Analyze Image'.61 3. Review the results:62 - For COVID cases: View highlighted infection regions.63 - For Non-COVID/Healthy cases: Review detailed diagnosis text.64 """)65 66 def clear_inputs():67 return {68 input_image: None,69 output_image: gr.update(visible=False),70 diagnosis_text: gr.update(visible=False),71 diagnosis_label: None,72 confidence_label: None73 }74 75 def handle_prediction(image, opacity=0.4): 76 prediction, confidence, output_img, analysis_text = processor.process_image(77 image, overlay_opacity=opacity78 )79 80 confidence_class = (81 "confidence-high" if confidence > 9082 else "confidence-medium" if confidence > 7083 else "confidence-low"84 )85 print(confidence_class)86 87 is_covid = output_img is not None88 89 return {90 diagnosis_label: prediction,91 confidence_label: gr.update(92 value=f"Confidence: {confidence:.2f}%",93 elem_classes=[confidence_class]94 ),95 output_image: gr.update(value=output_img, visible=is_covid),96 diagnosis_text: gr.update(value=analysis_text, visible=True)97 }98 99 submit_btn.click(100 fn=handle_prediction,101 inputs=[input_image],102 outputs=[103 diagnosis_label,104 confidence_label,105 output_image,106 diagnosis_text,107 ]108 )109 110 clear_btn.click(111 fn=clear_inputs,112 inputs=[],113 outputs=[114 input_image,115 output_image,116 diagnosis_text,117 diagnosis_label,118 confidence_label119 ]120 )121 122 return interface123 124if __name__ == "__main__":125 interface = create_interface()126 interface.launch(share=True)