mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2025-05-06 11:59:06 +08:00
Add Pollinations.AI API extension
This commit is contained in:
parent
82a973c043
commit
6946bbf66a
38
extensions/pollinations-api/README.md
Normal file
38
extensions/pollinations-api/README.md
Normal file
@ -0,0 +1,38 @@
|
||||
# Pollinations.AI API Extension for Stable Diffusion WebUI
|
||||
|
||||
This extension integrates [Pollinations.AI](https://pollinations.ai) with the Stable Diffusion WebUI, allowing users to generate images using Pollinations.AI's free, no-signup API instead of local models. This is especially useful for users without high-end GPUs or those who want to try different models without downloading them.
|
||||
|
||||
## Features
|
||||
|
||||
- Generate images using Pollinations.AI's cloud-based API directly from the WebUI
|
||||
- No API key or signup required
|
||||
- Access to multiple image generation models
|
||||
- Adjustable parameters (width, height, seed, etc.)
|
||||
- Option to disable the Pollinations logo overlay
|
||||
|
||||
## Usage
|
||||
|
||||
1. Go to the txt2img or img2img tab
|
||||
2. Select "Pollinations API" from the Script dropdown
|
||||
3. Enter your prompt and adjust parameters as needed
|
||||
4. Click Generate
|
||||
|
||||
## Parameters
|
||||
|
||||
- **Model**: Select the Pollinations.AI model to use for generation
|
||||
- **Width**: Width of the generated image (default: 1024)
|
||||
- **Height**: Height of the generated image (default: 1024)
|
||||
- **No Logo**: Disable the Pollinations logo overlay
|
||||
- **Enhance Prompt**: Use an LLM to enhance the prompt with more details
|
||||
|
||||
## About Pollinations.AI
|
||||
|
||||
Pollinations.AI is a free and open-source generative AI platform that provides:
|
||||
|
||||
- Free access to text, image, and audio generation APIs
|
||||
- No signup or API key required
|
||||
- Simple URL-based endpoints for easy integration
|
||||
- OpenAI-compatible interfaces
|
||||
- HTTPS and CORS support
|
||||
|
||||
Visit [Pollinations.AI](https://pollinations.ai) for more information.
|
6
extensions/pollinations-api/__init__.py
Normal file
6
extensions/pollinations-api/__init__.py
Normal file
@ -0,0 +1,6 @@
|
||||
from modules import script_callbacks, shared
|
||||
|
||||
def on_app_started(demo, app):
|
||||
print("Pollinations.AI API extension loaded!")
|
||||
|
||||
script_callbacks.on_app_started(on_app_started)
|
25
extensions/pollinations-api/install.py
Normal file
25
extensions/pollinations-api/install.py
Normal file
@ -0,0 +1,25 @@
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import importlib.util
|
||||
|
||||
# Check if required packages are installed
|
||||
required_packages = ['requests', 'urllib3']
|
||||
|
||||
def is_package_installed(package_name):
|
||||
return importlib.util.find_spec(package_name) is not None
|
||||
|
||||
def install_package(package_name):
|
||||
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name])
|
||||
|
||||
def install_requirements():
|
||||
for package in required_packages:
|
||||
if not is_package_installed(package):
|
||||
print(f"Installing {package}...")
|
||||
install_package(package)
|
||||
print(f"{package} installed successfully.")
|
||||
else:
|
||||
print(f"{package} is already installed.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
install_requirements()
|
146
extensions/pollinations-api/scripts/pollinations_api.py
Normal file
146
extensions/pollinations-api/scripts/pollinations_api.py
Normal file
@ -0,0 +1,146 @@
|
||||
import os
|
||||
import io
|
||||
import json
|
||||
import requests
|
||||
import urllib.parse
|
||||
from PIL import Image
|
||||
from modules import scripts, shared, images
|
||||
from modules.processing import process_images, Processed
|
||||
from modules.shared import opts, cmd_opts, state
|
||||
import gradio as gr
|
||||
|
||||
class PollinationsAPI(scripts.Script):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.models = []
|
||||
self.fetch_models()
|
||||
|
||||
def fetch_models(self):
|
||||
try:
|
||||
response = requests.get("https://image.pollinations.ai/models")
|
||||
if response.status_code == 200:
|
||||
self.models = response.json()
|
||||
else:
|
||||
print(f"Failed to fetch models: {response.status_code}")
|
||||
self.models = ["flux", "sdxl", "pixart", "dalle"] # Default models
|
||||
except Exception as e:
|
||||
print(f"Error fetching models: {e}")
|
||||
self.models = ["flux", "sdxl", "pixart", "dalle"] # Default models
|
||||
|
||||
def title(self):
|
||||
return "Pollinations API"
|
||||
|
||||
def show(self, is_img2img):
|
||||
return scripts.AlwaysVisible
|
||||
|
||||
def ui(self, is_img2img):
|
||||
with gr.Group():
|
||||
with gr.Accordion("Pollinations.AI API", open=False):
|
||||
enabled = gr.Checkbox(label="Enable Pollinations.AI API", value=False)
|
||||
model = gr.Dropdown(label="Model", choices=self.models, value=self.models[0] if self.models else "flux")
|
||||
width = gr.Slider(minimum=256, maximum=2048, step=64, label="Width", value=1024)
|
||||
height = gr.Slider(minimum=256, maximum=2048, step=64, label="Height", value=1024)
|
||||
no_logo = gr.Checkbox(label="No Logo", value=False)
|
||||
enhance = gr.Checkbox(label="Enhance Prompt", value=False)
|
||||
private = gr.Checkbox(label="Private (Don't show in public feed)", value=True)
|
||||
info = gr.HTML("<p>This extension uses <a href='https://pollinations.ai' target='_blank'>Pollinations.AI</a>'s free, no-signup API to generate images.</p>")
|
||||
|
||||
return [enabled, model, width, height, no_logo, enhance, private]
|
||||
|
||||
def process(
|
||||
self,
|
||||
p,
|
||||
enabled,
|
||||
model,
|
||||
width,
|
||||
height,
|
||||
no_logo,
|
||||
enhance,
|
||||
private,
|
||||
):
|
||||
if not enabled:
|
||||
return p
|
||||
|
||||
p.do_not_save_grid = True
|
||||
p.do_not_save_samples = True
|
||||
|
||||
# Store original parameters
|
||||
self.original_prompt = p.prompt
|
||||
self.original_negative_prompt = p.negative_prompt
|
||||
self.original_seed = p.seed
|
||||
self.original_width = p.width
|
||||
self.original_height = p.height
|
||||
|
||||
# Override parameters
|
||||
p.width = width
|
||||
p.height = height
|
||||
|
||||
return p
|
||||
|
||||
def postprocess(self, p, processed, enabled, model, width, height, no_logo, enhance, private):
|
||||
if not enabled:
|
||||
return processed
|
||||
|
||||
# Restore original parameters for metadata
|
||||
p.prompt = self.original_prompt
|
||||
p.negative_prompt = self.original_negative_prompt
|
||||
p.seed = self.original_seed
|
||||
p.width = self.original_width
|
||||
p.height = self.original_height
|
||||
|
||||
# Create a new Processed object
|
||||
result = Processed(p, [])
|
||||
|
||||
# Generate images using Pollinations API
|
||||
for i in range(p.batch_size * p.n_iter):
|
||||
try:
|
||||
# Prepare parameters
|
||||
params = {
|
||||
"model": model,
|
||||
"width": width,
|
||||
"height": height,
|
||||
"seed": p.seed + i if p.seed != -1 else None,
|
||||
"nologo": "true" if no_logo else "false",
|
||||
"enhance": "true" if enhance else "false",
|
||||
"private": "true" if private else "false",
|
||||
"referrer": "stable-diffusion-webui-extension"
|
||||
}
|
||||
|
||||
# Prepare prompt
|
||||
prompt = p.prompt
|
||||
if p.negative_prompt:
|
||||
prompt += " | negative: " + p.negative_prompt
|
||||
|
||||
# Encode prompt
|
||||
encoded_prompt = urllib.parse.quote(prompt)
|
||||
|
||||
# Build URL
|
||||
url = f"https://image.pollinations.ai/prompt/{encoded_prompt}"
|
||||
|
||||
# Make request
|
||||
response = requests.get(url, params=params, timeout=60)
|
||||
response.raise_for_status()
|
||||
|
||||
# Convert response to image
|
||||
image = Image.open(io.BytesIO(response.content))
|
||||
result.images.append(image)
|
||||
|
||||
# Add info to image
|
||||
images.save_image(
|
||||
image,
|
||||
p.outpath_samples,
|
||||
"",
|
||||
p.seed + i if p.seed != -1 else -1,
|
||||
p.prompt,
|
||||
opts.samples_format,
|
||||
info=f"Pollinations.AI API, Model: {model}",
|
||||
p=p,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error generating image with Pollinations API: {e}")
|
||||
# Add a blank image or error message image
|
||||
error_image = Image.new('RGB', (width, height), color=(0, 0, 0))
|
||||
result.images.append(error_image)
|
||||
|
||||
return result
|
Loading…
x
Reference in New Issue
Block a user