psinha823 commited on
Commit
66a7c3f
·
1 Parent(s): a152766

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = tf.keras.models.load_model("/content/model1_acc96_kera.h5")
8
+
9
+ # Function to preprocess the image and make predictions
10
+ def classify_alzheimers_image(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((128, 128))) / 255.0
16
+ input_image_resized = np.expand_dims(input_image_resized, axis=0)
17
+
18
+ # Making predictions
19
+ predictions = model.predict(input_image_resized)
20
+
21
+ # Getting the class with the highest probability
22
+ class_idx = np.argmax(predictions)
23
+ class_label = ["Mild Demented", "Moderate Demented", "Non-Demented", "Very Mild Demented"][class_idx]
24
+ confidence = predictions[0][class_idx]
25
+
26
+ return f"Prediction: {class_label}, Confidence: {confidence:.2f}"
27
+ except Exception as e:
28
+ return str(e)
29
+
30
+ # Creating a Gradio interface
31
+ iface = gr.Interface(
32
+ fn=classify_alzheimers_image,
33
+ inputs="image",
34
+ outputs="text",
35
+ title="Alzheimer's Disease Classification",
36
+ description=" Upload an MRI Image for classification.",
37
+ flagging_options = ["Wrong Prediction"],
38
+ theme = 'darkhuggingface'
39
+ )
40
+
41
+ # Launching the Gradio interface
42
+ iface.launch(inline = False)