|
import gradio as gr |
|
from PIL import Image |
|
import torch |
|
from torchvision import models, transforms |
|
import PyPDF2 |
|
import io |
|
|
|
|
|
model = models.resnet18(pretrained=True) |
|
model.eval() |
|
|
|
|
|
def preprocess_image(image): |
|
transform = transforms.Compose([ |
|
transforms.Resize((224, 224)), |
|
transforms.ToTensor(), |
|
]) |
|
return transform(image).unsqueeze(0) |
|
|
|
|
|
def predict_xray(image): |
|
image_tensor = preprocess_image(image) |
|
with torch.no_grad(): |
|
outputs = model(image_tensor) |
|
probs = torch.nn.functional.softmax(outputs[0], dim=0) |
|
|
|
|
|
conditions = ["Normal", "Pneumonia", "Cancer", "TB", "Other"] |
|
results = {conditions[i]: float(probs[i]) for i in range(len(conditions))} |
|
summary = f"Summary: Based on the X-ray, the patient is diagnosed with: {max(results, key=results.get)}" |
|
return summary, results |
|
|
|
|
|
def analyze_report(file): |
|
text = "" |
|
if file.name.endswith(".pdf"): |
|
pdf_reader = PyPDF2.PdfReader(file) |
|
for page in pdf_reader.pages: |
|
text += page.extract_text() |
|
|
|
|
|
report_summary = f"Patient Report: {text[:300]}..." |
|
return report_summary |
|
|
|
|
|
def create_interface(): |
|
with gr.Blocks() as demo: |
|
|
|
custom_css = """ |
|
.gradio-container { |
|
background-color: #f4f6f9; |
|
border-radius: 15px; |
|
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); |
|
padding: 30px; |
|
font-family: 'Segoe UI', sans-serif; |
|
} |
|
.title { |
|
font-size: 30px; |
|
text-align: center; |
|
color: #4C6A92; |
|
} |
|
.gradio-button { |
|
background-color: #3B82F6; |
|
color: white; |
|
border-radius: 10px; |
|
padding: 15px; |
|
} |
|
.result-box { |
|
background-color: #ffffff; |
|
border-radius: 10px; |
|
padding: 20px; |
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); |
|
margin-top: 20px; |
|
} |
|
""" |
|
|
|
|
|
gr.Markdown("<h1 class='title'>RadiologyScan AI</h1>") |
|
|
|
|
|
with gr.Row(): |
|
xray_input = gr.Image(label="Upload Chest X-ray", type="pil") |
|
report_input = gr.File(label="Upload Patient Report (PDF)", file_count="single") |
|
|
|
|
|
with gr.Row(): |
|
predict_button = gr.Button("Analyze X-ray", elem_classes="gradio-button") |
|
report_button = gr.Button("Analyze Report", elem_classes="gradio-button") |
|
|
|
|
|
xray_output = gr.Textbox(label="X-ray Summary", interactive=False, elem_classes="result-box") |
|
xray_result = gr.JSON(label="X-ray Results", interactive=False, elem_classes="result-box") |
|
|
|
|
|
report_output = gr.Textbox(label="Report Summary", interactive=False, elem_classes="result-box") |
|
|
|
|
|
predict_button.click(predict_xray, inputs=xray_input, outputs=[xray_output, xray_result]) |
|
report_button.click(analyze_report, inputs=report_input, outputs=report_output) |
|
|
|
return demo |
|
|
|
|
|
demo = create_interface() |
|
demo.launch(share=True) |
|
|