Spaces:
Running
Running
File size: 3,559 Bytes
b7130f6 7c7e6fb b7130f6 eb4340e b7130f6 f7e8a5c b7130f6 a80fd23 b7130f6 a80fd23 b7130f6 b4dffda b7130f6 b4dffda b7130f6 b4dffda b7130f6 eb4340e 2e381e6 f2bbc1c 8389ca8 7acd290 f2bbc1c 8389ca8 ca8bf87 b7130f6 eb4340e 9103190 b7130f6 |
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 123 124 125 126 127 128 129 |
import io
import random
import gradio as gr
import requests
from PIL import Image
MAX_SEED = 2 ** 31 - 1
session = requests.Session()
def _fetch_url(*args, **kwargs):
try:
response = session.get(*args, **kwargs)
response.raise_for_status()
return response.content
except requests.RequestException as e:
raise gr.Error(f"Request error. Status code: {response.status_code}") from None
return None
def add_to_gallery(image, gallery=None):
if gallery is None:
gallery = []
if image is not None:
gallery = gallery + [image]
return gallery
def generate(
prompt,
seed=None,
randomize_seed=True,
width=1024,
height=1024
):
if seed is None or randomize_seed:
seed = random.randint(0, MAX_SEED)
url = f"https://pollinations.ai/p/{prompt}?nologo=true&private=true"
params = dict(prompt=prompt, seed=seed, width=width, height=height)
image = _fetch_url(url, params=params)
if image:
return Image.open(io.BytesIO(image))
return None
with gr.Blocks(
title="Text-to-image UI",
css="footer {display: none !important}",
theme=gr.themes.Base(
primary_hue="red",
secondary_hue="fuchsia",
neutral_hue="neutral"
)
) as app:
gr.Markdown("# Text-to-Image UI")
with gr.Tab("🎨 Generate"):
result = gr.Image(
label="Image",
show_label=False,
format="jpeg",
interactive=False
)
with gr.Group():
with gr.Row():
prompt = gr.Textbox(
label="Prompt",
show_label=False,
placeholder="Enter your prompt..",
max_lines=1,
container=False
)
btn = gr.Button("Generate", variant="primary")
with gr.Tab("⚙️ Settings"):
with gr.Row():
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=1344,
step=64,
value=1024
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=1344,
step=64,
value=1024
)
with gr.Tab("🖼️ Gallery"):
gallery = gr.Gallery(
label="Gallery",
show_label=False,
format="jpeg",
interactive=False
)
with gr.Accordion("ℹ️ About this UI", open=False):
gr.Markdown(f"""
* Created by [🍒 cherry-ghosts community](https://hf.co/cherry-ghosts)
* Powered by [🐝 Pollinations](https://pollinations.ai) | [GitHub](https://github.com/pollinations/pollinations)
* Running on [Gradio](https://www.gradio.app) v{gr.__version__}
""")
gr.DuplicateButton()
btn.click(
generate,
inputs=[prompt, seed, randomize_seed, width, height],
outputs=[result],
api_name="run"
).then(
add_to_gallery,
inputs=[result, gallery],
outputs=[gallery],
queue=False,
show_api=False
)
if __name__ == "__main__":
app.queue().launch(debug=True, ssr_mode=False) |