raannakasturi commited on
Commit
6415f78
·
1 Parent(s): 110b052

Refactor image handling by moving image generation and verification to a new module, and update post creation to use the new fetch_image function

Browse files
Files changed (2) hide show
  1. image.py +78 -0
  2. post_blog.py +14 -66
image.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import pollinations as ai
3
+ from PIL import Image
4
+ import io
5
+ import re
6
+ import requests
7
+
8
+ def extract_summary(text):
9
+ text = text.replace("#", "").strip().lower()
10
+ match = re.search(r"summary(.*?)highlights", text.replace("#", ""), re.DOTALL)
11
+ if match:
12
+ return match.group(1).replace("#", "").replace("\n", "").strip()
13
+ return None
14
+
15
+ def generate_image(title, summary):
16
+ try:
17
+ extracted_summary = extract_summary(summary)
18
+ if not extracted_summary:
19
+ extracted_summary = summary
20
+ data = r"{}: {}".format(title.replace("{", "").replace("}","").replace("\n", ""), extracted_summary).strip().replace("&", "&").replace("{", "").replace("}","").replace("\n", "")
21
+ model_obj = ai.ImageModel(
22
+ model=ai.turbo,
23
+ width=1280,
24
+ height=720,
25
+ seed=2342342340,
26
+ enhance=True,
27
+ nologo=True
28
+ )
29
+ print(data)
30
+ model_obj.generate(
31
+ prompt=r"{}".format(data),
32
+ negative=("a bad image, low res, low quality, pixelated, blurry, bad lighting, bad angle, bad composition, bad focus, bad exposure, bad white balance, bad color, bad contrast, bad saturation, bad sharpness, bad noise, bad artifacts, bad framing, bad cropping, bad editing, bad filter, bad lens, bad camera, bad sensor, bad settings, bad post-processing, bad retouching, bad enhancement, bad manipulation, bad distortion, bad watermark, bad logo, bad text, bad overlay, bad background"),
33
+ save=True,
34
+ file="image.png"
35
+ )
36
+ with open("image.png", "rb") as image_file:
37
+ image_data = image_file.read()
38
+ base64_encoded = base64.b64encode(image_data).decode('utf-8')
39
+ data_uri = f"data:image/png;base64,{base64_encoded}"
40
+ return r"{}".format(data_uri).strip()
41
+ except Exception as e:
42
+ return f"An error occurred: {e}"
43
+
44
+ def verify_image(image_data):
45
+ try:
46
+ image_stream = io.BytesIO(image_data)
47
+ image = Image.open(image_stream)
48
+ image.verify()
49
+ image_stream.seek(0)
50
+ image = Image.open(image_stream)
51
+ return True
52
+ except Exception as e:
53
+ print(f"Error verifying image: {e}")
54
+ return False
55
+
56
+ def upload_image(data_uri, api_key):
57
+ try:
58
+ # Strip the `data:image/png;base64,` prefix
59
+ base64_image = data_uri.split(",")[1]
60
+ url = f'https://api.imgbb.com/1/upload?key={api_key}'
61
+ response = requests.post(url, data={'image': base64_image}).json()
62
+ print(response)
63
+ if response.get('status') == 200:
64
+ return response['data']['url']
65
+ else:
66
+ return "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
67
+ except Exception as e:
68
+ print(f"Error uploading image: {e}")
69
+ return "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
70
+
71
+ def fetch_image(title, summary, api_key):
72
+ image_url = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
73
+ data_uri = generate_image(title, summary)
74
+ image_data = base64.b64decode(data_uri.split(",")[1])
75
+ if verify_image(image_data):
76
+ image_url = upload_image(data_uri, api_key)
77
+ return image_url
78
+
post_blog.py CHANGED
@@ -1,12 +1,11 @@
1
  import re
2
  import os
3
- import time
4
- from urllib.parse import quote
5
- import base64
6
  import requests
7
  import dotenv
8
  import mistune
9
 
 
 
10
  dotenv.load_dotenv()
11
  access_key = os.getenv('ACCESS_KEY')
12
  client_id = os.getenv('CLIENT_ID')
@@ -14,64 +13,12 @@ client_secret = os.getenv('CLIENT_SECRET')
14
  refresh_token = os.getenv('REFRESH_TOKEN')
15
  blog_id = os.getenv('BLOG_ID')
16
 
17
- def extract_summary(text):
18
- text = text.replace("#", "").strip().lower()
19
- match = re.search(r"summary(.*?)highlights", text.replace("#", ""), re.DOTALL)
20
- if match:
21
- return match.group(1).replace("#", "").replace("\n", "").strip()
22
- return None
23
-
24
- from PIL import Image
25
- import io
26
-
27
- def verify_image(image_data):
28
- try:
29
- image_stream = io.BytesIO(image_data)
30
- image = Image.open(image_stream)
31
- image.verify()
32
- image_stream.seek(0)
33
- image = Image.open(image_stream)
34
- return True
35
- except Exception as e:
36
- print(f"Error verifying image: {e}")
37
- return False
38
-
39
- def generate_image(title, summary):
40
- default_image = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
41
- extracted_summary = extract_summary(summary)
42
- if extracted_summary is None:
43
- extracted_summary = title
44
- data = quote(f"{title} : {extracted_summary}")
45
- url = f"https://image.pollinations.ai/prompt/{data}?width=1280&height=720&seed=623862700&nologo=true&model=turbo"
46
- headers = {
47
- "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
48
- "accept-language": "en-US,en-GB;q=0.9,en;q=0.8",
49
- "cache-control": "max-age=0",
50
- "priority": "u=0, i",
51
- "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
52
- "sec-ch-ua-mobile": "?1",
53
- "sec-ch-ua-platform": "\"Android\"",
54
- "sec-fetch-dest": "document",
55
- "sec-fetch-mode": "navigate",
56
- "sec-fetch-site": "none",
57
- "sec-fetch-user": "?1",
58
- "upgrade-insecure-requests": "1",
59
- }
60
- response = requests.get(url, headers=headers)
61
- if response.status_code != 200:
62
- print(f"Failed to fetch image. Status code: {response.status_code}")
63
- return default_image
64
- elif not verify_image(response.content):
65
- return default_image
66
- else:
67
- return url
68
-
69
  def generate_post_html(title, summary, mindmap, citation):
70
- title = title.replace("{", r"{{").replace("}", r"}}")
71
- summary = summary.replace("{", r"{{").replace("}", r"}}")
72
- mindmap = mindmap.replace("{", r"{{").replace("}", r"}}")
73
- citation = citation.replace("{", r"{{").replace("}", r"}}")
74
- image = generate_image(title, summary)
75
  html_summary = mistune.html(summary)
76
  post = f"""
77
  <div>
@@ -139,9 +86,10 @@ def post_post(title, category, body, image):
139
  }],
140
  "title": title,
141
  "content": body,
142
- "labels": [category, "recent"]
143
  }
144
  data = requests.post(url, headers=headers, json=post_data).json()
 
145
  if data['status'] == 'LIVE':
146
  print(f"The post '{title}' is {data['status']}")
147
  return True
@@ -156,10 +104,11 @@ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
156
  if uaccess_key != access_key:
157
  return False
158
  else:
 
159
  post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
160
  status = post_post(post_title, post_category, post_body, post_image)
161
- print(f"Waiting for {2*60} seconds...")
162
- time.sleep(2*60)
163
  if status:
164
  print('Post created successfully')
165
  return True
@@ -174,7 +123,7 @@ def test(access_key):
174
  "2412.16344": {
175
  "id": "2412.16344",
176
  "doi": "https://doi.org/10.48550/arXiv.2412.16344",
177
- "title": "On the Interplay of Constraints from $B_s$, $D$, and $K$ Meson Mixing in $Z^\\prime$ Models with Implications for $b\to s ν\barν$ Transitions",
178
  "category": "Astrophysics",
179
  "citation": "Grant, C. E., Bautz, M. W., Miller, E. D., Foster, R. F., LaMarr, B., Malonis, A., Prigozhin, G., Schneider, B., Leitz, C., &amp; Falcone, A. D. (2024). Focal Plane of the Arcus Probe X-Ray Spectrograph. ArXiv. https://doi.org/10.48550/ARXIV.2412.16344",
180
  "summary": "## Summary\nThe Arcus Probe mission concept provides high-resolution soft X-ray and UV spectroscopy to study the universe. The X-ray Spectrograph (XRS) uses two CCD focal planes to detect and record X-ray photons. Laboratory performance results meet observatory requirements.\n\n## Highlights\n- The Arcus Probe mission concept explores the formation and evolution of clusters, galaxies, and stars.\n- The XRS instrument includes four parallel optical channels and two detector focal plane arrays.\n- The CCDs are designed and manufactured by MIT Lincoln Laboratory (MIT/LL).\n- The XRS focal plane utilizes high heritage MIT/LL CCDs with proven technologies.\n- Laboratory testing confirms CCID-94 performance meets required spectral resolution and readout noise.\n- The Arcus mission includes two co-aligned instruments working simultaneously.\n- The XRS Instrument Control Unit (XICU) controls the activities of the XRS.\n\n## Key Insights\n- The Arcus Probe mission concept provides a significant improvement in sensitivity and resolution over previous missions, enabling breakthrough science in understanding the universe.\n- The XRS instrument's design, including the use of two CCD focal planes and four parallel optical channels, allows for high-resolution spectroscopy and efficient detection of X-ray photons.\n- The CCDs used in the XRS instrument are designed and manufactured by MIT Lincoln Laboratory (MIT/LL), which has a proven track record of producing high-quality CCDs for space missions.\n- The laboratory performance results of the CCID-94 device demonstrate that it meets the required spectral resolution and readout noise for the Arcus mission, indicating that the instrument is capable of achieving its scientific goals.\n- The XRS Instrument Control Unit (XICU) plays a crucial role in controlling the activities of the XRS, including gathering and storing data, and processing event recognition.\n- The Arcus mission's use of two co-aligned instruments working simultaneously allows for a wide range of scientific investigations, including the study of time-domain science and the physics of time-dependent phenomena.\n- The high heritage MIT/LL CCDs used in the XRS focal plane provide a reliable and efficient means of detecting X-ray photons, enabling the instrument to achieve its scientific goals.",
@@ -199,5 +148,4 @@ def test(access_key):
199
  return status
200
 
201
  if __name__ == '__main__':
202
- # test(access_key)
203
- print(generate_image("title", "summary summaryyyyyy highlights"))
 
1
  import re
2
  import os
 
 
 
3
  import requests
4
  import dotenv
5
  import mistune
6
 
7
+ from image import fetch_image
8
+
9
  dotenv.load_dotenv()
10
  access_key = os.getenv('ACCESS_KEY')
11
  client_id = os.getenv('CLIENT_ID')
 
13
  refresh_token = os.getenv('REFRESH_TOKEN')
14
  blog_id = os.getenv('BLOG_ID')
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def generate_post_html(title, summary, mindmap, citation):
17
+ title = title.replace("{", r'{').replace("}", r'}')
18
+ summary = summary.replace("{", r'{').replace("}", r'}')
19
+ mindmap = mindmap.replace("{", r'{').replace("}", r'}')
20
+ citation = citation.replace("{", r'{').replace("}", r'}')
21
+ image = fetch_image(title, summary, os.getenv('IMGBB_API_KEY'))
22
  html_summary = mistune.html(summary)
23
  post = f"""
24
  <div>
 
86
  }],
87
  "title": title,
88
  "content": body,
89
+ "labels": [category, "ZZZZZZZZZ"]
90
  }
91
  data = requests.post(url, headers=headers, json=post_data).json()
92
+ print(data)
93
  if data['status'] == 'LIVE':
94
  print(f"The post '{title}' is {data['status']}")
95
  return True
 
104
  if uaccess_key != access_key:
105
  return False
106
  else:
107
+ status = True
108
  post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
109
  status = post_post(post_title, post_category, post_body, post_image)
110
+ # print(f"Waiting for {2*60} seconds...")
111
+ # time.sleep(2*60)
112
  if status:
113
  print('Post created successfully')
114
  return True
 
123
  "2412.16344": {
124
  "id": "2412.16344",
125
  "doi": "https://doi.org/10.48550/arXiv.2412.16344",
126
+ "title": "Analytic 3D vector non-uniform Fourier crystal optics in arbitrary $\bar{\bar{\varepsilon}}$ dielectric",
127
  "category": "Astrophysics",
128
  "citation": "Grant, C. E., Bautz, M. W., Miller, E. D., Foster, R. F., LaMarr, B., Malonis, A., Prigozhin, G., Schneider, B., Leitz, C., &amp; Falcone, A. D. (2024). Focal Plane of the Arcus Probe X-Ray Spectrograph. ArXiv. https://doi.org/10.48550/ARXIV.2412.16344",
129
  "summary": "## Summary\nThe Arcus Probe mission concept provides high-resolution soft X-ray and UV spectroscopy to study the universe. The X-ray Spectrograph (XRS) uses two CCD focal planes to detect and record X-ray photons. Laboratory performance results meet observatory requirements.\n\n## Highlights\n- The Arcus Probe mission concept explores the formation and evolution of clusters, galaxies, and stars.\n- The XRS instrument includes four parallel optical channels and two detector focal plane arrays.\n- The CCDs are designed and manufactured by MIT Lincoln Laboratory (MIT/LL).\n- The XRS focal plane utilizes high heritage MIT/LL CCDs with proven technologies.\n- Laboratory testing confirms CCID-94 performance meets required spectral resolution and readout noise.\n- The Arcus mission includes two co-aligned instruments working simultaneously.\n- The XRS Instrument Control Unit (XICU) controls the activities of the XRS.\n\n## Key Insights\n- The Arcus Probe mission concept provides a significant improvement in sensitivity and resolution over previous missions, enabling breakthrough science in understanding the universe.\n- The XRS instrument's design, including the use of two CCD focal planes and four parallel optical channels, allows for high-resolution spectroscopy and efficient detection of X-ray photons.\n- The CCDs used in the XRS instrument are designed and manufactured by MIT Lincoln Laboratory (MIT/LL), which has a proven track record of producing high-quality CCDs for space missions.\n- The laboratory performance results of the CCID-94 device demonstrate that it meets the required spectral resolution and readout noise for the Arcus mission, indicating that the instrument is capable of achieving its scientific goals.\n- The XRS Instrument Control Unit (XICU) plays a crucial role in controlling the activities of the XRS, including gathering and storing data, and processing event recognition.\n- The Arcus mission's use of two co-aligned instruments working simultaneously allows for a wide range of scientific investigations, including the study of time-domain science and the physics of time-dependent phenomena.\n- The high heritage MIT/LL CCDs used in the XRS focal plane provide a reliable and efficient means of detecting X-ray photons, enabling the instrument to achieve its scientific goals.",
 
148
  return status
149
 
150
  if __name__ == '__main__':
151
+ test(access_key)