Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import models, transforms
|
| 5 |
+
import PyPDF2 # For reading patient reports (PDFs)
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Load the pre-trained model (for example, ResNet18)
|
| 9 |
+
model = models.resnet18(pretrained=True)
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
# Define image preprocessing function
|
| 13 |
+
def preprocess_image(image):
|
| 14 |
+
transform = transforms.Compose([
|
| 15 |
+
transforms.Resize((224, 224)),
|
| 16 |
+
transforms.ToTensor(),
|
| 17 |
+
])
|
| 18 |
+
return transform(image).unsqueeze(0)
|
| 19 |
+
|
| 20 |
+
# Define a simple prediction function for X-ray images
|
| 21 |
+
def predict_xray(image):
|
| 22 |
+
image_tensor = preprocess_image(image)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(image_tensor)
|
| 25 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=0)
|
| 26 |
+
|
| 27 |
+
# Example output - replace with actual classes based on your model
|
| 28 |
+
conditions = ["Normal", "Pneumonia", "Cancer", "TB", "Other"]
|
| 29 |
+
results = {conditions[i]: float(probs[i]) for i in range(len(conditions))}
|
| 30 |
+
summary = f"Summary: Based on the X-ray, the patient is diagnosed with: {max(results, key=results.get)}"
|
| 31 |
+
return summary, results
|
| 32 |
+
|
| 33 |
+
# Define a function to read and analyze patient reports (PDFs)
|
| 34 |
+
def analyze_report(file):
|
| 35 |
+
text = ""
|
| 36 |
+
if file.name.endswith(".pdf"):
|
| 37 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
| 38 |
+
for page in pdf_reader.pages:
|
| 39 |
+
text += page.extract_text()
|
| 40 |
+
# You can process the extracted text and provide insights
|
| 41 |
+
# For now, let's assume the text contains diagnosis
|
| 42 |
+
report_summary = f"Patient Report: {text[:300]}..." # First 300 characters of report as preview
|
| 43 |
+
return report_summary
|
| 44 |
+
|
| 45 |
+
# Gradio Interface
|
| 46 |
+
def create_interface():
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
# Custom CSS for UI
|
| 49 |
+
custom_css = """
|
| 50 |
+
.gradio-container {
|
| 51 |
+
background-color: #f4f6f9;
|
| 52 |
+
border-radius: 15px;
|
| 53 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
| 54 |
+
padding: 30px;
|
| 55 |
+
font-family: 'Segoe UI', sans-serif;
|
| 56 |
+
}
|
| 57 |
+
.title {
|
| 58 |
+
font-size: 30px;
|
| 59 |
+
text-align: center;
|
| 60 |
+
color: #4C6A92;
|
| 61 |
+
}
|
| 62 |
+
.gradio-button {
|
| 63 |
+
background-color: #3B82F6;
|
| 64 |
+
color: white;
|
| 65 |
+
border-radius: 10px;
|
| 66 |
+
padding: 15px;
|
| 67 |
+
}
|
| 68 |
+
.result-box {
|
| 69 |
+
background-color: #ffffff;
|
| 70 |
+
border-radius: 10px;
|
| 71 |
+
padding: 20px;
|
| 72 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 73 |
+
margin-top: 20px;
|
| 74 |
+
}
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
# Title section
|
| 78 |
+
gr.Markdown("<h1 class='title'>RadiologyScan AI</h1>")
|
| 79 |
+
|
| 80 |
+
# Upload X-ray image section
|
| 81 |
+
with gr.Row():
|
| 82 |
+
xray_input = gr.Image(label="Upload Chest X-ray", type="pil")
|
| 83 |
+
report_input = gr.File(label="Upload Patient Report (PDF)", file_count="single")
|
| 84 |
+
|
| 85 |
+
# Buttons for analysis
|
| 86 |
+
with gr.Row():
|
| 87 |
+
predict_button = gr.Button("Analyze X-ray", elem_classes="gradio-button")
|
| 88 |
+
report_button = gr.Button("Analyze Report", elem_classes="gradio-button")
|
| 89 |
+
|
| 90 |
+
# Results section for the X-ray image
|
| 91 |
+
xray_output = gr.Textbox(label="X-ray Summary", interactive=False, elem_classes="result-box")
|
| 92 |
+
xray_result = gr.JSON(label="X-ray Results", interactive=False, elem_classes="result-box")
|
| 93 |
+
|
| 94 |
+
# Results section for the patient report
|
| 95 |
+
report_output = gr.Textbox(label="Report Summary", interactive=False, elem_classes="result-box")
|
| 96 |
+
|
| 97 |
+
# Event handlers for buttons
|
| 98 |
+
predict_button.click(predict_xray, inputs=xray_input, outputs=[xray_output, xray_result])
|
| 99 |
+
report_button.click(analyze_report, inputs=report_input, outputs=report_output)
|
| 100 |
+
|
| 101 |
+
return demo
|
| 102 |
+
|
| 103 |
+
# Launch the Gradio interface
|
| 104 |
+
demo = create_interface()
|
| 105 |
+
demo.launch(share=True)
|