File size: 2,570 Bytes
8f124a0
 
 
aefa7d6
8f124a0
aefa7d6
 
 
 
 
8f124a0
 
aefa7d6
 
 
 
8f124a0
 
 
aefa7d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7e4d0
 
aefa7d6
 
 
 
 
8f124a0
aefa7d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f124a0
 
aefa7d6
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import gradio as gr
import torch
import numpy as np
from collections import defaultdict
from transformers import AutoImageProcessor, AutoModel
from sklearn.metrics.pairwise import cosine_similarity

# Load model pretrained (visual embedding)
processor = AutoImageProcessor.from_pretrained("facebook/dino-vits16")
model = AutoModel.from_pretrained("facebook/dino-vits16")
model.eval()

# Database: label -> list of features
feature_db = defaultdict(list)

def extract_feature(image):
    inputs = processor(images=image, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)
    return outputs.last_hidden_state.mean(dim=1).numpy()

def tambah_sampling(image, label):
    if image is None or label.strip() == "":
        return "❌ Gambar dan label wajib diisi"

    feat = extract_feature(image)
    feature_db[label].append(feat)

    return f"✅ Sampling ditambahkan untuk label '{label}' (total: {len(feature_db[label])})"

def tebak_gambar(image):
    if image is None:
        return "❌ Upload gambar terlebih dahulu"

    if len(feature_db) == 0:
        return "❌ Belum ada data sampling"

    test_feat = extract_feature(image)

    label_scores = {}
    for label, feats in feature_db.items():
        sims = cosine_similarity(test_feat, np.vstack(feats))
        label_scores[label] = sims.mean()  # rata-rata kemiripan

    # Ambil label dengan skor tertinggi
    pred_label = max(label_scores, key=label_scores.get)
    skor = label_scores[pred_label]

    hasil = "📌 HASIL PREDIKSI (BERDASARKAN KEMIRIPAN)\n\n"
    for lbl, sc in sorted(label_scores.items(), key=lambda x: x[1], reverse=True):
        hasil += f"- {lbl}: {sc:.2f}\n"

    hasil += f"\n✅ Gambar PALING MIRIP dengan: **{pred_label}**"

    return hasil

with gr.Blocks() as app:
    gr.Markdown("## 🧠 Tebak Gambar Supervised (Similarity-Based)")
    gr.Markdown(
        "Upload gambar sampling dengan label, lalu upload gambar baru. "
        "AI menentukan label berdasarkan kemiripan visual, bukan gambar yang sama persis."
    )

    with gr.Tab("1️⃣ Tambah Data Sampling"):
        img1 = gr.Image(type="pil", label="Gambar Sampling")
        lbl = gr.Textbox(label="Label")
        out1 = gr.Textbox(label="Status")
        btn1 = gr.Button("Tambah")

        btn1.click(tambah_sampling, [img1, lbl], out1)

    with gr.Tab("2️⃣ Tebak Gambar"):
        img2 = gr.Image(type="pil", label="Gambar Uji")
        out2 = gr.Textbox(label="Hasil")
        btn2 = gr.Button("Tebak")

        btn2.click(tebak_gambar, img2, out2)

app.launch()