Spaces:
Running
Running
kapoyag update aning app.py uy
Browse files
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
|
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.
|
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.
|
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
|
50 |
-
gr.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
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()
|