File size: 5,178 Bytes
65850da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr
import json

# Include your JSON data directly in the script
data = [
    {
        "Note": "The image shows a new triangular opacity in the right lung apex, which indicates an abnormality in that area. The fullness of the right hilum, which is the area where the main bronchus, blood vessels, and nerves enter and exit the lung, is also new. The remainder of the lungs appears to be clear. The heart size is within the normal range. The blunting of bilateral costophrenic angles, with the right greater than the left, may be secondary to small effusions, which are fluid collections in the pleural space surrounding the lungs.",
        "Summary": {
            "chest_xray_summary": [
                {
                    "topic": "Right Lung Apex",
                    "description": "A new triangular opacity is observed in the right lung apex, indicating an abnormality in this area."
                },
                {
                    "topic": "Right Hilum",
                    "description": "There is fullness of the right hilum, which is a new finding. The hilum is where the main bronchus, blood vessels, and nerves enter and exit the lung."
                },
                {
                    "topic": "Lung Parenchyma",
                    "description": "The remainder of the lungs appears clear, aside from the noted abnormalities."
                },
                {
                    "topic": "Heart Size",
                    "description": "The heart size is within the normal range."
                },
                {
                    "topic": "Costophrenic Angles",
                    "description": "There is blunting of bilateral costophrenic angles, with the right side more affected than the left."
                },
                {
                    "topic": "Pleural Effusions",
                    "description": "The blunting of costophrenic angles may be secondary to small effusions in the pleural space surrounding the lungs."
                }
            ]
        },
        "MCQS": [
            {
                "category": "Diagnostic Accuracy",
                "question": "What finding is observed in the right lung apex on this chest X-ray?",
                "options": [
                    "A) A circular opacity",
                    "B) A triangular opacity",
                    "C) A linear opacity",
                    "D) No new opacity"
                ],
                "correct_answer": "B) A triangular opacity",
                "location_in_text": "The image shows a new triangular opacity in the right lung apex"
            },
            {
                "category": "Anatomical Detailing",
                "question": "Which anatomical structure shows fullness in this chest X-ray?",
                "options": [
                    "A) Left hilum",
                    "B) Right hilum",
                    "C) Left lung base",
                    "D) Right lung base"
                ],
                "correct_answer": "B) Right hilum",
                "location_in_text": "The fullness of the right hilum, which is the area where the main bronchus, blood vessels, and nerves enter and exit the lung, is also new."
            }
        ],
        "image": "https://huggingface.co/spaces/aaditya/demo_app/resolve/main/chest_xray.jpg"
    }
]

def display_data(index):
    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 = "<h3>Summary</h3>"
    if 'chest_xray_summary' in summary:
        for topic in summary['chest_xray_summary']:
            summary_html += f"<p><strong>{topic['topic']}:</strong> {topic['description']}</p>"
    else:
        summary_html += "<p>No summary available</p>"
    
    mcqs_html = "<h3>MCQs</h3>"
    for i, q in enumerate(mcqs, 1):
        mcqs_html += f"<p><strong>Q{i}: {q['question']}</strong></p>"
        mcqs_html += "<ul>"
        for option in q['options']:
            mcqs_html += f"<li>{option}</li>"
        mcqs_html += "</ul>"
        mcqs_html += f"<p>Correct Answer: {q['correct_answer']}</p>"
        if 'location_in_text' in q:
            mcqs_html += f"<p><em>Location in text: {q['location_in_text']}</em></p>"
        mcqs_html += "<hr>"
    
    return note, summary_html, mcqs_html, image_path

with gr.Blocks() as demo:
    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.Slider(0, len(data) - 1, step=1, label="Item Index")
    
    def update_display(index):
        return display_data(int(index))
    
    index.change(
        update_display,
        inputs=[index],
        outputs=[note, summary, mcqs, image]
    )
    
    demo.load(
        update_display,
        inputs=[index],
        outputs=[note, summary, mcqs, image]
    )

demo.launch()