Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# ------------------------------------------------------------------
|
| 7 |
+
# Helper: fetch list of available models
|
| 8 |
+
# ------------------------------------------------------------------
|
| 9 |
+
def get_models():
|
| 10 |
+
r = requests.get("https://image.pollinations.ai/models", timeout=10)
|
| 11 |
+
models = [m.strip() for m in r.text.splitlines() if m.strip()]
|
| 12 |
+
extra = ["kontext", "gptimage"]
|
| 13 |
+
for m in extra:
|
| 14 |
+
if m not in models:
|
| 15 |
+
models.append(m)
|
| 16 |
+
return models
|
| 17 |
+
|
| 18 |
+
# ------------------------------------------------------------------
|
| 19 |
+
# Image-generation function
|
| 20 |
+
# ------------------------------------------------------------------
|
| 21 |
+
def generate_image(prompt, model):
|
| 22 |
+
url = f"https://image.pollinations.ai/prompt/{prompt}?nologo=true&seed=random"
|
| 23 |
+
if model:
|
| 24 |
+
url += f"&model={model}"
|
| 25 |
+
r = requests.get(url, timeout=30)
|
| 26 |
+
img = Image.open(io.BytesIO(r.content))
|
| 27 |
+
return img
|
| 28 |
+
|
| 29 |
+
# ------------------------------------------------------------------
|
| 30 |
+
# Gradio UI
|
| 31 |
+
# ------------------------------------------------------------------
|
| 32 |
+
models = get_models()
|
| 33 |
+
|
| 34 |
+
with gr.Blocks(theme="soft") as demo:
|
| 35 |
+
gr.Markdown("# 🖼️ Pollinations Image Generator")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column(scale=3):
|
| 39 |
+
prompt_box = gr.Textbox(
|
| 40 |
+
label="Prompt",
|
| 41 |
+
placeholder="A cat wearing sunglasses on the moon...",
|
| 42 |
+
lines=2
|
| 43 |
+
)
|
| 44 |
+
model_dropdown = gr.Dropdown(
|
| 45 |
+
choices=models,
|
| 46 |
+
value=models[0],
|
| 47 |
+
label="Model"
|
| 48 |
+
)
|
| 49 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
| 50 |
+
|
| 51 |
+
with gr.Column(scale=3):
|
| 52 |
+
output_img = gr.Image(type="pil", label="Generated Image")
|
| 53 |
+
|
| 54 |
+
generate_btn.click(
|
| 55 |
+
fn=generate_image,
|
| 56 |
+
inputs=[prompt_box, model_dropdown],
|
| 57 |
+
outputs=output_img
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|