Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,48 +4,56 @@ import numpy as np
|
|
| 4 |
from PIL import Image
|
| 5 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
| 8 |
MODEL_PATH = "exported_model"
|
| 9 |
IMG_SIZE = (224, 224)
|
| 10 |
-
CLASS_NAMES = [
|
| 11 |
|
| 12 |
-
|
| 13 |
model = tf.saved_model.load(MODEL_PATH)
|
| 14 |
-
|
| 15 |
infer = model.signatures["serving_default"]
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
img = Image.fromarray(img_input).convert("RGB")
|
| 23 |
img = img.resize(IMG_SIZE)
|
| 24 |
|
| 25 |
arr = np.array(img).astype("float32")
|
| 26 |
arr = preprocess_input(arr)
|
| 27 |
arr = np.expand_dims(arr, axis=0)
|
| 28 |
|
| 29 |
-
# TensorFlow serving
|
| 30 |
outputs = infer(tf.constant(arr))
|
| 31 |
preds = list(outputs.values())[0].numpy()[0]
|
| 32 |
|
| 33 |
idx = np.argmax(preds)
|
| 34 |
-
confidence = preds[idx]
|
| 35 |
label = CLASS_NAMES[idx]
|
| 36 |
|
| 37 |
-
return f"
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=predict_guava_quality,
|
| 41 |
-
inputs=gr.Image(type="numpy", label="Tải ảnh
|
| 42 |
outputs=[
|
| 43 |
-
gr.Textbox(label="
|
| 44 |
-
gr.Number(label="Độ tin cậy (
|
| 45 |
],
|
| 46 |
-
title="
|
| 47 |
-
description="
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
demo.launch(
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 6 |
|
| 7 |
+
# ============================
|
| 8 |
+
# Load TensorFlow SavedModel
|
| 9 |
+
# ============================
|
| 10 |
MODEL_PATH = "exported_model"
|
| 11 |
IMG_SIZE = (224, 224)
|
| 12 |
+
CLASS_NAMES = ["bad", "good", "very_good"]
|
| 13 |
|
| 14 |
+
print("🔄 Loading SavedModel…")
|
| 15 |
model = tf.saved_model.load(MODEL_PATH)
|
|
|
|
| 16 |
infer = model.signatures["serving_default"]
|
| 17 |
+
print("✅ Model loaded!")
|
| 18 |
+
|
| 19 |
|
| 20 |
+
# ============================
|
| 21 |
+
# Prediction Function
|
| 22 |
+
# ============================
|
| 23 |
+
def predict_guava_quality(image):
|
| 24 |
+
if image is None:
|
| 25 |
+
return "❌ Vui lòng tải ảnh!", 0.0
|
| 26 |
|
| 27 |
+
img = Image.fromarray(image).convert("RGB")
|
|
|
|
| 28 |
img = img.resize(IMG_SIZE)
|
| 29 |
|
| 30 |
arr = np.array(img).astype("float32")
|
| 31 |
arr = preprocess_input(arr)
|
| 32 |
arr = np.expand_dims(arr, axis=0)
|
| 33 |
|
|
|
|
| 34 |
outputs = infer(tf.constant(arr))
|
| 35 |
preds = list(outputs.values())[0].numpy()[0]
|
| 36 |
|
| 37 |
idx = np.argmax(preds)
|
| 38 |
+
confidence = float(preds[idx])
|
| 39 |
label = CLASS_NAMES[idx]
|
| 40 |
|
| 41 |
+
return f"🍈 {label}", confidence
|
| 42 |
+
|
| 43 |
|
| 44 |
+
# ============================
|
| 45 |
+
# Gradio UI
|
| 46 |
+
# ============================
|
| 47 |
demo = gr.Interface(
|
| 48 |
fn=predict_guava_quality,
|
| 49 |
+
inputs=gr.Image(type="numpy", label="Tải ảnh Ổi"),
|
| 50 |
outputs=[
|
| 51 |
+
gr.Textbox(label="Kết quả dự đoán"),
|
| 52 |
+
gr.Number(label="Độ tin cậy (0–1)")
|
| 53 |
],
|
| 54 |
+
title="Guava Quality Classifier",
|
| 55 |
+
description="Phân loại chất lượng quả Ổi: very_good / good / bad"
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|