raannakasturi commited on
Commit
227f9f9
·
1 Parent(s): 7880100

Enhance image generation and fetching with error handling and retry logic

Browse files
Files changed (1) hide show
  1. image.py +30 -8
image.py CHANGED
@@ -26,12 +26,16 @@ def generate_image(title, summary):
26
  image_api_endpoint = "image.pollinations.ai"
27
  params = f"negative={quote(str(negative))}&seed=2342342340&width=1024&height=576&nologo=True&private=False&model=turbo&enhance=True"
28
  data_header = {"Content-Type": "application/json"}
29
- prompt = quote(f"{title.strip()}: {extracted_summary.strip()}")
30
  image_url = f"https://{image_api_endpoint}/prompt/{prompt}?{params}"
31
- img_data = requests.get(image_url, headers=data_header, timeout=60).content
 
 
 
 
32
  if img_data:
33
- base64_encoded = base64.b64encode(img_data).decode("utf-8")
34
- return f"data:image/png;base64,{base64_encoded}"
35
  return None
36
  except Exception as e:
37
  print(f"An error occurred during image generation: {e}")
@@ -69,12 +73,22 @@ def fetch_image(title, summary, api_key):
69
  summary = r"{}".format(summary)
70
  image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
71
  try:
72
- data_uri = generate_image(title, summary)
 
 
 
 
 
73
  if data_uri:
74
  base64_image = fix_base64_padding(data_uri.split(",")[1])
75
- image_data = base64.b64decode(base64_image)
76
- if verify_image(image_data):
77
- image_url = upload_image(data_uri, api_key)
 
 
 
 
 
78
  else:
79
  image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
80
  except Exception as e:
@@ -84,3 +98,11 @@ def fetch_image(title, summary, api_key):
84
  if os.path.exists("image.png"):
85
  os.remove("image.png")
86
  return image_url
 
 
 
 
 
 
 
 
 
26
  image_api_endpoint = "image.pollinations.ai"
27
  params = f"negative={quote(str(negative))}&seed=2342342340&width=1024&height=576&nologo=True&private=False&model=turbo&enhance=True"
28
  data_header = {"Content-Type": "application/json"}
29
+ prompt = quote(f"[[(({title.strip()}))]]: {extracted_summary.strip()}")
30
  image_url = f"https://{image_api_endpoint}/prompt/{prompt}?{params}"
31
+ response = requests.get(image_url, headers=data_header)
32
+ if response.status_code != 200:
33
+ print(f"Error generating image: {response.text}")
34
+ return None
35
+ img_data = response.content
36
  if img_data:
37
+ data = base64.b64encode(img_data).decode("utf-8")
38
+ return f"data:image/png;base64,{data}"
39
  return None
40
  except Exception as e:
41
  print(f"An error occurred during image generation: {e}")
 
73
  summary = r"{}".format(summary)
74
  image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
75
  try:
76
+ data_uri = None
77
+ i = 1
78
+ while not data_uri and i <= 3:
79
+ print(f"Attempt {i} to fetch image")
80
+ data_uri = generate_image(title, summary)
81
+ i += 1
82
  if data_uri:
83
  base64_image = fix_base64_padding(data_uri.split(",")[1])
84
+ image_data = None
85
+ try:
86
+ image_data = base64.b64decode(base64_image, validate=True)
87
+ except Exception as e:
88
+ print(f"Invalid base64 data: {e}")
89
+ if image_data:
90
+ if verify_image(image_data):
91
+ image_url = upload_image(data_uri, api_key)
92
  else:
93
  image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
94
  except Exception as e:
 
98
  if os.path.exists("image.png"):
99
  os.remove("image.png")
100
  return image_url
101
+
102
+
103
+ if __name__ == "__main__":
104
+ title = "Accelerated cell-type-specific regulatory evolution of the Homo sapiens brain"
105
+ summary = "This study investigates the accelerated regulatory evolution of gene expression in human brain cell types compared to chimpanzees. It reveals significant differences in gene expression, particularly in excitatory and inhibitory neurons, highlighting the role of regulatory evolution in human cognitive and behavioral traits"
106
+ api_key = "aa38b04047587c609f5c7e22f9d840f0"
107
+ image_url = fetch_image(title, summary, api_key)
108
+ print(image_url)