SoulMind01 commited on
Commit
4d30d15
·
1 Parent(s): 9163080

Added disclaimer

Browse files
Files changed (1) hide show
  1. app.py +34 -26
app.py CHANGED
@@ -13,13 +13,6 @@ CONFIDENCE_THRESHOLD = 0.7
13
 
14
 
15
  def preprocess_image(image):
16
- """
17
- Preprocesses the input image for the model.
18
- Args:
19
- image (PIL.Image): Input image.
20
- Returns:
21
- numpy.ndarray: Preprocessed image ready for prediction.
22
- """
23
  img = image.convert("RGB") # Ensure the image is RGB
24
  img = img.resize((128, 128)) # Resize to model's input size
25
  img_array = np.array(img) / 255.0 # Normalize pixel values to [0, 1]
@@ -28,19 +21,10 @@ def preprocess_image(image):
28
 
29
 
30
  def predict_image(image):
31
- """
32
- Predicts the class of the input image with confidence-based filtering.
33
- Args:
34
- image (PIL.Image): Input image.
35
- Returns:
36
- str: Predicted class label or uncertainty message.
37
- float: Confidence score (if applicable).
38
- """
39
  img_array = preprocess_image(image)
40
  prediction = model.predict(img_array)
41
  confidence = np.max(prediction)
42
 
43
- # Apply confidence threshold
44
  if confidence < CONFIDENCE_THRESHOLD:
45
  return "Uncertain: Low confidence", confidence
46
 
@@ -48,15 +32,39 @@ def predict_image(image):
48
  return predicted_class, confidence
49
 
50
 
51
- # Create a Gradio interface
52
- interface = gr.Interface(
53
- fn=predict_image,
54
- inputs=gr.Image(type="pil"),
55
- outputs=[gr.Textbox(label="Predicted Class"), gr.Textbox(label="Confidence")],
56
- title="Pneumonia Detection CNN",
57
- description="Upload an image to classify it as NORMAL or PNEUMONIA.",
58
- )
59
 
60
- # Launch the interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  if __name__ == "__main__":
62
- interface.launch(server_name="0.0.0.0", server_port=7860)
 
13
 
14
 
15
  def preprocess_image(image):
 
 
 
 
 
 
 
16
  img = image.convert("RGB") # Ensure the image is RGB
17
  img = img.resize((128, 128)) # Resize to model's input size
18
  img_array = np.array(img) / 255.0 # Normalize pixel values to [0, 1]
 
21
 
22
 
23
  def predict_image(image):
 
 
 
 
 
 
 
 
24
  img_array = preprocess_image(image)
25
  prediction = model.predict(img_array)
26
  confidence = np.max(prediction)
27
 
 
28
  if confidence < CONFIDENCE_THRESHOLD:
29
  return "Uncertain: Low confidence", confidence
30
 
 
32
  return predicted_class, confidence
33
 
34
 
35
+ def acknowledge(agree):
36
+ if agree:
37
+ return (
38
+ gr.update(visible=True),
39
+ "Thank you for acknowledging the disclaimer. You may now use the app.",
40
+ )
41
+ else:
42
+ return gr.update(visible=False), "You must accept the disclaimer to proceed."
43
 
44
+
45
+ # Create a Gradio interface using Blocks
46
+ with gr.Blocks() as app:
47
+ gr.Markdown(
48
+ "**Disclaimer:** This application is a student project developed as part of coursework and is intended solely for educational and experimental purposes. It is not a substitute for professional medical advice, diagnosis, or treatment. The results provided by this application should not be relied upon for medical decision-making. Use at your own discretion."
49
+ )
50
+
51
+ agree = gr.Checkbox(
52
+ label="I acknowledge that this application is for experimental use only and not suitable for medical purposes."
53
+ )
54
+ message = gr.Textbox(interactive=False)
55
+
56
+ with gr.Row(visible=False) as interface_row:
57
+ image_input = gr.Image(type="pil")
58
+ submit_button = gr.Button("Submit")
59
+ predicted_class = gr.Textbox(label="Predicted Class")
60
+ confidence = gr.Textbox(label="Confidence")
61
+
62
+ submit_button.click(
63
+ fn=predict_image, inputs=image_input, outputs=[predicted_class, confidence]
64
+ )
65
+
66
+ agree.change(acknowledge, agree, [interface_row, message])
67
+
68
+ # Launch the app
69
  if __name__ == "__main__":
70
+ app.launch()