psinha823 commited on
Commit
380299e
·
verified ·
1 Parent(s): d6a5031

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -27
app.py CHANGED
@@ -1,44 +1,85 @@
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}"
29
- except Exception as e:
30
- return str(e)
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)
 
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}"
29
+ # except Exception as e:
30
+ # return str(e)
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)
45
+ import tensorflow as tf
46
+ import numpy as np
47
+ from PIL import Image
48
+
49
+ # Load the trained model
50
+ model = tf.keras.models.load_model("./model12_acc99_kera.h5")
51
+
52
+ # Define image preprocessing function
53
+ def preprocess_image(image):
54
+ # Resize the image to match the input size of the model
55
+ img = image.resize((224, 224))
56
+ # Convert image to numpy array
57
+ img_array = np.array(img)
58
+ # Normalize pixel values to the range [0, 1]
59
+ img_array = img_array / 255.0
60
+ # Expand dimensions to match the model's expected input shape
61
+ img_array = np.expand_dims(img_array, axis=0)
62
+ return img_array
63
+
64
+ # Define the prediction function
65
+ def predict(image):
66
+ # Preprocess the image
67
+ processed_image = preprocess_image(image)
68
+ # Perform inference
69
+ predictions = model.predict(processed_image)
70
+ # Convert predictions to class labels
71
+ class_labels = ['Colon Adenocarcinoma', 'Colon Benign Tissue', 'Lung Adenocarcinoma', 'Lung Benign Tissue', 'Lung Squamous Cell Carcinoma']
72
+ predicted_class = class_labels[np.argmax(predictions)]
73
+ return predicted_class
74
 
 
 
 
75
 
76
  # Creating a Gradio interface
77
  iface = gr.Interface(
78
+ fn=predict,
79
  inputs="image",
80
  outputs="text",
81
  title="Lung and Colon Cancer Detection",
82
  description="Upload a Histopathology Image",
83
  flagging_options = ["Wrong Prediction"],
84
+ # theme = 'darkhuggingface'
85
+ )