VaneshDev commited on
Commit
99e561f
·
verified ·
1 Parent(s): 49f356e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -49,7 +49,7 @@ def preprocess_image(image):
49
  logger.debug(f"Preprocessed image tensor shape: {image_tensor.shape}")
50
  return image_tensor
51
 
52
- # Define prediction function with detailed output
53
  def predict_xray(image):
54
  try:
55
  if image is None:
@@ -61,12 +61,20 @@ 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
  most_likely_condition = max(results, key=results.get)
65
- confidence = results[most_likely_condition]
66
-
 
 
 
 
 
 
 
67
  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>."
68
 
69
- # Enhanced condition details
70
  condition_details = {
71
  "Normal": {"description": "No abnormal signs detected.", "recommendation": "Routine check-ups recommended."},
72
  "Pneumonia": {"description": "Lung inflammation detected, possibly infectious.", "recommendation": "Seek medical attention for treatment."},
@@ -78,6 +86,7 @@ def predict_xray(image):
78
  # Add the rest of the conditions as needed...
79
  }
80
 
 
81
  detailed_results = "<ul class='result-list'>"
82
  for condition, prob in results.items():
83
  detailed_results += f"<li><b>{condition}:</b> {prob:.2f}%</li>"
 
49
  logger.debug(f"Preprocessed image tensor shape: {image_tensor.shape}")
50
  return image_tensor
51
 
52
+ # Define prediction function with detailed output and error handling
53
  def predict_xray(image):
54
  try:
55
  if image is None:
 
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
+ # Handle case where predicted class might not be in our list of conditions
65
  most_likely_condition = max(results, key=results.get)
66
+
67
+ # Ensure the predicted condition is in the valid conditions list
68
+ if most_likely_condition not in conditions:
69
+ most_likely_condition = "Other"
70
+ confidence = 0.0
71
+ else:
72
+ confidence = results[most_likely_condition]
73
+
74
+ # Create a detailed summary of results
75
  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>."
76
 
77
+ # Enhanced condition details for each disease/condition
78
  condition_details = {
79
  "Normal": {"description": "No abnormal signs detected.", "recommendation": "Routine check-ups recommended."},
80
  "Pneumonia": {"description": "Lung inflammation detected, possibly infectious.", "recommendation": "Seek medical attention for treatment."},
 
86
  # Add the rest of the conditions as needed...
87
  }
88
 
89
+ # Display results in a clear format
90
  detailed_results = "<ul class='result-list'>"
91
  for condition, prob in results.items():
92
  detailed_results += f"<li><b>{condition}:</b> {prob:.2f}%</li>"