|
|
import gradio as gr |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
from tensorflow.keras.applications.mobilenet_v2 import ( |
|
|
MobileNetV2, |
|
|
preprocess_input, |
|
|
decode_predictions |
|
|
) |
|
|
|
|
|
|
|
|
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() |
|
|
|