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