VaneshDev commited on
Commit
2adfe45
·
verified ·
1 Parent(s): adf4d71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -58
app.py CHANGED
@@ -2,88 +2,75 @@ 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
 
7
- # Load a more specialized pre-trained model (DenseNet, similar to CheXNet)
8
- model = models.densenet121(pretrained=True) # DenseNet is good for image classification tasks like chest X-rays
9
- model.eval() # Set the model to evaluation mode
10
 
11
- # Define image preprocessing function
12
  def preprocess_image(image):
13
  transform = transforms.Compose([
14
- transforms.Resize((224, 224)), # Resize to fit the model input size
15
  transforms.ToTensor(),
16
  ])
17
  return transform(image).unsqueeze(0)
18
 
19
- # Define a prediction function for X-ray images with detailed output
20
  def predict_xray(image):
21
  image_tensor = preprocess_image(image)
22
  with torch.no_grad():
23
  outputs = model(image_tensor)
24
  probs = torch.nn.functional.softmax(outputs[0], dim=0)
25
-
26
- # Define the conditions (replace these with the actual conditions your model predicts)
27
  conditions = ["Normal", "Pneumonia", "Cancer", "TB", "Other"]
28
  results = {conditions[i]: float(probs[i]) for i in range(len(conditions))}
29
-
30
- # Identify the most likely condition and calculate the confidence
31
  most_likely_condition = max(results, key=results.get)
32
- confidence = results[most_likely_condition] * 100 # Convert to percentage
33
-
34
- # Provide a more detailed summary of the results
35
  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>."
36
-
37
- # Additional detailed descriptions and recommendations for each condition
38
  condition_details = {
39
  "Normal": {
40
  "description": "The X-ray shows no abnormal signs, and the lungs appear healthy.",
41
  "recommendation": "No further tests are required. Continue with regular health check-ups."
42
  },
43
  "Pneumonia": {
44
- "description": "Pneumonia is an infection that causes inflammation in the lungs. It can be caused by bacteria, viruses, or fungi.",
45
- "recommendation": "Consult a healthcare provider immediately for possible antibiotics and further treatment."
46
  },
47
  "Cancer": {
48
- "description": "Lung cancer can manifest in the form of abnormal growths in the lungs. It may require more advanced diagnostic tools like biopsies.",
49
- "recommendation": "Consult an oncologist for further tests and treatment options."
50
  },
51
  "TB": {
52
- "description": "Tuberculosis (TB) is a serious bacterial infection that mainly affects the lungs. It is treatable but requires proper care.",
53
- "recommendation": "Seek immediate medical attention for a proper treatment plan, which may include antibiotics."
54
  },
55
  "Other": {
56
- "description": "The X-ray may show signs of other possible conditions that need further investigation.",
57
- "recommendation": "Consult with a doctor to rule out any other serious conditions."
58
  }
59
  }
60
-
61
- # Displaying the results in a structured way (bullet points)
62
  detailed_results = "<ul>"
63
  for condition, prob in results.items():
64
  detailed_results += f"<li><b>{condition}:</b> {prob*100:.2f}%</li>"
65
  detailed_results += "</ul>"
66
-
67
- # Additional feedback for the most likely condition
68
- additional_feedback = condition_details.get(most_likely_condition, "Please consult with a doctor for further details.")
69
-
70
  return summary, detailed_results, additional_feedback
71
 
72
- # Define a function to read and analyze patient reports (PDFs)
73
  def analyze_report(file):
74
  text = ""
75
  if file.name.endswith(".pdf"):
76
  pdf_reader = PyPDF2.PdfReader(file)
77
  for page in pdf_reader.pages:
78
  text += page.extract_text()
79
- # For simplicity, we are just summarizing the first 300 characters
80
  report_summary = f"Patient Report (Preview): {text[:300]}..."
81
  return report_summary
82
 
83
- # Gradio Interface with enhanced UI
84
  def create_interface():
85
  with gr.Blocks() as demo:
86
- # Custom CSS for UI
87
  custom_css = """
88
  .gradio-container {
89
  background-color: #f4f6f9;
@@ -110,42 +97,38 @@ def create_interface():
110
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
111
  margin-top: 20px;
112
  }
113
- .result-list {
114
- padding-left: 20px;
115
- }
116
- .result-summary {
117
- font-size: 18px;
118
- color: #2F4F4F;
119
- }
120
  """
121
-
122
- # Title section
123
  gr.Markdown("<h1 class='title'>RadiologyScan AI</h1>")
124
-
125
- # Upload X-ray image section
 
 
 
 
 
 
 
 
 
 
126
  with gr.Row():
127
  xray_input = gr.Image(label="Upload Chest X-ray", type="pil")
128
  report_input = gr.File(label="Upload Patient Report (PDF)", file_count="single")
129
-
130
- # Buttons for analysis
131
  with gr.Row():
132
  predict_button = gr.Button("Analyze X-ray", elem_classes="gradio-button")
133
  report_button = gr.Button("Analyze Report", elem_classes="gradio-button")
134
-
135
- # Results section for the X-ray image
136
- xray_output = gr.HTML(label="X-ray Diagnosis Summary", elem_classes="result-box") # Removed interactive=False
137
- xray_result = gr.HTML(label="Detailed X-ray Results", elem_classes="result-box") # Removed interactive=False
138
  additional_feedback = gr.Textbox(label="Additional Feedback", interactive=False, elem_classes="result-box")
139
-
140
- # Results section for the patient report
141
  report_output = gr.Textbox(label="Report Summary", interactive=False, elem_classes="result-box")
142
-
143
- # Event handlers for buttons
144
  predict_button.click(predict_xray, inputs=xray_input, outputs=[xray_output, xray_result, additional_feedback])
145
  report_button.click(analyze_report, inputs=report_input, outputs=report_output)
146
-
147
  return demo
148
 
149
- # Launch the Gradio interface
150
  demo = create_interface()
151
- demo.launch(share=True)
 
2
  from PIL import Image
3
  import torch
4
  from torchvision import models, transforms
5
+ import PyPDF2
6
 
7
+ model = models.densenet121(pretrained=True)
8
+ model.eval()
 
9
 
 
10
  def preprocess_image(image):
11
  transform = transforms.Compose([
12
+ transforms.Resize((224, 224)),
13
  transforms.ToTensor(),
14
  ])
15
  return transform(image).unsqueeze(0)
16
 
 
17
  def predict_xray(image):
18
  image_tensor = preprocess_image(image)
19
  with torch.no_grad():
20
  outputs = model(image_tensor)
21
  probs = torch.nn.functional.softmax(outputs[0], dim=0)
22
+
 
23
  conditions = ["Normal", "Pneumonia", "Cancer", "TB", "Other"]
24
  results = {conditions[i]: float(probs[i]) for i in range(len(conditions))}
25
+
 
26
  most_likely_condition = max(results, key=results.get)
27
+ confidence = results[most_likely_condition] * 100
28
+
 
29
  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>."
30
+
 
31
  condition_details = {
32
  "Normal": {
33
  "description": "The X-ray shows no abnormal signs, and the lungs appear healthy.",
34
  "recommendation": "No further tests are required. Continue with regular health check-ups."
35
  },
36
  "Pneumonia": {
37
+ "description": "Pneumonia is an infection that causes inflammation in the lungs.",
38
+ "recommendation": "Consult a healthcare provider for treatment."
39
  },
40
  "Cancer": {
41
+ "description": "Lung cancer may appear as abnormal growths in the lungs.",
42
+ "recommendation": "Consult an oncologist for further diagnostic procedures."
43
  },
44
  "TB": {
45
+ "description": "Tuberculosis is a bacterial infection that affects the lungs.",
46
+ "recommendation": "Seek immediate medical attention for treatment."
47
  },
48
  "Other": {
49
+ "description": "There may be other conditions requiring investigation.",
50
+ "recommendation": "Consult a radiologist for further analysis."
51
  }
52
  }
53
+
 
54
  detailed_results = "<ul>"
55
  for condition, prob in results.items():
56
  detailed_results += f"<li><b>{condition}:</b> {prob*100:.2f}%</li>"
57
  detailed_results += "</ul>"
58
+
59
+ additional_feedback = condition_details.get(most_likely_condition, "Consult a doctor for more details.")
60
+
 
61
  return summary, detailed_results, additional_feedback
62
 
 
63
  def analyze_report(file):
64
  text = ""
65
  if file.name.endswith(".pdf"):
66
  pdf_reader = PyPDF2.PdfReader(file)
67
  for page in pdf_reader.pages:
68
  text += page.extract_text()
 
69
  report_summary = f"Patient Report (Preview): {text[:300]}..."
70
  return report_summary
71
 
 
72
  def create_interface():
73
  with gr.Blocks() as demo:
 
74
  custom_css = """
75
  .gradio-container {
76
  background-color: #f4f6f9;
 
97
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
98
  margin-top: 20px;
99
  }
 
 
 
 
 
 
 
100
  """
101
+
 
102
  gr.Markdown("<h1 class='title'>RadiologyScan AI</h1>")
103
+ gr.Markdown("""
104
+ <h3>🩻 Radiology Areas Covered:</h3>
105
+ <table style='width:100%; border: 1px solid #ccc;'>
106
+ <tr><th>Area</th><th>Common Tools</th><th>Focused Problems</th></tr>
107
+ <tr><td>Lungs</td><td>X-ray, CT</td><td>Pneumonia, TB, Lung cancer</td></tr>
108
+ <tr><td>Brain</td><td>MRI, CT</td><td>Stroke, Tumors</td></tr>
109
+ <tr><td>Bones/Joints</td><td>X-ray, CT, MRI</td><td>Fractures, Arthritis</td></tr>
110
+ <tr><td>Abdomen/Pelvis</td><td>Ultrasound, CT</td><td>Liver/kidney issues, tumors, appendicitis</td></tr>
111
+ <tr><td>Cancer Anywhere</td><td>MRI, CT, PET</td><td>Tumors, cancer spread, biopsy guidance</td></tr>
112
+ </table>
113
+ """)
114
+
115
  with gr.Row():
116
  xray_input = gr.Image(label="Upload Chest X-ray", type="pil")
117
  report_input = gr.File(label="Upload Patient Report (PDF)", file_count="single")
118
+
 
119
  with gr.Row():
120
  predict_button = gr.Button("Analyze X-ray", elem_classes="gradio-button")
121
  report_button = gr.Button("Analyze Report", elem_classes="gradio-button")
122
+
123
+ xray_output = gr.HTML(label="X-ray Diagnosis Summary", elem_classes="result-box")
124
+ xray_result = gr.HTML(label="Detailed X-ray Results", elem_classes="result-box")
 
125
  additional_feedback = gr.Textbox(label="Additional Feedback", interactive=False, elem_classes="result-box")
 
 
126
  report_output = gr.Textbox(label="Report Summary", interactive=False, elem_classes="result-box")
127
+
 
128
  predict_button.click(predict_xray, inputs=xray_input, outputs=[xray_output, xray_result, additional_feedback])
129
  report_button.click(analyze_report, inputs=report_input, outputs=report_output)
130
+
131
  return demo
132
 
 
133
  demo = create_interface()
134
+ demo.launch(share=True)