GeorgeSherif commited on
Commit
2fb86ad
·
1 Parent(s): 074b7e0
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -5,6 +5,7 @@ import random
5
  from datasets import load_dataset, Dataset, Features, Value, concatenate_datasets
6
  from huggingface_hub import login
7
  import json
 
8
 
9
  # Authenticate with Hugging Face
10
  token = os.getenv("HUGGINGFACE_TOKEN")
@@ -33,23 +34,32 @@ lock = threading.Lock()
33
 
34
  with open('nearest_neighbors_with_captions.json', 'r') as f:
35
  results = json.load(f)
36
- print("Loaded JSON data:", results) # Add this line to verify the data
37
 
38
- def get_caption_for_image_id(image_id):
39
  """
40
  Retrieve the caption for a given image_id from the JSON data.
41
  """
42
- # Check if image_id is a test image
43
- if image_id in results:
44
- return results[image_id]["caption"]
45
-
46
- # If image_id is not a test image, search in nearest neighbors
47
- for test_image_data in results.values():
48
- for neighbor in test_image_data["nearest_neighbors"]:
49
- if neighbor["image_id"] == image_id:
50
- return neighbor["caption"]
 
 
 
 
 
 
 
 
51
 
52
  # Return None if the image_id is not found
 
53
  return None
54
 
55
  # Function to get a random image that hasn’t been annotated or skipped
 
5
  from datasets import load_dataset, Dataset, Features, Value, concatenate_datasets
6
  from huggingface_hub import login
7
  import json
8
+ import re
9
 
10
  # Authenticate with Hugging Face
11
  token = os.getenv("HUGGINGFACE_TOKEN")
 
34
 
35
  with open('nearest_neighbors_with_captions.json', 'r') as f:
36
  results = json.load(f)
37
+ # print("Loaded JSON data:", results) # Add this line to verify the data
38
 
39
+ def get_caption_for_image_id(image_path):
40
  """
41
  Retrieve the caption for a given image_id from the JSON data.
42
  """
43
+ # Extract the numeric part of the image ID
44
+ match = re.search(r'(\d+)', image_path)
45
+ if match:
46
+ image_id = match.group(1).lstrip('0') # Remove leading zeros
47
+ print("Searching for image_id:", image_id) # Debugging line
48
+
49
+ # Check if image_id is a test image
50
+ if image_id in results:
51
+ print("Found caption in results:", results[image_id]["caption"]) # Debugging line
52
+ return results[image_id]["caption"]
53
+
54
+ # If image_id is not a test image, search in nearest neighbors
55
+ for test_image_data in results.values():
56
+ for neighbor in test_image_data["nearest_neighbors"]:
57
+ if neighbor["image_id"] == image_id:
58
+ print("Found caption in nearest neighbors:", neighbor["caption"]) # Debugging line
59
+ return neighbor["caption"]
60
 
61
  # Return None if the image_id is not found
62
+ print("Caption not found for image_id:", image_id) # Debugging line
63
  return None
64
 
65
  # Function to get a random image that hasn’t been annotated or skipped