Spaces:
Sleeping
Sleeping
File size: 2,187 Bytes
43d2da7 ea8145c 43d2da7 ea8145c 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 000f7a6 43d2da7 000f7a6 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff 43d2da7 b0c4eff | 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 | import gradio as gr
import requests
from PIL import Image
from io import BytesIO
import urllib.parse
import spaces
@spaces.GPU(duration=60)
def generate_image(prompt: str, width: int = 1024, height: int = 1024, seed: int = -1):
if not prompt or not prompt.strip():
return None, "Please enter a prompt."
encoded_prompt = urllib.parse.quote(prompt.strip())
url = f"https://image.pollinations.ai/prompt/{encoded_prompt}"
params = {
"width": width,
"height": height,
"nologo": "true",
"enhance": "true"
}
if seed != -1:
params["seed"] = int(seed)
try:
response = requests.get(url, params=params, timeout=90)
response.raise_for_status()
image = Image.open(BytesIO(response.content))
return image, f"✅ Generated successfully\nPrompt: {prompt}"
except Exception as e:
return None, f"❌ Error: {str(e)}"
with gr.Blocks(title="Image Generator") as demo:
gr.Markdown("""
# 🎨 Image Generator
Powered by **Pollinations.ai** - no API key needed.
""")
with gr.Row():
with gr.Column(scale=2):
prompt = gr.Textbox(
label="Prompt",
placeholder="A cute cat wearing sunglasses, cyberpunk style...",
lines=3
)
with gr.Row():
width = gr.Slider(512, 1280, value=1024, step=64, label="Width")
height = gr.Slider(512, 1280, value=1024, step=64, label="Height")
seed = gr.Number(value=-1, label="Seed (-1 = random)", precision=0)
generate_btn = gr.Button("Generate Image", variant="primary")
with gr.Column(scale=3):
output_image = gr.Image(label="Generated Image", type="pil")
status = gr.Textbox(label="Status", interactive=False)
generate_btn.click(
fn=generate_image,
inputs=[prompt, width, height, seed],
outputs=[output_image, status]
)
prompt.submit(
fn=generate_image,
inputs=[prompt, width, height, seed],
outputs=[output_image, status]
)
if __name__ == "__main__":
demo.launch(ssr_mode=False) |