VaneshDev commited on
Commit
b80bff3
·
verified ·
1 Parent(s): 5a31d0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -1,17 +1,17 @@
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
 
7
- # Load the pre-trained model (ResNet18 with new weight parameter)
8
- model = models.resnet18(weights="IMAGENET1K_V1") # Updated to the new weight parameter in torchvision 0.13+
9
- model.eval()
10
 
11
  # Define image preprocessing function
12
  def preprocess_image(image):
13
  transform = transforms.Compose([
14
- transforms.Resize((224, 224)),
15
  transforms.ToTensor(),
16
  ])
17
  return transform(image).unsqueeze(0)
@@ -27,7 +27,7 @@ def predict_xray(image):
27
  conditions = ["Normal", "Pneumonia", "Cancer", "TB", "Other"]
28
  results = {conditions[i]: float(probs[i]) for i in range(len(conditions))}
29
 
30
- # Determine the most likely condition and provide detailed feedback
31
  most_likely_condition = max(results, key=results.get)
32
  confidence = results[most_likely_condition] * 100 # Convert to percentage
33
 
@@ -64,7 +64,7 @@ def predict_xray(image):
64
  detailed_results += f"<li><b>{condition}:</b> {prob*100:.2f}%</li>"
65
  detailed_results += "</ul>"
66
 
67
- # Additional advice based on 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
 
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
 
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)
 
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
 
 
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