raannakasturi commited on
Commit
3c517aa
·
verified ·
1 Parent(s): 670359a

Update image.py

Browse files
Files changed (1) hide show
  1. image.py +98 -98
image.py CHANGED
@@ -1,99 +1,99 @@
1
- import base64
2
- import io
3
- import os
4
- import re
5
- import requests
6
- import dotenv
7
- from PIL import Image
8
- import pollinations as ai
9
-
10
- dotenv.load_dotenv()
11
-
12
- def extract_summary(text):
13
- text = text.replace("#", "").strip().lower()
14
- match = re.search(r"summary(.*?)highlights", text, re.DOTALL)
15
- if match:
16
- return match.group(1).strip()
17
- return text
18
-
19
- def fix_base64_padding(data):
20
- missing_padding = len(data) % 4
21
- if missing_padding:
22
- data += "=" * (4 - missing_padding)
23
- return data
24
-
25
- def generate_image(title, summary):
26
- try:
27
- extracted_summary = extract_summary(summary)
28
- prompt = f"{title.strip()}: {extracted_summary.strip()}"
29
- model_obj = ai.ImageModel(
30
- model=ai.flux_pro,
31
- width=1280,
32
- height=720,
33
- seed=2342342340,
34
- enhance=True,
35
- nologo=True
36
- )
37
- image = model_obj.generate(
38
- prompt=prompt,
39
- negative="low quality, blurry, pixelated",
40
- save=True,
41
- file="image.png"
42
- )
43
- if type(image) == str:
44
- print(f"An error occurred during image generation: {image}")
45
- return None
46
- image_url = image.params.get("url")
47
- if image_url:
48
- img_data = requests.get(image_url).content
49
- base64_encoded = base64.b64encode(img_data).decode("utf-8")
50
- return f"data:image/png;base64,{base64_encoded}"
51
- return None
52
- except Exception as e:
53
- print(f"An error occurred during image generation: {e}")
54
- return None
55
-
56
- def verify_image(image_data):
57
- try:
58
- image_stream = io.BytesIO(image_data)
59
- img = Image.open(image_stream)
60
- img.verify()
61
- return True
62
- except Exception as e:
63
- print(f"Error verifying image: {e}")
64
- return False
65
-
66
- def upload_image(data_uri, api_key):
67
- image_url = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
68
- try:
69
- base64_image = fix_base64_padding(data_uri.split(",")[1])
70
- url = f"https://api.imgbb.com/1/upload?key={api_key}"
71
- response = requests.post(url, data={"image": base64_image}).json()
72
- if response.get("status") == 200:
73
- image_url = response["data"]["display_url"]
74
- else:
75
- print(f"Error uploading image: {response}")
76
- image_url = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
77
- except Exception as e:
78
- print(f"Error uploading image: {e}")
79
- image_url = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
80
- finally:
81
- return image_url
82
-
83
- def fetch_image(title, summary, api_key):
84
- image_url = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
85
- try:
86
- data_uri = generate_image(title, summary)
87
- if data_uri:
88
- base64_image = fix_base64_padding(data_uri.split(",")[1])
89
- image_data = base64.b64decode(base64_image)
90
- if verify_image(image_data):
91
- image_url = upload_image(data_uri, api_key)
92
- else:
93
- image_url = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
94
- except Exception as e:
95
- print(f"Error fetching image: {e}")
96
- image_url = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png"
97
- finally:
98
- os.remove("image.png")
99
  return image_url
 
1
+ import base64
2
+ import io
3
+ import os
4
+ import re
5
+ import requests
6
+ import dotenv
7
+ from PIL import Image
8
+ import pollinations as ai
9
+
10
+ dotenv.load_dotenv()
11
+
12
+ def extract_summary(text):
13
+ text = text.replace("#", "").strip().lower()
14
+ match = re.search(r"summary(.*?)highlights", text, re.DOTALL)
15
+ if match:
16
+ return match.group(1).strip()
17
+ return text
18
+
19
+ def fix_base64_padding(data):
20
+ missing_padding = len(data) % 4
21
+ if missing_padding:
22
+ data += "=" * (4 - missing_padding)
23
+ return data
24
+
25
+ def generate_image(title, summary):
26
+ try:
27
+ extracted_summary = extract_summary(summary)
28
+ prompt = f"{title.strip()}: {extracted_summary.strip()}"
29
+ model_obj = ai.ImageModel(
30
+ model=ai.flux_pro,
31
+ width=1280,
32
+ height=720,
33
+ seed=2342342340,
34
+ enhance=True,
35
+ nologo=True
36
+ )
37
+ image = model_obj.generate(
38
+ prompt=prompt,
39
+ negative="low quality, blurry, pixelated, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, poorly rendered hands, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, bad composition, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, unreal, animate, cgi, render, artwork, illustration, 3d render, cinema 4d, artstation, octane render, mutated body parts, painting, oil painting, 2d, sketch, bad photography, bad photo, deviant art, aberrations, abstract, anime, black and white, collapsed, conjoined, creative, drawing, extra windows, harsh lighting, jpeg artifacts, low saturation, monochrome, multiple levels, overexposed, oversaturated, photoshop, rotten, surreal, twisted, UI, underexposed, unnatural, unreal engine, unrealistic, video game, deformed body features",
40
+ save=True,
41
+ file="image.png"
42
+ )
43
+ if type(image) == str:
44
+ print(f"An error occurred during image generation: {image}")
45
+ return None
46
+ image_url = image.params.get("url")
47
+ if image_url:
48
+ img_data = requests.get(image_url).content
49
+ base64_encoded = base64.b64encode(img_data).decode("utf-8")
50
+ return f"data:image/png;base64,{base64_encoded}"
51
+ return None
52
+ except Exception as e:
53
+ print(f"An error occurred during image generation: {e}")
54
+ return None
55
+
56
+ def verify_image(image_data):
57
+ try:
58
+ image_stream = io.BytesIO(image_data)
59
+ img = Image.open(image_stream)
60
+ img.verify()
61
+ return True
62
+ except Exception as e:
63
+ print(f"Error verifying image: {e}")
64
+ return False
65
+
66
+ def upload_image(data_uri, api_key):
67
+ image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
68
+ try:
69
+ base64_image = fix_base64_padding(data_uri.split(",")[1])
70
+ url = f"https://api.imgbb.com/1/upload?key={api_key}"
71
+ response = requests.post(url, data={"image": base64_image}).json()
72
+ if response.get("status") == 200:
73
+ image_url = response["data"]["display_url"]
74
+ else:
75
+ print(f"Error uploading image: {response}")
76
+ image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
77
+ except Exception as e:
78
+ print(f"Error uploading image: {e}")
79
+ image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
80
+ finally:
81
+ return image_url
82
+
83
+ def fetch_image(title, summary, api_key):
84
+ image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
85
+ try:
86
+ data_uri = generate_image(title, summary)
87
+ if data_uri:
88
+ base64_image = fix_base64_padding(data_uri.split(",")[1])
89
+ image_data = base64.b64decode(base64_image)
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:
95
+ print(f"Error fetching image: {e}")
96
+ image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
97
+ finally:
98
+ os.remove("image.png")
99
  return image_url