File size: 976 Bytes
6e2e541 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import gradio as gr
import numpy as np
from PIL import Image
from tensorflow.keras.applications.mobilenet_v2 import (
MobileNetV2,
preprocess_input,
decode_predictions
)
# Load model sekali saja
model = MobileNetV2(weights="imagenet")
def predict_image(img):
if img is None:
return "Silakan upload gambar terlebih dahulu"
img = img.convert("RGB")
img = img.resize((224, 224))
x = np.array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
decoded = decode_predictions(preds, top=5)[0]
result = "🤖 Hasil Tebakan AI:\n\n"
for i, p in enumerate(decoded):
result += f"{i+1}. {p[1]} — {p[2]*100:.2f}%\n"
return result
demo = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(lines=7),
title="🤖 AI Tebak Gambar",
description="Upload gambar, AI akan menebak isinya",
allow_flagging="never"
)
demo.launch()
|