Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
from torchvision import models, transforms
|
| 5 |
-
import PyPDF2
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
|
|
@@ -53,7 +52,7 @@ def preprocess_image(image):
|
|
| 53 |
def predict_xray(image):
|
| 54 |
try:
|
| 55 |
if image is None:
|
| 56 |
-
return "Error: No image uploaded.", ""
|
| 57 |
|
| 58 |
image_tensor = preprocess_image(image)
|
| 59 |
with torch.no_grad():
|
|
@@ -61,62 +60,19 @@ def predict_xray(image):
|
|
| 61 |
probs = torch.nn.functional.softmax(outputs, dim=1)[0] # Softmax over all conditions
|
| 62 |
results = {conditions[i]: float(probs[i].cpu().numpy()) * 100 for i in range(len(conditions))}
|
| 63 |
|
| 64 |
-
#
|
| 65 |
most_likely_condition = max(results, key=results.get)
|
| 66 |
-
|
| 67 |
-
# Check if the predicted class is in the list of valid conditions
|
| 68 |
-
if most_likely_condition not in conditions:
|
| 69 |
-
logger.warning(f"Unexpected condition predicted: {most_likely_condition}. Defaulting to 'Other'.")
|
| 70 |
-
most_likely_condition = "Other"
|
| 71 |
-
confidence = 0.0
|
| 72 |
-
else:
|
| 73 |
-
confidence = results[most_likely_condition]
|
| 74 |
-
|
| 75 |
-
# Create a detailed summary of results
|
| 76 |
-
summary = f"**Summary**: Based on the X-ray analysis, the most likely diagnosis is: <b>{most_likely_condition}</b> with a confidence of <b>{confidence:.2f}%</b>."
|
| 77 |
-
|
| 78 |
-
# Enhanced condition details for each disease/condition
|
| 79 |
-
condition_details = {
|
| 80 |
-
"Normal": {"description": "No abnormal signs detected.", "recommendation": "Routine check-ups recommended."},
|
| 81 |
-
"Pneumonia": {"description": "Lung inflammation detected, possibly infectious.", "recommendation": "Seek medical attention for treatment."},
|
| 82 |
-
"Cancer": {"description": "Suspicious masses suggest cancer; further imaging needed.", "recommendation": "Consult an oncologist."},
|
| 83 |
-
"TB": {"description": "Cavitary lesions indicate tuberculosis.", "recommendation": "Immediate medical evaluation required."},
|
| 84 |
-
"Other": {"description": "Unclear abnormality; further investigation needed.", "recommendation": "Consult a radiologist."},
|
| 85 |
-
"Fractures": {"description": "Bone break detected.", "recommendation": "Orthopedic evaluation."},
|
| 86 |
-
"COPD": {"description": "Lung damage from COPD observed.", "recommendation": "Pulmonary consultation."},
|
| 87 |
-
# Add the rest of the conditions as needed...
|
| 88 |
-
}
|
| 89 |
|
| 90 |
-
# Display
|
| 91 |
-
|
| 92 |
-
for condition, prob in results.items():
|
| 93 |
-
detailed_results += f"<li><b>{condition}:</b> {prob:.2f}%</li>"
|
| 94 |
-
detailed_results += "</ul>"
|
| 95 |
-
|
| 96 |
-
additional_feedback = f"<div class='feedback-box'><b>Description:</b> {condition_details[most_likely_condition]['description']}<br><b>Recommendation:</b> {condition_details[most_likely_condition]['recommendation']}</div>"
|
| 97 |
|
| 98 |
logger.info(f"Prediction: {most_likely_condition} with confidence {confidence:.2f}%")
|
| 99 |
-
return summary
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
logger.error(f"Error in predict_xray: {str(e)}")
|
| 103 |
-
return f"Error: {str(e)}", ""
|
| 104 |
-
|
| 105 |
-
# Define function to read and analyze patient reports (PDFs)
|
| 106 |
-
def analyze_report(file):
|
| 107 |
-
text = ""
|
| 108 |
-
if file and file.name.endswith(".pdf"):
|
| 109 |
-
try:
|
| 110 |
-
pdf_reader = PyPDF2.PdfReader(file)
|
| 111 |
-
for page in pdf_reader.pages:
|
| 112 |
-
text += page.extract_text() or ""
|
| 113 |
-
report_summary = f"Patient Report (Preview): {text[:300]}..." if text else "No readable text found in the PDF."
|
| 114 |
-
except Exception as e:
|
| 115 |
-
logger.error(f"Error reading PDF: {str(e)}")
|
| 116 |
-
report_summary = f"Error processing PDF: {str(e)}"
|
| 117 |
-
else:
|
| 118 |
-
report_summary = "Please upload a valid PDF file."
|
| 119 |
-
return report_summary
|
| 120 |
|
| 121 |
# Gradio Interface with enhanced UI
|
| 122 |
def create_interface():
|
|
@@ -127,9 +83,6 @@ def create_interface():
|
|
| 127 |
.gradio-button { background-color: #3B82F6; color: white; border-radius: 10px; padding: 15px 30px; font-size: 16px; transition: background-color 0.3s; }
|
| 128 |
.gradio-button:hover { background-color: #2563EB; }
|
| 129 |
.result-box { background-color: #ffffff; border-radius: 10px; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); margin-top: 20px; max-width: 100%; }
|
| 130 |
-
.result-list { padding-left: 20px; margin: 10px 0; }
|
| 131 |
-
.result-summary { font-size: 18px; color: #2F4F4F; font-weight: 500; }
|
| 132 |
-
.feedback-box { background-color: #F0FFF4; padding: 10px; border-left: 4px solid #38A169; border-radius: 5px; margin-top: 10px; }
|
| 133 |
"""
|
| 134 |
|
| 135 |
gr.Markdown("<h1 class='title'>RadiologyScan AI</h1>")
|
|
@@ -138,28 +91,18 @@ def create_interface():
|
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column(scale=1):
|
| 140 |
xray_input = gr.Image(label="Upload X-ray", type="pil", elem_id="xray-input")
|
| 141 |
-
with gr.Column(scale=1):
|
| 142 |
-
report_input = gr.File(label="Upload Patient Report (PDF)", file_count="single", elem_id="report-input")
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
predict_button = gr.Button("Analyze X-ray", elem_classes="gradio-button")
|
| 146 |
-
report_button = gr.Button("Analyze Report", elem_classes="gradio-button")
|
| 147 |
|
| 148 |
with gr.Column():
|
| 149 |
-
xray_output = gr.HTML(label="
|
| 150 |
-
xray_result = gr.HTML(label="Detailed X-ray Results", elem_classes="result-box")
|
| 151 |
-
additional_feedback = gr.HTML(label="Additional Feedback", elem_classes="result-box")
|
| 152 |
-
report_output = gr.Textbox(label="Report Summary", interactive=False, elem_classes="result-box")
|
| 153 |
|
| 154 |
predict_button.click(
|
| 155 |
fn=predict_xray,
|
| 156 |
inputs=xray_input,
|
| 157 |
-
outputs=
|
| 158 |
)
|
| 159 |
-
report_button.click(
|
| 160 |
-
fn=analyze_report,
|
| 161 |
-
inputs=report_input,
|
| 162 |
-
outputs=report_output)
|
| 163 |
|
| 164 |
return demo
|
| 165 |
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
from torchvision import models, transforms
|
|
|
|
| 5 |
import logging
|
| 6 |
import os
|
| 7 |
|
|
|
|
| 52 |
def predict_xray(image):
|
| 53 |
try:
|
| 54 |
if image is None:
|
| 55 |
+
return "Error: No image uploaded.", ""
|
| 56 |
|
| 57 |
image_tensor = preprocess_image(image)
|
| 58 |
with torch.no_grad():
|
|
|
|
| 60 |
probs = torch.nn.functional.softmax(outputs, dim=1)[0] # Softmax over all conditions
|
| 61 |
results = {conditions[i]: float(probs[i].cpu().numpy()) * 100 for i in range(len(conditions))}
|
| 62 |
|
| 63 |
+
# Get the most likely condition
|
| 64 |
most_likely_condition = max(results, key=results.get)
|
| 65 |
+
confidence = results[most_likely_condition]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Display only the diagnosis summary
|
| 68 |
+
summary = f"**Diagnosis**: The most likely diagnosis is: <b>{most_likely_condition}</b> with a confidence of <b>{confidence:.2f}%</b>."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
logger.info(f"Prediction: {most_likely_condition} with confidence {confidence:.2f}%")
|
| 71 |
+
return summary
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
logger.error(f"Error in predict_xray: {str(e)}")
|
| 75 |
+
return f"Error: {str(e)}", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Gradio Interface with enhanced UI
|
| 78 |
def create_interface():
|
|
|
|
| 83 |
.gradio-button { background-color: #3B82F6; color: white; border-radius: 10px; padding: 15px 30px; font-size: 16px; transition: background-color 0.3s; }
|
| 84 |
.gradio-button:hover { background-color: #2563EB; }
|
| 85 |
.result-box { background-color: #ffffff; border-radius: 10px; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); margin-top: 20px; max-width: 100%; }
|
|
|
|
|
|
|
|
|
|
| 86 |
"""
|
| 87 |
|
| 88 |
gr.Markdown("<h1 class='title'>RadiologyScan AI</h1>")
|
|
|
|
| 91 |
with gr.Row():
|
| 92 |
with gr.Column(scale=1):
|
| 93 |
xray_input = gr.Image(label="Upload X-ray", type="pil", elem_id="xray-input")
|
|
|
|
|
|
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
predict_button = gr.Button("Analyze X-ray", elem_classes="gradio-button")
|
|
|
|
| 97 |
|
| 98 |
with gr.Column():
|
| 99 |
+
xray_output = gr.HTML(label="Diagnosis Summary", elem_classes="result-box")
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
predict_button.click(
|
| 102 |
fn=predict_xray,
|
| 103 |
inputs=xray_input,
|
| 104 |
+
outputs=xray_output
|
| 105 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
return demo
|
| 108 |
|