skibi11 commited on
Commit
5eab736
·
verified ·
1 Parent(s): 3a9db5b

kapoyag update aning app.py uy

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  from huggingface_hub import hf_hub_download
@@ -5,7 +7,7 @@ import numpy as np
5
  from PIL import Image
6
  import os
7
 
8
- # --- 1. Load the Model from your other Hugging Face Repo ---
9
  model = None
10
  try:
11
  model_path = hf_hub_download(
@@ -16,11 +18,9 @@ try:
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):
25
  img = img_pil.resize((224, 224))
26
  img_array = np.array(img)
@@ -32,26 +32,29 @@ def preprocess_image(img_pil):
32
  img_array = np.expand_dims(img_array, axis=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
46
  except Exception as e:
47
  raise gr.Error(f"Error during prediction: {e}")
48
 
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.",
56
- api_name="predict" # <--- ADD THIS LINE
57
- ).launch()
 
 
1
+ # Final robust 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
 
7
  from PIL import Image
8
  import os
9
 
10
+ # --- 1. Load the Model ---
11
  model = None
12
  try:
13
  model_path = hf_hub_download(
 
18
  print("--- MODEL LOADED SUCCESSFULLY! ---")
19
  except Exception as e:
20
  print(f"--- ERROR LOADING MODEL: {e} ---")
 
 
21
  raise gr.Error(f"Failed to load model: {e}")
22
 
23
+ # --- 2. Pre-processing Logic ---
24
  def preprocess_image(img_pil):
25
  img = img_pil.resize((224, 224))
26
  img_array = np.array(img)
 
32
  img_array = np.expand_dims(img_array, axis=0)
33
  return img_array
34
 
35
+ # --- 3. Prediction Logic ---
36
  def predict(image_from_gradio):
37
+ if not isinstance(image_from_gradio, np.ndarray):
38
+ return {"error": "Invalid input type. Expected an image."}
39
+
40
  try:
41
  pil_image = Image.fromarray(image_from_gradio)
42
  processed_image = preprocess_image(pil_image)
43
  prediction = model.predict(processed_image)
44
 
 
45
  labels = [f"Class_{i}" for i in range(prediction.shape[1])]
46
  confidences = {label: float(score) for label, score in zip(labels, prediction[0])}
47
  return confidences
48
  except Exception as e:
49
  raise gr.Error(f"Error during prediction: {e}")
50
 
51
+ # --- 4. Create Gradio Interface using gr.Blocks for stability ---
52
+ with gr.Blocks() as demo:
53
+ gr.Interface(
54
+ fn=predict,
55
+ inputs=gr.Image(type="numpy"),
56
+ outputs=gr.JSON(),
57
+ api_name="predict"
58
+ )
59
+
60
+ demo.launch()