Upload 2 files
Browse files
README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
-
---
|
2 |
-
title: ClipArte
|
3 |
-
emoji: 🏃
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: indigo
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.44.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: cc-by-nc-sa-3.0
|
11 |
-
short_description: Uso di clip con immagini di esempio
|
12 |
-
---
|
13 |
-
|
14 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
---
|
2 |
+
title: ClipArte
|
3 |
+
emoji: 🏃
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.44.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: cc-by-nc-sa-3.0
|
11 |
+
short_description: Uso di clip con immagini di esempio
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import torch
|
3 |
+
from transformers import CLIPProcessor, CLIPModel
|
4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
+
from PIL import Image
|
6 |
+
import gradio as gr
|
7 |
+
from pathlib import Path
|
8 |
+
|
9 |
+
def load_clip_model(device):
|
10 |
+
model = CLIPModel.from_pretrained("laion/CLIP-ViT-L-14-laion2B-s32B-b82K").to(device)
|
11 |
+
processor = CLIPProcessor.from_pretrained("laion/CLIP-ViT-L-14-laion2B-s32B-b82K")
|
12 |
+
return model, processor
|
13 |
+
|
14 |
+
def load_embeddings(embedding_file):
|
15 |
+
df = pd.read_csv(embedding_file)
|
16 |
+
embeddings = df.iloc[:, 1:].values # Escludi la prima colonna (filename)
|
17 |
+
image_paths = df['filename'].tolist() # Salva i nomi dei file
|
18 |
+
return embeddings, image_paths
|
19 |
+
|
20 |
+
def query_images(text, model, processor, image_embeddings, image_paths, device):
|
21 |
+
# Genera l'embedding per il testo
|
22 |
+
text_inputs = processor(text=[text], return_tensors="pt", padding=True).to(device)
|
23 |
+
with torch.no_grad():
|
24 |
+
text_embedding = model.get_text_features(**text_inputs).cpu().numpy().flatten()
|
25 |
+
|
26 |
+
# Calcola la similarità coseno tra l'embedding del testo e gli embeddings delle immagini
|
27 |
+
similarities = cosine_similarity([text_embedding], image_embeddings)[0]
|
28 |
+
|
29 |
+
# Ottieni gli indici delle tre immagini più simili
|
30 |
+
top_indices = similarities.argsort()[-3:][::-1]
|
31 |
+
|
32 |
+
# Restituisci i percorsi delle immagini più simili e i loro punteggi
|
33 |
+
return [(Path("img") / image_paths[i], similarities[i]) for i in top_indices]
|
34 |
+
|
35 |
+
def predict(query_text):
|
36 |
+
similar_images = query_images(query_text, model, processor, embeddings, image_paths, device)
|
37 |
+
image_outputs = []
|
38 |
+
scores = []
|
39 |
+
|
40 |
+
for img_path, score in similar_images:
|
41 |
+
img = Image.open(img_path)
|
42 |
+
image_outputs.append(img)
|
43 |
+
scores.append(score)
|
44 |
+
|
45 |
+
# Formatta i punteggi per il DataFrame
|
46 |
+
scores_formatted = [[score] for score in scores] # Converti in una lista di liste
|
47 |
+
return image_outputs, scores_formatted # Restituisci le immagini e i punteggi
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
51 |
+
model, processor = load_clip_model(device)
|
52 |
+
|
53 |
+
# Carica gli embeddings dal file CSV
|
54 |
+
embedding_file = "embeddings.csv" # Sostituisci con il percorso corretto
|
55 |
+
embeddings, image_paths = load_embeddings(embedding_file)
|
56 |
+
|
57 |
+
# Crea l'interfaccia Gradio
|
58 |
+
interface = gr.Interface(
|
59 |
+
fn=predict,
|
60 |
+
inputs="text",
|
61 |
+
outputs=[
|
62 |
+
gr.Gallery(label="Similar Images", elem_id="image_gallery"),
|
63 |
+
gr.Dataframe(label="Similarity Scores", headers=["Score"]) # Rimosso show_footer
|
64 |
+
],
|
65 |
+
title="Find Similar Images",
|
66 |
+
description="Insert text to find the three most similar images."
|
67 |
+
)
|
68 |
+
|
69 |
+
interface.launch()
|