-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamlitApp.py
More file actions
220 lines (170 loc) · 7.04 KB
/
StreamlitApp.py
File metadata and controls
220 lines (170 loc) · 7.04 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import streamlit as st
import numpy as np
from PIL import Image
import pandas as pd
from sentence_transformers import SentenceTransformer
from scipy.spatial.distance import cosine
import ast
import requests
from io import BytesIO
from tensorflow import keras
from keras.applications import ResNet50
# Chargement des données
articles = pd.read_csv("data_emb.csv")
articles_images = pd.read_parquet("resnet50_image_embeddings.parquet")
articles_combined = articles.copy()
embedding_cols = [f"resnet50_{i}" for i in range(0, 2048)] # ou df.columns spécifiques
articles_images['embeddings_images'] = articles_images[embedding_cols].apply(lambda row: row.tolist(), axis=1)
# Convertir les embeddings de str -> np.array
articles['embeddings'] = articles['embeddings'].apply(lambda x: np.array(ast.literal_eval(x)))
E_text = np.vstack(articles['embeddings'].to_numpy())
E_img = np.vstack(articles_images['embeddings_images'].to_numpy())
articles_combined["embeddings"] = list(np.hstack([E_text, E_img]))
# Charger le modèle une seule fois (important pour Streamlit)
@st.cache_resource
def load_cnn_model():
resnet_model = ResNet50(
include_top=False,
weights="imagenet",
pooling="avg"
)
resnet_model.trainable = False # on ne fine-tune pas, juste extraction de features
return resnet_model
@st.cache_resource
def load_text_model():
return SentenceTransformer("all-MiniLM-L6-v2")
# Fonction de similarité
def compute_similarity_images(query_emb, df, top_k=10, combined=False):
query_emb = np.array(query_emb).flatten()
df = df.copy()
# Calcul de la similarité
df['similarity'] = df['embeddings_images'].apply(lambda emb: 1 - cosine(query_emb, np.array(emb).flatten()))
# Trier par similarité décroissante
df_sorted = df.sort_values("similarity", ascending=False)
# Garder seulement le premier article unique par product_code
df_unique = df_sorted.drop_duplicates(subset='product_code', keep='first')
if combined:
return df_unique
# Sélectionner les top_k
df_topk = df_unique.head(top_k)
sims = df_topk['similarity'].values
order = df_topk.index.tolist()
return sims, order
# Fonction de similarité
def compute_similarity(query_emb, df, top_k=10):
query_emb = np.array(query_emb).flatten()
df = df.copy()
# Calcul de la similarité
df['similarity'] = df['embeddings'].apply(lambda emb: 1 - cosine(query_emb, np.array(emb).flatten()))
# Trier par similarité décroissante
df_sorted = df.sort_values("similarity", ascending=False)
# Garder seulement le premier article unique par product_code
df_unique = df_sorted.drop_duplicates(subset='product_code', keep='first')
# Sélectionner les top_k
df_topk = df_unique.head(top_k)
sims = df_topk['similarity'].values
order = df_topk.index.tolist()
return sims, order
def encode_image(image: Image.Image):
try:
model = load_cnn_model()
img = image.convert("RGB").resize((256, 256))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
embeddings = model.predict(img_array)
return embeddings.flatten()
except Exception as e:
print(e)
def encode_text(text: str):
model = load_text_model()
embeddings = model.encode(text, show_progress_bar=True)
return embeddings
# Fonction utilitaire pour charger image depuis URL
def load_image_from_url(url):
try:
response = requests.get(url)
img = Image.open(BytesIO(response.content))
return img
except:
st.error(f"Impossible de charger l'image depuis {url}")
return None
# Interface Streamlit
st.markdown(
"""
<h1 style='text-align: center;'>
Plateforme de recherche de similarité Channel
</h1>
""",
unsafe_allow_html=True
)
option = st.sidebar.selectbox(
"Choisir une option",
["Recherche par image", "Recherche par texte", "Recherche combinée"]
)
# Recherche par image
if option == "Recherche par image":
st.header("Recherche basée sur une image")
uploaded_file = st.file_uploader("Charge une image", type=["jpg", "png"])
if uploaded_file:
img = Image.open(uploaded_file)
st.image(img, caption="Image fournie", width=300)
query_emb = encode_image(img)
sims, order = compute_similarity_images(query_emb, articles_images)
st.subheader("🔝 Top 10 articles visuellement similaires")
for idx in order[:10]:
row = articles.iloc[idx]
col1, col2 = st.columns([1, 3])
with col1:
img_row = load_image_from_url(row["imageurl"])
if img_row:
st.image(img_row, width=100)
with col2:
st.write(f"**{row['title_translated']}**")
st.write(f"*Code du produit : {row['product_code']}*")
st.write(f"Catégorie: {row['category1_code']} / {row['category2_code']}")
st.write(f"Prix : {row['price']} €")
# Recherche par texte
elif option == "Recherche par texte":
st.header("Recherche basée sur un texte")
query = st.text_input("Entrez une description")
if query:
query_emb = encode_text(query)
sims, order = compute_similarity(query_emb, articles)
st.subheader("Top 10 articles similaires en termes de description")
for idx in order[:10]:
row = articles.iloc[idx]
col1, col2 = st.columns([1, 3])
with col1:
img_row = load_image_from_url(row["imageurl"])
if img_row:
st.image(img_row, width=100)
with col2:
st.write(f"**{row['title_translated']}**")
st.write(f"*Code du produit : {row['product_code']}*")
st.write(f"Catégorie: {row['category1_code']} / {row['category2_code']}")
st.write(f"Prix : {row['price']} €")
# Recherche combinée
elif option == "Recherche combinée":
st.header("Recherche combinée image + texte")
uploaded_file = st.file_uploader("Charge une image", type=["jpg", "png"])
query_text = st.text_input("Entrez une description")
if uploaded_file and query_text:
img = Image.open(uploaded_file)
st.image(img, width=300)
emb_img = encode_image(img)
emb_txt = encode_text(query_text)
query_emb = np.hstack([emb_txt, emb_img])
sims, order = compute_similarity(query_emb, articles_combined)
st.subheader("Top 10 articles combinés")
for idx in order[:50]:
row = articles.iloc[idx]
col1, col2 = st.columns([1, 3])
with col1:
img_row = load_image_from_url(row["imageurl"])
if img_row:
st.image(img_row, width=100)
with col2:
st.write(f"**{row['title_translated']}**")
st.write(f"*Code du produit : {row['product_code']}*")
st.write(f"Catégorie: {row['category1_code']} / {row['category2_code']}")
st.write(f"Prix : {row['price']} €")