skibi11 commited on
Commit
ecfc393
·
verified ·
1 Parent(s): 1d7422f

create app.py

Browse files

file containing the Gradio code to run your model

Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from tensorflow.keras.models import load_model
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
+ model = load_model(model_path)
14
+ print("Model loaded successfully!")
15
+ except Exception as e:
16
+ print(f"Error loading model: {e}")
17
+ model = None
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
29
+
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
+ pil_image = Image.fromarray(image_array.astype('uint8'), 'RGB')
36
+ processed_image = preprocess_image(pil_image)
37
+ prediction = model.predict(processed_image)
38
+
39
+ # Convert prediction to a JSON-friendly format
40
+ labels = [f"Class_{i}" for i in range(prediction.shape[1])]
41
+ confidences = {label: float(score) for label, score in zip(labels, prediction[0])}
42
+ return confidences
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()