Spaces:
Runtime error
Runtime error
BuddyBytes.co
commited on
Commit
·
d217f88
1
Parent(s):
b104550
Add clipsense-min
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoProcessor, AutoModel
|
| 4 |
+
|
| 5 |
+
# Load MetaCLIP
|
| 6 |
+
model_name = "facebook/metaclip-2-worldwide-s16"
|
| 7 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 8 |
+
model = AutoModel.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def classify(image, labels_text):
|
| 12 |
+
if image is None or not labels_text.strip():
|
| 13 |
+
return (
|
| 14 |
+
"<div style='font-size:22px; color:#b00000;'>กรุณาอัปโหลดภาพและกรอก labels ก่อน</div>",
|
| 15 |
+
""
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
labels = [l.strip() for l in labels_text.split("\n") if l.strip()]
|
| 19 |
+
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
| 20 |
+
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
out = model(**inputs)
|
| 23 |
+
probs = out.logits_per_image.softmax(dim=1)[0]
|
| 24 |
+
|
| 25 |
+
best_idx = torch.argmax(probs)
|
| 26 |
+
best_label = labels[best_idx]
|
| 27 |
+
best_prob = float(probs[best_idx])
|
| 28 |
+
|
| 29 |
+
# ===== HTML การ์ดไฮไลท์ตรงกลาง =====
|
| 30 |
+
highlight_html = f"""
|
| 31 |
+
<div style="
|
| 32 |
+
text-align:center;
|
| 33 |
+
padding:30px;
|
| 34 |
+
border-radius:20px;
|
| 35 |
+
background: linear-gradient(135deg, #ffe7e7, #fff4e6);
|
| 36 |
+
border:1px solid #f2d0d0;
|
| 37 |
+
box-shadow:0 4px 20px rgba(0,0,0,0.08);
|
| 38 |
+
margin-bottom: 20px;
|
| 39 |
+
">
|
| 40 |
+
<div style='font-size:40px; font-weight:900; color:#b80000; margin-bottom:10px;'>
|
| 41 |
+
ผลลัพธ์ที่ใกล้ที่สุด
|
| 42 |
+
</div>
|
| 43 |
+
|
| 44 |
+
<div style='font-size:48px; font-weight:900; color:#d00000;'>
|
| 45 |
+
🎯 {best_label} 🎯
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
+
<div style='font-size:26px; margin-top:14px; color:#444;'>
|
| 49 |
+
ความมั่นใจ: <b>{best_prob:.4f}</b>
|
| 50 |
+
</div>
|
| 51 |
+
</div>
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
# ===== Markdown รายละเอียดทั้งหมด =====
|
| 55 |
+
details_md = "### รายละเอียดทั้งหมด\n"
|
| 56 |
+
for lbl, p in zip(labels, probs):
|
| 57 |
+
details_md += f"- **{lbl}**: {float(p):.4f}\n"
|
| 58 |
+
|
| 59 |
+
return highlight_html, details_md
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 63 |
+
gr.Markdown(
|
| 64 |
+
"""
|
| 65 |
+
# 🎨 ClipSense Mini
|
| 66 |
+
ทดลองว่า MetaCLIP 2 คิดว่าภาพของคุณ “ใกล้คำไหนที่สุด”
|
| 67 |
+
"""
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
with gr.Row(equal_height=True):
|
| 71 |
+
# ===== LEFT : Image + Labels =====
|
| 72 |
+
with gr.Column(scale=1):
|
| 73 |
+
image = gr.Image(type="pil", height=350, label="อัปโหลดรูปภาพ")
|
| 74 |
+
|
| 75 |
+
labels_box = gr.Textbox(
|
| 76 |
+
label="Labels (ใส่ทีละบรรทัด / ใช้ภาษาไทยได้)",
|
| 77 |
+
lines=7,
|
| 78 |
+
placeholder="ผู้หญิง\nผู้ชาย\nรูปโป๊\nนางตานี\nแมว\nหมา"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
btn = gr.Button("วิเคราะห์ภาพด้วย ClipSense")
|
| 82 |
+
|
| 83 |
+
# ===== RIGHT : Result =====
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
highlight = gr.HTML("<div>ผลลัพธ์จะปรากฏที่นี่</div>")
|
| 86 |
+
details = gr.Markdown("")
|
| 87 |
+
|
| 88 |
+
btn.click(classify, [image, labels_box], [highlight, details])
|
| 89 |
+
|
| 90 |
+
demo.launch()
|