Whitepaper
Docs
Sign In
Tool
Tool
v1.0
GenAI (Pollinations)
Tool ID
image_generation
Creator
@kaneki
Downloads
309+
Generates AI Images with the free Pollinations API (including flux-pro)
Get
README
No README available
Tool Code
Show
""" title: GenAI (Pollinations) author: # email: # date: 2025-02-07 version: 1.0 license: # description: AI Image generations with SOTA models using free model from Pollinations.ai with lots of customizable options for the LLM """ import requests from requests.exceptions import RequestException from urllib.parse import quote from typing import Awaitable, Callable import asyncio class Tools: def __init__(self): pass async def create_image( self, prompt: str, image_format: str, model: str, enhance: bool, __event_emitter__: Callable[[dict], Awaitable[None]], ) -> str: """ Use an API to create images with state-of-the-art AI image generation models using natural language, ensuring that the final image clearly reflects the user's intent by optimizing the provided prompt with additional descriptive details as necessary. Args: prompt: A natural language text prompt to generate the image. It must be highly descriptive and at least around 50 to 100 words; if the user's prompt is too brief, you are required to enrich it by adding details. In doing so, describe most aspects of the image—including mood, style, colors, emotions, perspective, and media type (such as image, drawing, 3d model, painting, selfie)—using adjectives. This prompt will be fed to the image generation model so use good prompting techniques for image models like sdxl or DALLE. image_format: The desired image format (aspect ratio) specified as one of the following options: default (16 by 9, 1920×1080), square (1 by 1, 1080×1080), landscape (16 by 9, 1920×1080), landscape_large (16 by 9, 2560×1440), portrait (9 by 16, 1080×1920), portrait_large (9 by 16, 1440×2560); note that higher resolutions may increase processing time. enhance: A boolean flag indicating whether to apply additional image quality enhancement processes; use this only if the situation calls for improved quality despite the potential for increased processing time. Enhancment happens by having the prompt optimized by an additional model. model: The AI model to employ for image generation, which influences speed and quality. Choose a task-specific model if the prompt’s intent closely aligns with its specialized capabilities; otherwise, default to a general model. Options include: flux (general, good quality), flux-pro (general, best quality but slower), flux-realism (for realistically styled images without artistic embellishments), flux-anime (for anime, comics, manga, etc.), flux-3d (for intricate 3D scenes), flux-cablyai (general, good quality with Cablyai inference redirection), and turbo (general, medium to low quality with marginal speed differences from flux). seed: (Optional) A random generation seed for reproducible results. Returns: str: A message containing a Markdown-embeddable direct link to the generated image along with all the provided argument details. """ await __event_emitter__( { "data": { "description": "Image generation started", "status": "in_progress", "done": False, }, "type": "status", } ) try: # Define supported image formats formats = { "default": (1920, 1080), "square": (1080, 1080), "landscape": (1920, 1080), "landscape_large": (2560, 1440), "portrait": (1080, 1920), "portrait_large": (1440, 2560), } if image_format not in formats: raise ValueError(image_format="default") width, height = formats[image_format] url = f"https://image.pollinations.ai/prompt/{quote(prompt)}?width={width}&height={height}&model={model}&nologo=true&enhance={enhance}" asyncio.create_task(asyncio.to_thread(requests.get, url)) await __event_emitter__( { "data": { "description": "Image generation finished", "status": "complete", "done": True, }, "type": "status", } ) return f"Here's the the AI-generated image from the image tool. To display it, embed it directly in your response using this Markdown: . Do NOT use a code block. The image will render automatically. Other details contained in this link can be used only if asked for or deemed strictly relevant." except RequestException as e: error_msg = f"Network error occurred: {str(e)}" await __event_emitter__( { "data": { "description": error_msg, "status": "complete", "done": True, }, "type": "status", } ) return error_msg except Exception as e: error_msg = f"An error occurred: {str(e)}" await __event_emitter__( { "data": { "description": error_msg, "status": "complete", "done": True, }, "type": "status", } ) return error_msg