my-fix-branch
Shane 7 months ago
parent c1dd68d832
commit 30e871e822
  1. 38
      foodie_utils.py

@ -227,8 +227,8 @@ def get_image(search_query):
current_time = time.time() current_time = time.time()
time_since_last_request = current_time - last_flickr_request_time time_since_last_request = current_time - last_flickr_request_time
if time_since_last_request < 1: if time_since_last_request < 5:
time.sleep(1 - time_since_last_request) time.sleep(5 - time_since_last_request)
last_flickr_request_time = time.time() last_flickr_request_time = time.time()
@ -272,8 +272,19 @@ def get_image(search_query):
temp_file = None temp_file = None
try: try:
img_response = requests.get(img_url, headers=headers, timeout=10) for attempt in range(3):
img_response.raise_for_status() img_response = requests.get(img_url, headers=headers, timeout=10)
if img_response.status_code == 429:
wait_time = 5 * (2 ** attempt)
logging.warning(f"Rate limit hit for {img_url}. Retrying after {wait_time}s (attempt {attempt+1}/3).")
time.sleep(wait_time)
continue
img_response.raise_for_status()
break
else:
logging.warning(f"Rate limit hit for {img_url} after retries. Falling back to Pixabay.")
return None
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file: with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
temp_file.write(img_response.content) temp_file.write(img_response.content)
temp_path = temp_file.name temp_path = temp_file.name
@ -313,7 +324,7 @@ def get_image(search_query):
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
if e.response.status_code == 429: if e.response.status_code == 429:
logging.warning(f"Rate limit hit for {img_url}. Falling back to Pixabay.") logging.warning(f"Rate limit hit for {img_url} after retries. Falling back to Pixabay.")
return None return None
else: else:
logging.warning(f"Download failed for {img_url}: {e}") logging.warning(f"Download failed for {img_url}: {e}")
@ -341,6 +352,7 @@ def get_image(search_query):
photo_id = match.group(1) photo_id = match.group(1)
photo_ids.add(photo_id) photo_ids.add(photo_id)
photo_ids = list(photo_ids)[:5] # Limit to 5 IDs
logging.info(f"Found {len(photo_ids)} Flickr photo IDs via DDG: {photo_ids}") logging.info(f"Found {len(photo_ids)} Flickr photo IDs via DDG: {photo_ids}")
return photo_ids return photo_ids
except Exception as e: except Exception as e:
@ -349,14 +361,14 @@ def get_image(search_query):
def classify_keywords(keywords): def classify_keywords(keywords):
prompt = ( prompt = (
"Given the following keywords from an image search query, classify each as 'specific' (e.g., brand names, unique entities) or 'generic' (e.g., common or abstract terms). " "Given the following keywords from an image search query, classify each as 'specific' (e.g., brand names, unique entities like 'Taco Bell' or 'Paris') or 'generic' (e.g., common or abstract terms like 'dining' or 'trends'). "
"Return a JSON object mapping each keyword to its classification.\n\n" "Return a JSON object mapping each keyword to its classification.\n\n"
"Keywords: " + ", ".join(keywords) + "\n\n" "Keywords: " + ", ".join(keywords) + "\n\n"
"Example output:\n" "Example output format (do not use these exact keywords in your response):\n"
"```json\n" "```json\n"
"{\n" "{\n"
" \"Wingstop\": \"specific\",\n" " \"keyword1\": \"specific\",\n"
" \"dining\": \"generic\"\n" " \"keyword2\": \"generic\"\n"
"}\n```" "}\n```"
) )
try: try:
@ -1176,14 +1188,14 @@ def get_flickr_image(search_query, relevance_keywords):
# Helper function to classify keywords as specific or generic # Helper function to classify keywords as specific or generic
def classify_keywords(keywords): def classify_keywords(keywords):
prompt = ( prompt = (
"Given the following keywords from an image search query, classify each as 'specific' (e.g., brand names, unique entities) or 'generic' (e.g., common or abstract terms). " "Given the following keywords from an image search query, classify each as 'specific' (e.g., brand names, unique entities like 'Taco Bell' or 'Paris') or 'generic' (e.g., common or abstract terms like 'dining' or 'trends'). "
"Return a JSON object mapping each keyword to its classification.\n\n" "Return a JSON object mapping each keyword to its classification.\n\n"
"Keywords: " + ", ".join(keywords) + "\n\n" "Keywords: " + ", ".join(keywords) + "\n\n"
"Example output:\n" "Example output format (do not use these exact keywords in your response):\n"
"```json\n" "```json\n"
"{\n" "{\n"
" \"Wingstop\": \"specific\",\n" " \"keyword1\": \"specific\",\n"
" " \"dining\": \"generic\"\n" " \"keyword2\": \"generic\"\n"
"}\n```" "}\n```"
) )
try: try:

Loading…
Cancel
Save