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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import gradio as gr
import pandas as pd
from datasets import load_dataset
from openai import OpenAI
from PIL import Image
import io
import base64
import logging
import torch
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from threading import Thread
from typing import Iterator
import os
import spaces

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# App version
APP_VERSION = "1.0.0"
logger.info(f"Starting Radiology Teaching App v{APP_VERSION}")

# Global variables
pipe = None
llm = None
tokenizer = None
device = 0 if torch.cuda.is_available() else "cpu"
logger.info(f"Using device: {device}")

# Initialize Whisper
MODEL_NAME = "openai/whisper-large-v3-turbo"
BATCH_SIZE = 8
FILE_LIMIT_MB = 5000

try:
    logger.info("Initializing Whisper model...")
    pipe = pipeline(
        task="automatic-speech-recognition",
        model=MODEL_NAME,
        chunk_length_s=30,
        device=device,
    )
except Exception as e:
    logger.error(f"Error initializing Whisper model: {str(e)}")
    pipe = None

# Initialize Llama
try:
    logger.info("Initializing Llama model...")
    llm_model_id = "chuanli11/Llama-3.2-3B-Instruct-uncensored"
    
    # Initialize tokenizer first
    tokenizer = AutoTokenizer.from_pretrained(llm_model_id)
    tokenizer.use_default_system_prompt = False
    
    # Initialize model with proper device mapping
    if torch.cuda.is_available():
        logger.info("Loading Llama model on GPU...")
        llm = AutoModelForCausalLM.from_pretrained(
            llm_model_id,
            torch_dtype=torch.float16,
            device_map="auto",
            load_in_8bit=True  # Use 8-bit quantization to reduce memory usage
        )
    else:
        logger.info("Loading Llama model on CPU...")
        llm = AutoModelForCausalLM.from_pretrained(
            llm_model_id,
            device_map={"": "cpu"},
            low_cpu_mem_usage=True
        )
except Exception as e:
    logger.error(f"Error initializing Llama model: {str(e)}")
    llm = None
    tokenizer = None

try:
    # Load only 10 rows from the dataset
    logger.info("Loading MIMIC-CXR dataset...")
    dataset = load_dataset("itsanmolgupta/mimic-cxr-dataset", split="train").select(range(10))
    df = pd.DataFrame(dataset)
    logger.info(f"Successfully loaded {len(df)} cases")
except Exception as e:
    logger.error(f"Error loading dataset: {str(e)}")
    raise

def encode_image_to_base64(image_bytes):
    return base64.b64encode(image_bytes).decode('utf-8')

def analyze_report(user_findings, ground_truth_findings, ground_truth_impression, api_key):
    if not api_key:
        return "Please provide a DeepSeek API key to analyze the report."
    
    try:
        client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
        logger.info("Analyzing report with DeepSeek...")
        
        prompt = f"""You are an expert radiologist reviewing a trainee's chest X-ray report. 
        
        Trainee's Findings:
        {user_findings}
        
        Ground Truth Findings:
        {ground_truth_findings}
        
        Ground Truth Impression:
        {ground_truth_impression}
        
        Please provide:
        1. Number of important findings missed by the trainee (list them)
        2. Quality assessment of the trainee's report (structure, completeness, accuracy)
        3. Constructive feedback for improvement
        
        Format your response in clear sections."""

        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[
                {"role": "system", "content": "You are an expert radiologist providing constructive feedback."},
                {"role": "user", "content": prompt}
            ],
            stream=False
        )
        
        return response.choices[0].message.content
    except Exception as e:
        logger.error(f"Error in report analysis: {str(e)}")
        return f"Error analyzing report: {str(e)}"

@spaces.GPU
def transcribe(inputs, task="transcribe"):
    """Transcribe audio using Whisper"""
    if inputs is None:
        raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
    if pipe is None:
        raise gr.Error("Whisper model not initialized properly!")
    
    try:
        logger.info("Transcribing audio...")
        text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
        return text
    except Exception as e:
        logger.error(f"Error in transcription: {str(e)}")
        raise gr.Error(f"Transcription failed: {str(e)}")

@spaces.GPU
def analyze_with_llama(
    transcribed_text: str,
    ground_truth_findings: str,
    ground_truth_impression: str,
    max_new_tokens: int = 1024,
    temperature: float = 0.6,
) -> Iterator[str]:
    """Analyze transcribed report against ground truth using Llama"""
    global llm, tokenizer  # Add global declaration
    
    if llm is None or tokenizer is None:
        raise gr.Error("Llama model not initialized properly!")
    
    try:
        task_prompt = f"""You are an expert radiologist. Compare the following transcribed radiology report with the ground truth and provide detailed feedback.

        Transcribed Report:
        {transcribed_text}

        Ground Truth Findings:
        {ground_truth_findings}

        Ground Truth Impression:
        {ground_truth_impression}

        Please analyze:
        1. Accuracy of findings
        2. Completeness of report
        3. Structure and clarity
        4. Areas for improvement
        
        Provide your analysis in a clear, structured format."""

        conversation = [
            {"role": "system", "content": "You are an expert radiologist providing detailed feedback."},
            {"role": "user", "content": task_prompt}
        ]

        input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
        input_ids = input_ids.to(llm.device)

        streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
        generate_kwargs = dict(
            input_ids=input_ids,
            streamer=streamer,
            max_new_tokens=max_new_tokens,
            do_sample=True,
            temperature=temperature,
            num_beams=1,
        )

        t = Thread(target=llm.generate, kwargs=generate_kwargs)
        t.start()

        outputs = []
        for text in streamer:
            outputs.append(text)
            yield "".join(outputs)
    except Exception as e:
        logger.error(f"Error in Llama analysis: {str(e)}")
        raise gr.Error(f"Analysis failed: {str(e)}")

def load_random_case(hide_ground_truth):
    try:
        # Randomly select a case from our dataset
        random_case = df.sample(n=1).iloc[0]
        logger.info("Loading random case...")
        
        # Get the image, findings, and impression
        image = random_case['image']
        # Store full findings and impression regardless of hide_ground_truth
        findings = random_case['findings']
        impression = random_case['impression']
        
        # Only hide display if hide_ground_truth is True
        display_findings = "" if hide_ground_truth else findings
        display_impression = "" if hide_ground_truth else impression
        
        # Return both display values and actual values
        return image, display_findings, display_impression, findings, impression
    except Exception as e:
        logger.error(f"Error loading random case: {str(e)}")
        return None, "Error loading case", "Error loading case", "", ""

def process_case(image, user_findings, hide_ground_truth, api_key, current_findings="", current_impression="", actual_findings="", actual_impression=""):
    # Use actual findings/impression for analysis if they exist, otherwise fall back to current values
    findings_for_analysis = actual_findings if actual_findings else current_findings
    impression_for_analysis = actual_impression if actual_impression else current_impression
    
    analysis = analyze_report(user_findings, findings_for_analysis, impression_for_analysis, api_key)
    
    # Return display values based on hide_ground_truth
    display_findings = "" if hide_ground_truth else findings_for_analysis
    display_impression = "" if hide_ground_truth else impression_for_analysis
    
    return display_findings, display_impression, analysis

# Create the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown(f"# Radiology Report Training System v{APP_VERSION}")
    gr.Markdown("### Practice your chest X-ray reading and reporting skills")
    
    # Add state variables to store actual findings and impression
    actual_findings_state = gr.State("")
    actual_impression_state = gr.State("")
    
    with gr.Tab("DeepSeek Analysis"):
        with gr.Row():
            with gr.Column():
                image_display = gr.Image(label="Chest X-ray Image", type="pil")
                api_key_input = gr.Textbox(label="DeepSeek API Key", type="password")
                hide_truth = gr.Checkbox(label="Hide Ground Truth", value=False)
                load_btn = gr.Button("Load Random Case")
            
            with gr.Column():
                user_findings_input = gr.Textbox(label="Your Findings", lines=10, placeholder="Type or dictate your findings here...")
                ground_truth_findings = gr.Textbox(label="Ground Truth Findings", lines=5, interactive=False)
                ground_truth_impression = gr.Textbox(label="Ground Truth Impression", lines=5, interactive=False)
                analysis_output = gr.Textbox(label="Analysis and Feedback", lines=10, interactive=False)
                submit_btn = gr.Button("Submit Report")

    with gr.Tab("Local Inference"):
        gr.Markdown("### Use Local Models for Transcription and Analysis")
        
        with gr.Row():
            with gr.Column():
                # Transcription Interface
                audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Record or Upload Audio")
                task_input = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
                transcribe_button = gr.Button("Transcribe Audio")
                transcription_output = gr.Textbox(label="Transcription Output", lines=5)
                
                # Load case for comparison
                load_case_btn = gr.Button("Load Random Case for Comparison")
                local_ground_truth_findings = gr.Textbox(label="Ground Truth Findings", lines=5, interactive=False)
                local_ground_truth_impression = gr.Textbox(label="Ground Truth Impression", lines=5, interactive=False)
            
            with gr.Column():
                # Editable transcription and analysis interface
                edited_transcription = gr.Textbox(label="Edit Transcription", lines=10)
                temperature_input = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.6, step=0.1)
                max_tokens_input = gr.Slider(label="Max Tokens", minimum=256, maximum=2048, value=1024, step=128)
                analyze_btn = gr.Button("Analyze with Llama")
                llama_analysis_output = gr.Textbox(label="Llama Analysis Output", lines=15, interactive=False)

        # Event handlers for Local Inference tab
        transcribe_button.click(
            fn=transcribe,
            inputs=[audio_input, task_input],
            outputs=transcription_output
        )
        
        # Copy transcription to editable box
        transcription_output.change(
            fn=lambda x: x,
            inputs=[transcription_output],
            outputs=[edited_transcription]
        )
        
        # Load case for local analysis
        local_image_display = gr.Image(label="Chest X-ray Image", type="pil")  # Add this line
        load_case_btn.click(
            fn=load_random_case,
            inputs=[gr.Checkbox(value=False, visible=False)],  # Hidden checkbox for hide_ground_truth
            outputs=[
                local_image_display,  # Update this line
                local_ground_truth_findings,
                local_ground_truth_impression,
                gr.State(),  # Hidden state
                gr.State()   # Hidden state
            ]
        )
        
        # Analyze with Llama
        analyze_btn.click(
            fn=analyze_with_llama,
            inputs=[
                edited_transcription,
                local_ground_truth_findings,
                local_ground_truth_impression,
                max_tokens_input,
                temperature_input
            ],
            outputs=llama_analysis_output
        )

    # Event handlers for DeepSeek Analysis tab
    load_btn.click(
        fn=load_random_case,
        inputs=[hide_truth],
        outputs=[
            image_display,
            ground_truth_findings,
            ground_truth_impression,
            actual_findings_state,
            actual_impression_state
        ]
    )
    
    submit_btn.click(
        fn=process_case,
        inputs=[
            image_display,
            user_findings_input,
            hide_truth,
            api_key_input,
            ground_truth_findings,
            ground_truth_impression,
            actual_findings_state,
            actual_impression_state
        ],
        outputs=[
            ground_truth_findings,
            ground_truth_impression,
            analysis_output
        ]
    )

    hide_truth.change(
        fn=lambda x, f, i: ("" if x else f, "" if x else i, ""),
        inputs=[hide_truth, actual_findings_state, actual_impression_state],
        outputs=[ground_truth_findings, ground_truth_impression, analysis_output]
    )

logger.info("Starting Gradio interface...")
demo.launch()