psinha823 commited on
Commit
d6a5031
·
verified ·
1 Parent(s): 01f4123

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -32
app.py CHANGED
@@ -1,47 +1,28 @@
1
  import gradio as gr
 
2
  import numpy as np
3
  from PIL import Image
4
  import tensorflow as tf
5
- import os
6
 
7
- # Print the current working directory
8
- print("Current working directory:", os.getcwd())
9
-
10
- # Update this to the correct path of your model
11
- model_path = r'./model12acc99_kera.h5'
12
-
13
- # Custom object configuration handling
14
- def custom_separable_conv2d(**kwargs):
15
- # Remove unsupported parameters if they exist
16
- kwargs.pop('kernel_initializer', None)
17
- kwargs.pop('kernel_regularizer', None)
18
- kwargs.pop('kernel_constraint', None)
19
- return tf.keras.layers.SeparableConv2D(**kwargs)
20
-
21
- # Register custom object
22
- custom_objects = {'SeparableConv2D': custom_separable_conv2d}
23
-
24
- # Check if the file exists
25
- if not os.path.exists(model_path):
26
- raise FileNotFoundError(f"Model file not found at {model_path}")
27
-
28
- # Load the model with custom objects
29
- model_h5 = tf.keras.models.load_model(model_path, custom_objects=custom_objects)
30
 
31
  # Function to preprocess the image and make predictions
32
- def classify_Lung_and_colon_image(input_image):
33
  try:
34
  # Preprocess the image (resize, normalize, etc.)
35
  input_image = np.array(input_image)
36
- input_image_resized = np.array(Image.fromarray(input_image).resize((224, 224))) / 255.0
 
 
37
  input_image_resized = np.expand_dims(input_image_resized, axis=0)
38
 
39
  # Making predictions
 
40
  predictions = model_h5.predict(input_image_resized)
41
 
42
  # Getting the class with the highest probability
43
  class_idx = np.argmax(predictions)
44
- class_label = ["Colon Adenocarcinoma", "Colon Benign Tissue", "Lung Adenocarcinoma", "Lung Benign Tissue", "Lung Squamous Cell Carcinoma"][class_idx]
45
  confidence = predictions[0][class_idx]
46
 
47
  return f"Prediction: {class_label}, Confidence: {confidence:.2f}"
@@ -50,14 +31,14 @@ def classify_Lung_and_colon_image(input_image):
50
 
51
  # Creating a Gradio interface
52
  iface = gr.Interface(
53
- fn=classify_Lung_and_colon_image,
54
  inputs="image",
55
  outputs="text",
56
  title="Lung and Colon Cancer Detection",
57
- description="Upload a Histopathology Image for classification.",
58
- flagging_options=["wrongPrediction"],
59
- theme='default' # Changed theme to 'default' for compatibility
60
  )
61
 
62
  # Launching the Gradio interface
63
- iface.launch(inline=False)
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  import numpy as np
4
  from PIL import Image
5
  import tensorflow as tf
 
6
 
7
+ model_path = r'./model12_acc99_kera.h5'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Function to preprocess the image and make predictions
10
+ def classify_lung_and_colon(input_image):
11
  try:
12
  # Preprocess the image (resize, normalize, etc.)
13
  input_image = np.array(input_image)
14
+ input_image_copy = input_image.copy() # Making a copy to avoid the array reference issue
15
+ input_image_resized = np.array(Image.fromarray(input_image_copy).resize((228,228)))
16
+ # / 255.0
17
  input_image_resized = np.expand_dims(input_image_resized, axis=0)
18
 
19
  # Making predictions
20
+ model_h5 = tf.keras.models.load_model(model_path)
21
  predictions = model_h5.predict(input_image_resized)
22
 
23
  # Getting the class with the highest probability
24
  class_idx = np.argmax(predictions)
25
+ class_label = ["Colon Adenocarcinoma", "Colon Benign Tissue", "Lung Adenocarcinoma", "Lung Benign Tissue","Lung Squamous Cell Carcinoma"][class_idx]
26
  confidence = predictions[0][class_idx]
27
 
28
  return f"Prediction: {class_label}, Confidence: {confidence:.2f}"
 
31
 
32
  # Creating a Gradio interface
33
  iface = gr.Interface(
34
+ fn=classify_lung_and_colon,
35
  inputs="image",
36
  outputs="text",
37
  title="Lung and Colon Cancer Detection",
38
+ description="Upload a Histopathology Image",
39
+ flagging_options = ["Wrong Prediction"],
40
+ theme = 'darkhuggingface'
41
  )
42
 
43
  # Launching the Gradio interface
44
+ iface.launch(inline = False)