File size: 1,555 Bytes
d61f523
 
 
 
0eea65a
d61f523
0eea65a
 
 
 
 
 
 
d61f523
 
0eea65a
 
d61f523
0eea65a
d61f523
 
 
0eea65a
 
 
 
 
 
 
 
 
 
 
 
 
 
d61f523
0eea65a
 
 
 
 
 
 
 
 
 
 
 
145f454
0eea65a
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
import gradio as gr
import requests
import random
import os
from PIL import Image

def process(prompt, seed, width, height):
    if seed == "":
        seed = random.randint(0, 999999999)
    else:
        seed = int(seed)

    filename = f"{random.randint(111111111, 999999999)}.jpg"
    file_path = os.path.join(os.path.dirname(__file__), filename)

    url = f"https://image.pollinations.ai/prompt/{prompt}?model=flux-realism&width={width}&height={height}&nologo=true&seed={seed}"
    response = requests.get(url)

    if response.status_code == 200:
        with open(file_path, "wb") as f:
            f.write(response.content)

        # Convert PNG to JPG using PIL
        try:
            img = Image.open(file_path)
            rgb_img = img.convert("RGB")
            rgb_img.save(file_path, "JPEG")
        except Exception as e:
            return f"Error converting image: {e}"

        return file_path
    else:
        return "Failed to generate image."

title = "Pollinations Image Generator"
description = "Generate high-resolution AI art using Pollinations API. You can customize the seed, resolution, and prompt."

iface = gr.Interface(
    fn=process,
    inputs=[
        gr.Textbox(label="Prompt"),
        gr.Textbox(label="Seed (optional)", placeholder="Leave empty for random"),
        gr.Slider(512, 2048, step=64, label="Width", value=1024),
        gr.Slider(512, 2048, step=64, label="Height", value=1024),
    ],
    outputs=gr.File(label="Download Image"),
    title=title,
    description=description,
)

iface.launch()