Mrhuman1 commited on
Commit
f807de7
·
verified ·
1 Parent(s): a5a0035

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -2,30 +2,28 @@ import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
- from huggingface_hub import hf_hub_download
6
 
7
- # Download the model from HF Files
8
- model_path = hf_hub_download(repo_id="username/space_name", filename="brain_tumor_model.h5")
9
- model = tf.keras.models.load_model(model_path)
10
 
11
  # Class labels
12
  class_names = ['Glioma Tumor', 'Meningioma Tumor', 'No Tumor', 'Pituitary Tumor']
13
 
14
- # Preprocessing function
15
  def preprocess_image(image):
16
  image = image.resize((224, 224))
17
  image = np.array(image) / 255.0
18
  if image.shape[-1] == 4:
19
- image = image[..., :3]
20
  image = np.expand_dims(image, axis=0)
21
  return image
22
 
23
- # Prediction function
24
  def predict(image):
25
  img = preprocess_image(image)
26
- predictions = model.predict(img)
27
- predicted_class = class_names[np.argmax(predictions)]
28
- confidence = np.max(predictions)
29
  return f"Predicted Tumor Type: {predicted_class} (Confidence: {confidence:.2f})"
30
 
31
  # Gradio interface
@@ -33,8 +31,8 @@ interface = gr.Interface(
33
  fn=predict,
34
  inputs=gr.Image(type="pil"),
35
  outputs="text",
36
- title="Brain Tumor Classifier",
37
- description="Upload a Brain MRI image. The model will classify it as Glioma, Meningioma, Pituitary Tumor, or No Tumor."
38
  )
39
 
40
  interface.launch()
 
2
  import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
+ # Load the model from local path (available in Space's files)
7
+ model = tf.keras.models.load_model("brain_tumor_model.h5")
 
8
 
9
  # Class labels
10
  class_names = ['Glioma Tumor', 'Meningioma Tumor', 'No Tumor', 'Pituitary Tumor']
11
 
12
+ # Image preprocessing
13
  def preprocess_image(image):
14
  image = image.resize((224, 224))
15
  image = np.array(image) / 255.0
16
  if image.shape[-1] == 4:
17
+ image = image[..., :3] # Remove alpha if present
18
  image = np.expand_dims(image, axis=0)
19
  return image
20
 
21
+ # Prediction logic
22
  def predict(image):
23
  img = preprocess_image(image)
24
+ prediction = model.predict(img)
25
+ predicted_class = class_names[np.argmax(prediction)]
26
+ confidence = np.max(prediction)
27
  return f"Predicted Tumor Type: {predicted_class} (Confidence: {confidence:.2f})"
28
 
29
  # Gradio interface
 
31
  fn=predict,
32
  inputs=gr.Image(type="pil"),
33
  outputs="text",
34
+ title="🧠 Brain Tumor MRI Classifier",
35
+ description="Upload a Brain MRI image to predict if it is Glioma, Meningioma, Pituitary Tumor, or No Tumor."
36
  )
37
 
38
  interface.launch()