import gradio as gr import json from pathlib import Path def load_json(file_path): with open(file_path, 'r') as file: return json.load(file) def display_data(index, data): item = data[index] note = item.get('Note', 'No note available') summary = item.get('Summary', {}) mcqs = item.get('MCQS', {}) image_path = item.get('image', '') summary_html = "

Summary

" if 'chest_xray_summary' in summary: for topic in summary['chest_xray_summary']: summary_html += f"

{topic['topic']}: {topic['description']}

" else: summary_html += "

No summary available

" mcqs_html = "

MCQs

" if isinstance(mcqs, dict) and 'questions' in mcqs: for i, q in enumerate(mcqs['questions'], 1): mcqs_html += f"

Q{i}: {q['question']}

" mcqs_html += "" mcqs_html += f"

Correct Answer: {q['correct_answer']}

" if 'location_in_text' in q: mcqs_html += f"

Location in text: {q['location_in_text']}

" mcqs_html += "
" elif isinstance(mcqs, list): for i, q in enumerate(mcqs, 1): mcqs_html += f"

Q{i}: {q['question']}

" mcqs_html += "" mcqs_html += f"

Correct Answer: {q['correct_answer']}

" if 'location_in_text' in q: mcqs_html += f"

Location in text: {q['location_in_text']}

" mcqs_html += "
" else: mcqs_html += "

No MCQs available for this item.

" return note, summary_html, mcqs_html, image_path, f"Item {index + 1} of {len(data)}" def next_item(index, data): return min(index + 1, len(data) - 1) def prev_item(index, data): return max(index - 1, 0) def create_app(json_path): data = load_json(json_path) with gr.Blocks() as app: gr.Markdown("# Chest X-Ray Analysis Viewer") with gr.Row(): with gr.Column(scale=1): image = gr.Image(label="X-Ray Image") with gr.Column(scale=2): note = gr.Textbox(label="Note", lines=4) summary = gr.HTML(label="Summary") mcqs = gr.HTML(label="MCQs") index = gr.State(0) item_count = gr.Textbox(label="Item Count") with gr.Row(): prev_btn = gr.Button("Previous") next_btn = gr.Button("Next") def update_display(index): return display_data(index, data) next_btn.click( next_item, inputs=[index, gr.State(data)], outputs=[index] ).then( update_display, inputs=[index], outputs=[note, summary, mcqs, image, item_count] ) prev_btn.click( prev_item, inputs=[index, gr.State(data)], outputs=[index] ).then( update_display, inputs=[index], outputs=[note, summary, mcqs, image, item_count] ) app.load( update_display, inputs=[index], outputs=[note, summary, mcqs, image, item_count] ) return app json_path = Path("mimic_data.json") app = create_app(json_path) app.launch()