Spaces:
Sleeping
Sleeping
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoProcessor, AutoModel
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# โหลด processor และ model
|
| 7 |
+
model_name = "google/siglip2-base-patch16-224"
|
| 8 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 9 |
+
model = AutoModel.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# ฟังก์ชันประมวลผล
|
| 12 |
+
def match_image_text(image, text):
|
| 13 |
+
inputs = processor(text=text, images=image, return_tensors="pt", padding=True)
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
|
| 17 |
+
image_embeds = outputs.image_embeds
|
| 18 |
+
text_embeds = outputs.text_embeds
|
| 19 |
+
|
| 20 |
+
# คำนวณ cosine similarity
|
| 21 |
+
similarity = torch.nn.functional.cosine_similarity(image_embeds, text_embeds).item()
|
| 22 |
+
return f"Similarity score: {similarity:.4f}"
|
| 23 |
+
|
| 24 |
+
# Gradio UI
|
| 25 |
+
gr.Interface(
|
| 26 |
+
fn=match_image_text,
|
| 27 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Enter a caption")],
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="SigLIP2 Image-Text Similarity",
|
| 30 |
+
description="ใส่รูป + คำบรรยาย แล้วดูว่าโมเดลคิดว่าแมตช์กันแค่ไหน"
|
| 31 |
+
).launch()
|