rairo commited on
Commit
b8d3065
·
verified ·
1 Parent(s): 3203c15

Update image_gen.py

Browse files
Files changed (1) hide show
  1. image_gen.py +22 -0
image_gen.py CHANGED
@@ -250,6 +250,28 @@ def generate_image(prompt_text, style, model="hf"):
250
  # Return a placeholder image in case of failure
251
  return Image.new('RGB', (1024, 1024), color=(200,200,200)), None
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  # edit image function
254
  def edit_section_image(image_url: str, gemini_prompt: str):
255
  """
 
250
  # Return a placeholder image in case of failure
251
  return Image.new('RGB', (1024, 1024), color=(200,200,200)), None
252
 
253
+ def generate_image_with_retry(prompt_text, style, model="hf", max_retries=3):
254
+ """
255
+ Attempt to generate an image using generate_image, retrying up to max_retries if needed.
256
+ Args:
257
+ prompt_text (str): The text prompt for image generation.
258
+ style (str or None): The style of the image (ignored for Pollinations Turbo).
259
+ model (str): Which model to use ("hf" or "pollinations_turbo").
260
+ max_retries (int): Maximum number of retries.
261
+ Returns:
262
+ tuple: The generated image and its Base64 string.
263
+ """
264
+ for attempt in range(max_retries):
265
+ try:
266
+ if attempt > 0:
267
+ time.sleep(2 ** attempt)
268
+ return generate_image(prompt_text, style, model=model)
269
+ except Exception as e:
270
+ logger.error(f"Attempt {attempt+1} failed: {e}")
271
+ if attempt == max_retries - 1:
272
+ raise
273
+ return None, None
274
+
275
  # edit image function
276
  def edit_section_image(image_url: str, gemini_prompt: str):
277
  """