ghadaAlmuaikel
commited on
Commit
•
a4bfc26
1
Parent(s):
fdd08ab
Update app.py
Browse files
app.py
CHANGED
@@ -217,34 +217,41 @@ def process_best_match(best_match, language):
|
|
217 |
return best_image_url, info_html, audio_file
|
218 |
|
219 |
# Function to match the uploaded image with the DataFrame to retrive the image of painting from the Datafram and it story in text and audio
|
|
|
220 |
def compare_images(image, language):
|
221 |
try:
|
|
|
222 |
inputs = processor(images=image, return_tensors="pt")
|
223 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
224 |
image_features = model.get_image_features(**inputs).to(device)
|
225 |
|
226 |
-
best_score = -2.0
|
227 |
-
best_match_idx = None
|
228 |
|
|
|
229 |
for idx, image_url in enumerate(df['image_url']):
|
230 |
db_image = fetch_image_from_url(image_url)
|
231 |
if db_image is None:
|
232 |
continue
|
233 |
|
234 |
-
|
235 |
-
|
236 |
-
|
|
|
237 |
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
|
|
244 |
|
|
|
245 |
if best_match_idx is None:
|
246 |
return None, "Error: No valid image match found in the database.", None
|
247 |
|
|
|
248 |
best_match = df.iloc[best_match_idx]
|
249 |
return process_best_match(best_match, language)
|
250 |
|
|
|
217 |
return best_image_url, info_html, audio_file
|
218 |
|
219 |
# Function to match the uploaded image with the DataFrame to retrive the image of painting from the Datafram and it story in text and audio
|
220 |
+
|
221 |
def compare_images(image, language):
|
222 |
try:
|
223 |
+
|
224 |
inputs = processor(images=image, return_tensors="pt")
|
225 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
226 |
image_features = model.get_image_features(**inputs).to(device)
|
227 |
|
228 |
+
best_score = -2.0
|
229 |
+
best_match_idx = None
|
230 |
|
231 |
+
|
232 |
for idx, image_url in enumerate(df['image_url']):
|
233 |
db_image = fetch_image_from_url(image_url)
|
234 |
if db_image is None:
|
235 |
continue
|
236 |
|
237 |
+
|
238 |
+
db_inputs = processor(images=db_image, return_tensors="pt")
|
239 |
+
db_inputs = {k: v.to(device) for k, v in db_inputs.items()}
|
240 |
+
db_image_features = model.get_image_features(**db_inputs).to(device)
|
241 |
|
242 |
+
|
243 |
+
similarity = torch.nn.functional.cosine_similarity(image_features, db_image_features).item()
|
244 |
+
|
245 |
+
|
246 |
+
if similarity > best_score:
|
247 |
+
best_score = similarity
|
248 |
+
best_match_idx = idx
|
249 |
|
250 |
+
|
251 |
if best_match_idx is None:
|
252 |
return None, "Error: No valid image match found in the database.", None
|
253 |
|
254 |
+
|
255 |
best_match = df.iloc[best_match_idx]
|
256 |
return process_best_match(best_match, language)
|
257 |
|