Spaces:
Running
Running
Commit
·
110b052
1
Parent(s):
5e3b796
Add image verification and error handling in generate_image function
Browse files- post_blog.py +41 -3
post_blog.py
CHANGED
@@ -2,6 +2,7 @@ import re
|
|
2 |
import os
|
3 |
import time
|
4 |
from urllib.parse import quote
|
|
|
5 |
import requests
|
6 |
import dotenv
|
7 |
import mistune
|
@@ -20,14 +21,50 @@ def extract_summary(text):
|
|
20 |
return match.group(1).replace("#", "").replace("\n", "").strip()
|
21 |
return None
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def generate_image(title, summary):
|
|
|
24 |
extracted_summary = extract_summary(summary)
|
25 |
if extracted_summary is None:
|
26 |
extracted_summary = title
|
27 |
data = quote(f"{title} : {extracted_summary}")
|
28 |
url = f"https://image.pollinations.ai/prompt/{data}?width=1280&height=720&seed=623862700&nologo=true&model=turbo"
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def generate_post_html(title, summary, mindmap, citation):
|
33 |
title = title.replace("{", r"{{").replace("}", r"}}")
|
@@ -162,4 +199,5 @@ def test(access_key):
|
|
162 |
return status
|
163 |
|
164 |
if __name__ == '__main__':
|
165 |
-
test(access_key)
|
|
|
|
2 |
import os
|
3 |
import time
|
4 |
from urllib.parse import quote
|
5 |
+
import base64
|
6 |
import requests
|
7 |
import dotenv
|
8 |
import mistune
|
|
|
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"}}")
|
|
|
199 |
return status
|
200 |
|
201 |
if __name__ == '__main__':
|
202 |
+
# test(access_key)
|
203 |
+
print(generate_image("title", "summary summaryyyyyy highlights"))
|