File size: 5,157 Bytes
3c517aa
 
 
 
f50b29d
3c517aa
10266ab
5100f6b
82d7e57
 
3c517aa
f50b29d
 
 
 
 
 
 
 
 
 
 
 
 
3c517aa
f50b29d
 
 
3c517aa
 
f50b29d
3c517aa
 
f50b29d
 
 
3c517aa
 
 
 
 
704d4a1
dcc4dc2
 
f50b29d
 
 
5100f6b
 
 
e1f51f1
5100f6b
 
 
f50b29d
 
 
dcc4dc2
f50b29d
 
 
 
 
 
 
 
 
dcc4dc2
f50b29d
dcc4dc2
 
 
f50b29d
 
 
 
dcc4dc2
 
f50b29d
 
 
 
3c517aa
 
 
 
 
 
 
 
 
 
 
eb6ef16
704d4a1
f50b29d
3c517aa
704d4a1
3c517aa
f50b29d
 
 
 
 
 
3c517aa
 
f50b29d
3c517aa
df4da64
 
227f9f9
 
704d4a1
 
f50b29d
 
 
 
eb6ef16
f50b29d
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
118
119
120
121
122
import base64
import io
import os
import re
import time
from PIL import Image
from g4f.client import Client
from g4f.Provider import RetryProvider, PollinationsAI, ImageLabs, Blackbox, HuggingSpace, Airforce, Prodia
from g4f.Provider.hf_space.BlackForestLabsFlux1Schnell import BlackForestLabsFlux1Schnell
from g4f.Provider.hf_space.VoodoohopFlux1Schnell import VoodoohopFlux1Schnell

NEGATIVE_PROMPT = (
    "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"
)

def extract_summary(text):
    """
    Clean and extract the summary portion from the text.
    """
    text = text.replace("#", "").strip().lower()
    match = re.search(r"summary(.*?)highlights", text, re.DOTALL)
    return match.group(1).strip() if match else text

def fix_base64_padding(data):
    """
    Ensure that the base64 string has the proper padding.
    """
    missing_padding = len(data) % 4
    if missing_padding:
        data += "=" * (4 - missing_padding)
    return data

def generate_image(title, category, summary):
    print("Generating image...")
    start = time.time()
    prompt = f"Generate accurate image representing the {category} concept: ```{title.strip()}: {summary.strip()}```"
    client = Client()
    attempts = [
        ([BlackForestLabsFlux1Schnell, VoodoohopFlux1Schnell, HuggingSpace], "flux-schnell"),
        ([HuggingSpace, PollinationsAI], "flux-dev"),
        ([PollinationsAI], "flux-pro"),
        ([Airforce, PollinationsAI, Blackbox], "flux"),
        ([PollinationsAI], "dall-e-3"),
        ([HuggingSpace], "sd-3"),
        ([Prodia], Prodia.default_image_model)
    ]
    
    for providers, model in attempts:
        try:
            provider = RetryProvider(
                providers=providers,
                shuffle=True,
                single_provider_retry=True,
                max_retries=3,
            )
            response = client.images.generate(
                provider=provider,
                model=model,
                prompt=prompt,
                negative_prompt=NEGATIVE_PROMPT,
                response_format="b64_json",
                width=1024,
                height=576,
            )
            img_data = response.data[0].b64_json
            elapsed = time.time() - start
            print(f"Image generated in {elapsed:.2f} seconds using model {model}")
            if img_data:
                return f"data:image/png;base64,{img_data}"
        except Exception as e:
            print(f"Attempt with model {model} failed: {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 fetch_image(title, category, summary):
    summary = extract_summary(summary)
    fallback_url = "https://i.ibb.co/TBJqggw/Image-Not-Found.jpg"
    try:
        data_uri = generate_image(title, category, summary)
        if data_uri:
            base64_str = data_uri.split(",")[1]
            base64_str = fix_base64_padding(base64_str)
            decoded = base64.b64decode(base64_str, validate=True)
            if verify_image(decoded):
                return f"data:image/png;base64,{base64_str}"
        return fallback_url
    except Exception as e:
        print(f"Error fetching image: {e}")
        return fallback_url
    finally:
        if os.path.exists("image.png"):
            os.remove("image.png")

if __name__ == "__main__":
    title = "Exposition: Enumerative Geometry and Tree-Level Gromov-Witten Invariants"
    category = "Mathematics"
    summary = (
        "The text discusses the Kontsevich-Manin formula for enumerating degree d rational curves via Gromov-Witten invariants. "
        "It details the calculation of these invariants using moduli spaces of stable maps and explores their implications in enumerative geometry."
    )
    image_url = fetch_image(title, category, summary)
    print(image_url)