Update app.py
Browse files
app.py
CHANGED
|
@@ -3,36 +3,78 @@ 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
|
@@ -41,11 +83,31 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 41 |
placeholder="A cat wearing sunglasses on the moon...",
|
| 42 |
lines=2
|
| 43 |
)
|
|
|
|
|
|
|
| 44 |
model_dropdown = gr.Dropdown(
|
| 45 |
choices=models,
|
| 46 |
-
value=
|
| 47 |
-
label="Model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
|
|
|
| 49 |
generate_btn = gr.Button("Generate", variant="primary")
|
| 50 |
|
| 51 |
with gr.Column(scale=3):
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
# Model information dictionary - maps model names to their descriptions
|
| 7 |
+
MODEL_INFO = {
|
| 8 |
+
"None": "Default model (good for general purposes)",
|
| 9 |
+
"stability": "Stability AI's Stable Diffusion (balanced quality)",
|
| 10 |
+
"openjourney": "Midjourney-style images",
|
| 11 |
+
"dreamlike": "Dreamlike photorealistic art",
|
| 12 |
+
"protogen": "Sci-fi and futuristic styles",
|
| 13 |
+
"sd14": "Stable Diffusion 1.4 (classic model)",
|
| 14 |
+
"sd21": "Stable Diffusion 2.1 (improved details)",
|
| 15 |
+
"kontext": "Context-aware generation",
|
| 16 |
+
"gptimage": "GPT-assisted image generation",
|
| 17 |
+
"anything": "Anime-style generation",
|
| 18 |
+
"realistic": "Photorealistic outputs",
|
| 19 |
+
"analog": "Film photography style",
|
| 20 |
+
"arcanediffusion": "Fantasy art style",
|
| 21 |
+
"dreamshaper": "Enhanced creative variations",
|
| 22 |
+
"deliberate": "Precise prompt following",
|
| 23 |
+
"revanimated": "Animated/cartoon styles",
|
| 24 |
+
"meinamix": "Anime/portrait hybrid"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
# ------------------------------------------------------------------
|
| 28 |
# Helper: fetch list of available models
|
| 29 |
# ------------------------------------------------------------------
|
| 30 |
def get_models():
|
| 31 |
+
try:
|
| 32 |
+
r = requests.get("https://image.pollinations.ai/models", timeout=10)
|
| 33 |
+
models = [m.strip() for m in r.text.splitlines() if m.strip()]
|
| 34 |
+
except:
|
| 35 |
+
# Fallback if API is unavailable
|
| 36 |
+
models = list(MODEL_INFO.keys())
|
| 37 |
+
|
| 38 |
+
# Ensure our known models are included
|
| 39 |
+
for m in MODEL_INFO:
|
| 40 |
+
if m not in models and m != "None":
|
| 41 |
models.append(m)
|
| 42 |
+
|
| 43 |
+
# Sort alphabetically but keep "None" first
|
| 44 |
+
models = sorted(models)
|
| 45 |
+
if "None" in models:
|
| 46 |
+
models.remove("None")
|
| 47 |
+
models.insert(0, "None")
|
| 48 |
return models
|
| 49 |
|
| 50 |
# ------------------------------------------------------------------
|
| 51 |
# Image-generation function
|
| 52 |
# ------------------------------------------------------------------
|
| 53 |
def generate_image(prompt, model):
|
| 54 |
+
if model == "None":
|
| 55 |
+
model = "" # API expects empty string for default
|
| 56 |
+
|
| 57 |
url = f"https://image.pollinations.ai/prompt/{prompt}?nologo=true&seed=random"
|
| 58 |
if model:
|
| 59 |
url += f"&model={model}"
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
r = requests.get(url, timeout=30)
|
| 63 |
+
r.raise_for_status()
|
| 64 |
+
img = Image.open(io.BytesIO(r.content))
|
| 65 |
+
return img
|
| 66 |
+
except Exception as e:
|
| 67 |
+
raise gr.Error(f"Failed to generate image: {str(e)}")
|
| 68 |
|
| 69 |
# ------------------------------------------------------------------
|
| 70 |
+
# Gradio UI with improved model selector
|
| 71 |
# ------------------------------------------------------------------
|
| 72 |
models = get_models()
|
| 73 |
|
| 74 |
with gr.Blocks(theme="soft") as demo:
|
| 75 |
+
gr.Markdown("""# 🖼️ Pollinations Image Generator
|
| 76 |
+
### Select a model style that matches your desired output
|
| 77 |
+
""")
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
with gr.Column(scale=3):
|
|
|
|
| 83 |
placeholder="A cat wearing sunglasses on the moon...",
|
| 84 |
lines=2
|
| 85 |
)
|
| 86 |
+
|
| 87 |
+
# Improved model dropdown with descriptions
|
| 88 |
model_dropdown = gr.Dropdown(
|
| 89 |
choices=models,
|
| 90 |
+
value="None",
|
| 91 |
+
label="Model Style",
|
| 92 |
+
info="Select a generation style",
|
| 93 |
+
interactive=True,
|
| 94 |
+
filterable=True, # Allows searching through models
|
| 95 |
+
elem_classes="model-selector"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Add model description display
|
| 99 |
+
model_description = gr.Markdown(MODEL_INFO["None"])
|
| 100 |
+
|
| 101 |
+
# Update description when model changes
|
| 102 |
+
def update_description(model):
|
| 103 |
+
return MODEL_INFO.get(model, "No description available")
|
| 104 |
+
|
| 105 |
+
model_dropdown.change(
|
| 106 |
+
fn=update_description,
|
| 107 |
+
inputs=model_dropdown,
|
| 108 |
+
outputs=model_description
|
| 109 |
)
|
| 110 |
+
|
| 111 |
generate_btn = gr.Button("Generate", variant="primary")
|
| 112 |
|
| 113 |
with gr.Column(scale=3):
|