fix
This commit is contained in:
+25
-13
@@ -227,8 +227,8 @@ def get_image(search_query):
|
||||
|
||||
current_time = time.time()
|
||||
time_since_last_request = current_time - last_flickr_request_time
|
||||
if time_since_last_request < 1:
|
||||
time.sleep(1 - time_since_last_request)
|
||||
if time_since_last_request < 5:
|
||||
time.sleep(5 - time_since_last_request)
|
||||
|
||||
last_flickr_request_time = time.time()
|
||||
|
||||
@@ -272,8 +272,19 @@ def get_image(search_query):
|
||||
|
||||
temp_file = None
|
||||
try:
|
||||
img_response = requests.get(img_url, headers=headers, timeout=10)
|
||||
img_response.raise_for_status()
|
||||
for attempt in range(3):
|
||||
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:
|
||||
temp_file.write(img_response.content)
|
||||
temp_path = temp_file.name
|
||||
@@ -313,7 +324,7 @@ def get_image(search_query):
|
||||
|
||||
except requests.exceptions.HTTPError as e:
|
||||
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
|
||||
else:
|
||||
logging.warning(f"Download failed for {img_url}: {e}")
|
||||
@@ -341,6 +352,7 @@ def get_image(search_query):
|
||||
photo_id = match.group(1)
|
||||
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}")
|
||||
return photo_ids
|
||||
except Exception as e:
|
||||
@@ -349,14 +361,14 @@ def get_image(search_query):
|
||||
|
||||
def classify_keywords(keywords):
|
||||
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"
|
||||
"Keywords: " + ", ".join(keywords) + "\n\n"
|
||||
"Example output:\n"
|
||||
"Example output format (do not use these exact keywords in your response):\n"
|
||||
"```json\n"
|
||||
"{\n"
|
||||
" \"Wingstop\": \"specific\",\n"
|
||||
" \"dining\": \"generic\"\n"
|
||||
" \"keyword1\": \"specific\",\n"
|
||||
" \"keyword2\": \"generic\"\n"
|
||||
"}\n```"
|
||||
)
|
||||
try:
|
||||
@@ -1176,14 +1188,14 @@ def get_flickr_image(search_query, relevance_keywords):
|
||||
# Helper function to classify keywords as specific or generic
|
||||
def classify_keywords(keywords):
|
||||
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"
|
||||
"Keywords: " + ", ".join(keywords) + "\n\n"
|
||||
"Example output:\n"
|
||||
"Example output format (do not use these exact keywords in your response):\n"
|
||||
"```json\n"
|
||||
"{\n"
|
||||
" \"Wingstop\": \"specific\",\n"
|
||||
" " \"dining\": \"generic\"\n"
|
||||
" \"keyword1\": \"specific\",\n"
|
||||
" \"keyword2\": \"generic\"\n"
|
||||
"}\n```"
|
||||
)
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user