raannakasturi commited on
Commit
fc1cea9
·
1 Parent(s): 8e10899

Refactor image generation to use API endpoint and remove pollinations dependency

Browse files
Files changed (2) hide show
  1. image.py +16 -23
  2. requirements.txt +0 -1
image.py CHANGED
@@ -3,8 +3,8 @@ import io
3
  import os
4
  import re
5
  import requests
 
6
  from PIL import Image
7
- import pollinations as ai
8
 
9
  def extract_summary(text):
10
  text = text.replace("#", "").strip().lower()
@@ -22,27 +22,14 @@ def fix_base64_padding(data):
22
  def generate_image(title, summary):
23
  try:
24
  extracted_summary = extract_summary(summary)
25
- prompt = f"{title.strip()}: {extracted_summary.strip()}"
26
- model_obj = ai.ImageModel(
27
- model=ai.flux_pro,
28
- width=1280,
29
- height=720,
30
- seed=2342342340,
31
- enhance=True,
32
- nologo=True
33
- )
34
- image = model_obj.generate(
35
- prompt=prompt,
36
- 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",
37
- save=True,
38
- file="image.png"
39
- )
40
- if type(image) == str:
41
- print(f"An error occurred during image generation: {image}")
42
- return None
43
- image_url = image.params.get("url")
44
- if image_url:
45
- img_data = requests.get(image_url, timeout=60).content
46
  base64_encoded = base64.b64encode(img_data).decode("utf-8")
47
  return f"data:image/png;base64,{base64_encoded}"
48
  return None
@@ -94,4 +81,10 @@ def fetch_image(title, summary, api_key):
94
  finally:
95
  if os.path.exists("image.png"):
96
  os.remove("image.png")
97
- return image_url
 
 
 
 
 
 
 
3
  import os
4
  import re
5
  import requests
6
+ from urllib.parse import quote
7
  from PIL import Image
 
8
 
9
  def extract_summary(text):
10
  text = text.replace("#", "").strip().lower()
 
22
  def generate_image(title, summary):
23
  try:
24
  extracted_summary = extract_summary(summary)
25
+ 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",
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
 
81
  finally:
82
  if os.path.exists("image.png"):
83
  os.remove("image.png")
84
+ return image_url
85
+
86
+
87
+ if __name__ == "__main__":
88
+ title = "A beautiful sunset over the mountains"
89
+ summary = "A beautiful sunset over the mountains with vibrant colors and a clear sky."
90
+ print(fetch_image(title, summary, "aa38b04047587c609f5c7e22f9d840f0"))
requirements.txt CHANGED
@@ -5,5 +5,4 @@ mistune==3.0.2
5
  nbconvert==7.16.4
6
  requests==2.32.3
7
  pillow==11.0.0
8
- pollinations==2.1
9
  sib-api-v3-sdk==7.6.0
 
5
  nbconvert==7.16.4
6
  requests==2.32.3
7
  pillow==11.0.0
 
8
  sib-api-v3-sdk==7.6.0