Spaces:
Sleeping
Sleeping
kgupta21
commited on
Commit
·
5b7e38a
1
Parent(s):
ecd47e6
Add enhanced radiology teaching application with logging and version tracking
Browse files
app.py
CHANGED
@@ -5,10 +5,25 @@ from openai import OpenAI
|
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
import base64
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def encode_image_to_base64(image_bytes):
|
14 |
return base64.b64encode(image_bytes).decode('utf-8')
|
@@ -19,6 +34,7 @@ def analyze_report(user_findings, ground_truth_findings, ground_truth_impression
|
|
19 |
|
20 |
try:
|
21 |
client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
|
|
|
22 |
|
23 |
prompt = f"""You are an expert radiologist reviewing a trainee's chest X-ray report.
|
24 |
|
@@ -49,18 +65,24 @@ def analyze_report(user_findings, ground_truth_findings, ground_truth_impression
|
|
49 |
|
50 |
return response.choices[0].message.content
|
51 |
except Exception as e:
|
|
|
52 |
return f"Error analyzing report: {str(e)}"
|
53 |
|
54 |
def load_random_case(hide_ground_truth):
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
def process_case(image, user_findings, hide_ground_truth, api_key, current_findings="", current_impression=""):
|
66 |
if hide_ground_truth:
|
@@ -71,7 +93,7 @@ def process_case(image, user_findings, hide_ground_truth, api_key, current_findi
|
|
71 |
|
72 |
# Create the Gradio interface
|
73 |
with gr.Blocks() as demo:
|
74 |
-
gr.Markdown("# Radiology Report Training System")
|
75 |
gr.Markdown("### Practice your chest X-ray reading and reporting skills")
|
76 |
|
77 |
with gr.Row():
|
@@ -118,4 +140,5 @@ with gr.Blocks() as demo:
|
|
118 |
outputs=[ground_truth_findings, ground_truth_impression, analysis_output]
|
119 |
)
|
120 |
|
|
|
121 |
demo.launch()
|
|
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
import base64
|
8 |
+
import logging
|
9 |
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO)
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
+
|
14 |
+
# App version
|
15 |
+
APP_VERSION = "1.0.0"
|
16 |
+
logger.info(f"Starting Radiology Teaching App v{APP_VERSION}")
|
17 |
+
|
18 |
+
try:
|
19 |
+
# Load only 10 rows from the dataset
|
20 |
+
logger.info("Loading MIMIC-CXR dataset...")
|
21 |
+
dataset = load_dataset("itsanmolgupta/mimic-cxr-dataset", split="train").select(range(10))
|
22 |
+
df = pd.DataFrame(dataset)
|
23 |
+
logger.info(f"Successfully loaded {len(df)} cases")
|
24 |
+
except Exception as e:
|
25 |
+
logger.error(f"Error loading dataset: {str(e)}")
|
26 |
+
raise
|
27 |
|
28 |
def encode_image_to_base64(image_bytes):
|
29 |
return base64.b64encode(image_bytes).decode('utf-8')
|
|
|
34 |
|
35 |
try:
|
36 |
client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
|
37 |
+
logger.info("Analyzing report with DeepSeek...")
|
38 |
|
39 |
prompt = f"""You are an expert radiologist reviewing a trainee's chest X-ray report.
|
40 |
|
|
|
65 |
|
66 |
return response.choices[0].message.content
|
67 |
except Exception as e:
|
68 |
+
logger.error(f"Error in report analysis: {str(e)}")
|
69 |
return f"Error analyzing report: {str(e)}"
|
70 |
|
71 |
def load_random_case(hide_ground_truth):
|
72 |
+
try:
|
73 |
+
# Randomly select a case from our dataset
|
74 |
+
random_case = df.sample(n=1).iloc[0]
|
75 |
+
logger.info("Loading random case...")
|
76 |
+
|
77 |
+
# Get the image, findings, and impression
|
78 |
+
image = random_case['image']
|
79 |
+
findings = "" if hide_ground_truth else random_case['findings']
|
80 |
+
impression = "" if hide_ground_truth else random_case['impression']
|
81 |
+
|
82 |
+
return image, findings, impression
|
83 |
+
except Exception as e:
|
84 |
+
logger.error(f"Error loading random case: {str(e)}")
|
85 |
+
return None, "Error loading case", "Error loading case"
|
86 |
|
87 |
def process_case(image, user_findings, hide_ground_truth, api_key, current_findings="", current_impression=""):
|
88 |
if hide_ground_truth:
|
|
|
93 |
|
94 |
# Create the Gradio interface
|
95 |
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown(f"# Radiology Report Training System v{APP_VERSION}")
|
97 |
gr.Markdown("### Practice your chest X-ray reading and reporting skills")
|
98 |
|
99 |
with gr.Row():
|
|
|
140 |
outputs=[ground_truth_findings, ground_truth_impression, analysis_output]
|
141 |
)
|
142 |
|
143 |
+
logger.info("Starting Gradio interface...")
|
144 |
demo.launch()
|