the-shoaib2 commited on
Commit
bc038a5
·
verified ·
1 Parent(s): 0af3afe

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ import os
6
+
7
+ # Configuration
8
+ MODEL_PATH = "best_model.keras"
9
+ IMG_SIZE = (299, 299)
10
+ CLASSES = ['glioma', 'meningioma', 'notumor', 'pituitary']
11
+
12
+ # Load model
13
+ print(f"Loading model from {MODEL_PATH}...")
14
+ try:
15
+ if os.path.exists(MODEL_PATH):
16
+ model = tf.keras.models.load_model(MODEL_PATH)
17
+ print("Model loaded successfully.")
18
+ else:
19
+ print("Error: Model file not found.")
20
+ model = None
21
+ except Exception as e:
22
+ print(f"Failed to load model: {e}")
23
+ model = None
24
+
25
+ def predict(image):
26
+ if model is None:
27
+ return "Model not loaded. Please check the logs."
28
+
29
+ try:
30
+ # Preprocess image
31
+ image = image.resize(IMG_SIZE)
32
+ img_array = np.array(image)
33
+ img_array = img_array.astype(np.float32) / 255.0
34
+ img_batch = np.expand_dims(img_array, axis=0)
35
+
36
+ # Predict
37
+ predictions = model.predict(img_batch, verbose=0)
38
+ probs = predictions[0]
39
+
40
+ # Format results
41
+ results = {CLASSES[i]: float(probs[i]) for i in range(len(CLASSES))}
42
+ return results
43
+ except Exception as e:
44
+ return f"Error: {e}"
45
+
46
+ # Build Gradio Interface
47
+ interface = gr.Interface(
48
+ fn=predict,
49
+ inputs=gr.Image(type="pil"),
50
+ outputs=gr.Label(num_top_classes=4),
51
+ title="MRI Brain Tumor Classification",
52
+ description="Upload an MRI scan to classify it into one of four categories: Glioma, Meningioma, No Tumor, or Pituitary.",
53
+ theme="soft"
54
+ )
55
+
56
+ if __name__ == "__main__":
57
+ interface.launch()