Spaces:
Running
Running
simplified app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
-
# Final app.py for your Hugging Face Space
|
2 |
-
|
3 |
import gradio as gr
|
4 |
-
import tensorflow as tf
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
import numpy as np
|
7 |
from PIL import Image
|
@@ -9,31 +7,18 @@ import os
|
|
9 |
|
10 |
# --- 1. Load the Model from your other Hugging Face Repo ---
|
11 |
model = None
|
12 |
-
print("--- SCRIPT START ---")
|
13 |
try:
|
14 |
-
print("Downloading Keras model from the Hub...")
|
15 |
model_path = hf_hub_download(
|
16 |
repo_id="skibi11/leukolook-eye-detector",
|
17 |
filename="MobileNetV1_best.keras"
|
18 |
)
|
19 |
-
print(f"Model downloaded to: {model_path}")
|
20 |
-
print("Loading model with tf.keras.models.load_model...")
|
21 |
-
|
22 |
-
# This is a more robust way to load the model
|
23 |
model = tf.keras.models.load_model(model_path)
|
24 |
-
|
25 |
print("--- MODEL LOADED SUCCESSFULLY! ---")
|
26 |
-
model.summary() # Print a summary of the model to confirm it's loaded
|
27 |
-
|
28 |
except Exception as e:
|
29 |
-
print("---
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
import traceback
|
34 |
-
traceback.print_exc()
|
35 |
-
print("--- END OF ERROR ---")
|
36 |
-
|
37 |
|
38 |
# --- 2. Define the Pre-processing Logic ---
|
39 |
def preprocess_image(img_pil):
|
@@ -41,7 +26,6 @@ def preprocess_image(img_pil):
|
|
41 |
img_array = np.array(img)
|
42 |
if img_array.ndim == 2:
|
43 |
img_array = np.stack((img_array,)*3, axis=-1)
|
44 |
-
# Ensure image has 3 channels if it's not
|
45 |
if img_array.shape[-1] == 4:
|
46 |
img_array = img_array[..., :3]
|
47 |
img_array = img_array / 255.0
|
@@ -49,15 +33,13 @@ def preprocess_image(img_pil):
|
|
49 |
return img_array
|
50 |
|
51 |
# --- 3. Define the Prediction Function ---
|
52 |
-
def predict(
|
53 |
-
if model is None:
|
54 |
-
raise gr.Error("Model is not loaded. Please check the Space logs for errors.")
|
55 |
-
|
56 |
try:
|
57 |
-
pil_image = Image.fromarray(
|
58 |
processed_image = preprocess_image(pil_image)
|
59 |
prediction = model.predict(processed_image)
|
60 |
|
|
|
61 |
labels = [f"Class_{i}" for i in range(prediction.shape[1])]
|
62 |
confidences = {label: float(score) for label, score in zip(labels, prediction[0])}
|
63 |
return confidences
|
@@ -67,7 +49,7 @@ def predict(image_array):
|
|
67 |
# --- 4. Create and Launch the Gradio API ---
|
68 |
gr.Interface(
|
69 |
fn=predict,
|
70 |
-
inputs=gr.Image(),
|
71 |
outputs="json",
|
72 |
title="LeukoLook Eye Detector API",
|
73 |
description="API for the LeukoLook project."
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
|
|
7 |
|
8 |
# --- 1. Load the Model from your other Hugging Face Repo ---
|
9 |
model = None
|
|
|
10 |
try:
|
|
|
11 |
model_path = hf_hub_download(
|
12 |
repo_id="skibi11/leukolook-eye-detector",
|
13 |
filename="MobileNetV1_best.keras"
|
14 |
)
|
|
|
|
|
|
|
|
|
15 |
model = tf.keras.models.load_model(model_path)
|
|
|
16 |
print("--- MODEL LOADED SUCCESSFULLY! ---")
|
|
|
|
|
17 |
except Exception as e:
|
18 |
+
print(f"--- ERROR LOADING MODEL: {e} ---")
|
19 |
+
# If the model fails to load, we cannot proceed.
|
20 |
+
# Gradio will show an error state.
|
21 |
+
raise gr.Error(f"Failed to load model: {e}")
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# --- 2. Define the Pre-processing Logic ---
|
24 |
def preprocess_image(img_pil):
|
|
|
26 |
img_array = np.array(img)
|
27 |
if img_array.ndim == 2:
|
28 |
img_array = np.stack((img_array,)*3, axis=-1)
|
|
|
29 |
if img_array.shape[-1] == 4:
|
30 |
img_array = img_array[..., :3]
|
31 |
img_array = img_array / 255.0
|
|
|
33 |
return img_array
|
34 |
|
35 |
# --- 3. Define the Prediction Function ---
|
36 |
+
def predict(image_from_gradio):
|
|
|
|
|
|
|
37 |
try:
|
38 |
+
pil_image = Image.fromarray(image_from_gradio)
|
39 |
processed_image = preprocess_image(pil_image)
|
40 |
prediction = model.predict(processed_image)
|
41 |
|
42 |
+
# Convert prediction to a JSON-friendly format
|
43 |
labels = [f"Class_{i}" for i in range(prediction.shape[1])]
|
44 |
confidences = {label: float(score) for label, score in zip(labels, prediction[0])}
|
45 |
return confidences
|
|
|
49 |
# --- 4. Create and Launch the Gradio API ---
|
50 |
gr.Interface(
|
51 |
fn=predict,
|
52 |
+
inputs=gr.Image(type="numpy"),
|
53 |
outputs="json",
|
54 |
title="LeukoLook Eye Detector API",
|
55 |
description="API for the LeukoLook project."
|