Spaces:
Running
Running
updated app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,49 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
# --- 1. Load the Model from your other Hugging Face Repo ---
|
|
|
|
|
8 |
try:
|
|
|
9 |
model_path = hf_hub_download(
|
10 |
repo_id="skibi11/leukolook-eye-detector",
|
11 |
filename="MobileNetV1_best.keras"
|
12 |
)
|
13 |
-
|
14 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
except Exception as e:
|
16 |
-
print(
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# --- 2. Define the Pre-processing Logic ---
|
20 |
def preprocess_image(img_pil):
|
21 |
-
# This MUST match your training pre-processing
|
22 |
img = img_pil.resize((224, 224))
|
23 |
img_array = np.array(img)
|
24 |
if img_array.ndim == 2:
|
25 |
img_array = np.stack((img_array,)*3, axis=-1)
|
|
|
|
|
|
|
26 |
img_array = img_array / 255.0
|
27 |
img_array = np.expand_dims(img_array, axis=0)
|
28 |
return img_array
|
@@ -30,21 +51,24 @@ def preprocess_image(img_pil):
|
|
30 |
# --- 3. Define the Prediction Function ---
|
31 |
def predict(image_array):
|
32 |
if model is None:
|
33 |
-
raise gr.Error("Model is not loaded. Please check the Space logs.")
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
# --- 4. Create and Launch the Gradio API ---
|
45 |
gr.Interface(
|
46 |
fn=predict,
|
47 |
inputs=gr.Image(),
|
48 |
outputs="json",
|
49 |
-
title="LeukoLook Eye Detector API"
|
|
|
50 |
).launch()
|
|
|
1 |
+
# Final app.py for your Hugging Face Space
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
import tensorflow as tf # Import tensorflow directly
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
import numpy as np
|
7 |
from PIL import Image
|
8 |
+
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("--- AN ERROR OCCURRED DURING MODEL LOADING ---")
|
30 |
+
print(f"Error Type: {type(e)}")
|
31 |
+
print(f"Error Message: {e}")
|
32 |
+
# Also print the traceback for more details
|
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):
|
|
|
40 |
img = img_pil.resize((224, 224))
|
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
|
48 |
img_array = np.expand_dims(img_array, axis=0)
|
49 |
return img_array
|
|
|
51 |
# --- 3. Define the Prediction Function ---
|
52 |
def predict(image_array):
|
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(image_array.astype('uint8'), 'RGB')
|
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
|
64 |
+
except Exception as e:
|
65 |
+
raise gr.Error(f"Error during prediction: {e}")
|
66 |
|
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."
|
74 |
).launch()
|