Spaces:
Running
Running
smileycutie0
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
MAX_SEED = 2 ** 31 - 1
|
| 9 |
+
session = requests.Session()
|
| 10 |
+
|
| 11 |
+
def _fetch_url(*args, **kwargs):
|
| 12 |
+
try:
|
| 13 |
+
response = session.get(*args, **kwargs)
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
return response.content
|
| 16 |
+
except requests.RequestException as e:
|
| 17 |
+
print(f"Request error: {e}")
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
def generate(
|
| 21 |
+
prompt,
|
| 22 |
+
seed=None,
|
| 23 |
+
randomize_seed=True,
|
| 24 |
+
width=1024,
|
| 25 |
+
height=1024
|
| 26 |
+
):
|
| 27 |
+
if seed is None or randomize_seed:
|
| 28 |
+
seed = random.randint(0, MAX_SEED)
|
| 29 |
+
url = f"https://pollinations.ai/p/{prompt}?nologo=true&private=true"
|
| 30 |
+
params = dict(prompt=prompt, seed=seed, width=width, height=height)
|
| 31 |
+
image = _fetch_url(url, params=params)
|
| 32 |
+
if image:
|
| 33 |
+
return Image.open(io.BytesIO(image))
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
with gr.Blocks(
|
| 37 |
+
title="Text-to-image UI",
|
| 38 |
+
css="footer {display: none !important}"
|
| 39 |
+
) as app:
|
| 40 |
+
gr.Markdown("Text-to-image UI by [cherry-ghosts](https://hf.co/cherry-ghosts)")
|
| 41 |
+
with gr.Tab("Generate"):
|
| 42 |
+
result = gr.Image(
|
| 43 |
+
label="Image",
|
| 44 |
+
show_label=False,
|
| 45 |
+
format="jpeg",
|
| 46 |
+
interactive=False
|
| 47 |
+
)
|
| 48 |
+
with gr.Group():
|
| 49 |
+
with gr.Row():
|
| 50 |
+
prompt = gr.Textbox(
|
| 51 |
+
label="Prompt",
|
| 52 |
+
show_label=False,
|
| 53 |
+
placeholder="Enter your prompt..",
|
| 54 |
+
max_lines=1,
|
| 55 |
+
container=False
|
| 56 |
+
)
|
| 57 |
+
btn = gr.Button("Generate", variant="primary")
|
| 58 |
+
|
| 59 |
+
with gr.Tab("Advanced settings"):
|
| 60 |
+
with gr.Row():
|
| 61 |
+
seed = gr.Slider(
|
| 62 |
+
label="Seed",
|
| 63 |
+
minimum=0,
|
| 64 |
+
maximum=MAX_SEED,
|
| 65 |
+
step=1
|
| 66 |
+
value=0
|
| 67 |
+
)
|
| 68 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
width = gr.Slider(
|
| 72 |
+
label="Width",
|
| 73 |
+
minimum=256,
|
| 74 |
+
maximum=1344,
|
| 75 |
+
step=64
|
| 76 |
+
value=1024
|
| 77 |
+
)
|
| 78 |
+
height = gr.Slider(
|
| 79 |
+
label="Height",
|
| 80 |
+
minimum=256,
|
| 81 |
+
maximum=1344,
|
| 82 |
+
step=64
|
| 83 |
+
value=1024
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
btn.click(
|
| 87 |
+
generate,
|
| 88 |
+
inputs=[prompt, seed, randomize_seed, width, height],
|
| 89 |
+
outputs=[result],
|
| 90 |
+
api_name="run"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
app.queue().launch(debug=True, ssr_mode=False)
|