File size: 5,533 Bytes
3c517aa
 
 
 
 
fc1cea9
3c517aa
10266ab
 
 
3c517aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0485034
10266ab
0485034
10266ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc1cea9
10266ab
3c517aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22c0846
3c517aa
 
 
 
 
 
 
 
 
 
 
 
95ef056
 
3c517aa
 
227f9f9
 
 
 
 
 
3c517aa
 
227f9f9
 
 
 
 
 
 
 
3c517aa
 
 
 
 
 
df4da64
 
fc1cea9
227f9f9
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import base64
import io
import os
import re
import requests
from urllib.parse import quote
from PIL import Image
from g4f.client import Client
from g4f.Provider import RetryProvider, Airforce, Blackbox, BlackboxCreateAgent, PollinationsAI


def extract_summary(text):
    text = text.replace("#", "").strip().lower()
    match = re.search(r"summary(.*?)highlights", text, re.DOTALL)
    if match:
        return match.group(1).strip()
    return text

def fix_base64_padding(data):
    missing_padding = len(data) % 4
    if missing_padding:
        data += "=" * (4 - missing_padding)
    return data

def generate_image(title, summary):
    try:
        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, NSFW, NUDE, vulgar, negative, unsuitable, inappropriate, offensive, revealing, sexual, explicit",
        extracted_summary = extract_summary(summary)
        prompt = quote(f"[[IMAGE GENERATED SHOULD BE SAFE FOR WORK (SFW). NO NUDES OR ANYTHING REVEALING IMAGES NOR SHOULD THEY BE VULGAR OR UNSCIENTIFIC]] [[(({title.strip()}))]]: {extracted_summary.strip()}")
        client = Client(
            image_provider=RetryProvider(
                providers=[Airforce, Blackbox, BlackboxCreateAgent, PollinationsAI],
                shuffle=True,
                single_provider_retry=True,
                max_retries=3,
            )
        )
        img_data = client.images.generate(
            model="flux",
            prompt=prompt,
            negative_prompt=negative,
            response_format="b64_json",
            width=1024,
            height=576,
        ).data[0].b64_json
        if img_data:
            return f"data:image/png;base64,{img_data}"
        return None
    except Exception as e:
        print(f"An error occurred during image generation: {e}")
        return None

def verify_image(image_data):
    try:
        image_stream = io.BytesIO(image_data)
        img = Image.open(image_stream)
        img.verify()
        return True
    except Exception as e:
        print(f"Error verifying image: {e}")
        return False

def upload_image(data_uri, api_key):
    image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
    try:
        base64_image = fix_base64_padding(data_uri.split(",")[1])
        url = f"https://api.imgbb.com/1/upload?key={api_key}"
        response = requests.post(url, data={"image": base64_image}, timeout=60).json()
        if response.get("status") == 200:
            image_url = response["data"]["display_url"]
        else:
            print(f"Error uploading image: {response}")
            image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
    except Exception as e:
        print(f"Error uploading image: {e}")
        image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
    finally:
        return image_url

def fetch_image(title, summary, api_key):
    title = r"{}".format(title)
    summary = r"{}".format(summary)
    image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
    try:
        data_uri = None
        i = 1
        while not data_uri and i <= 3:
            print(f"Attempt {i} to fetch image")
            data_uri = generate_image(title, summary)
            i += 1
        if data_uri:
            base64_image = fix_base64_padding(data_uri.split(",")[1])
            image_data = None
            try:
                image_data = base64.b64decode(base64_image, validate=True)
            except Exception as e:
                print(f"Invalid base64 data: {e}")
            if image_data:
                if verify_image(image_data):
                    image_url = upload_image(data_uri, api_key)
        else:
            image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
    except Exception as e:
        print(f"Error fetching image: {e}")
        image_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
    finally:
        if os.path.exists("image.png"):
            os.remove("image.png")
        return image_url


if __name__ == "__main__":
    title = "Accelerated cell-type-specific regulatory evolution of the Homo sapiens brain"
    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"
    api_key = "aa38b04047587c609f5c7e22f9d840f0"
    image_url = fetch_image(title, summary, api_key)
    print(image_url)