Skip to main content

Python client library for Pollinations.AI API

Project description

PollinationsAPI Python Client

A developer-friendly Python SDK for interacting with the Pollinations API, supporting multimodal AI features: image generation, text generation (including OpenAI-compatible chat), vision (image analysis), speech-to-text, and text-to-speech.


Features

  • Image Generation: Create images from text prompts with multiple model options.
  • Text Generation: Generate or complete text, including OpenAI-compatible chat and streaming.
  • Vision (Image Analysis): Describe or answer questions about images (from URL or local files).
  • Speech-to-Text: Transcribe audio files to text.
  • Text-to-Speech: Synthesize speech from text, save as MP3.
  • Model Listing: List available image, text, and voice models.
  • Real-time Feeds: Subscribe to live image/text generation events via SSE.

Installation

You can install through PIP:

pip install pollinations-api
pip install requests
pip install sseclient-py    # For streaming (SSE) support

Quick Start

from pollinations_api import PollinationsAPI, PollinationsAPIError

api = PollinationsAPI(referrer="my-app")  # Optionally add a backend Bearer token

# Image Generation
try:
    img_bytes = api.generate_image("A beautiful sunset over ocean", width=512, height=512, nologo=True, save_to="sunset.jpg")
    print("Image saved to sunset.jpg")
except PollinationsAPIError as e:
    print(e)

# Text Generation
try:
    text = api.generate_text("Explain quantum computing simply", model="openai")
    print("Text generation result:", text)
except PollinationsAPIError as e:
    print(e)

# OpenAI-Compatible Chat (non-streaming)
try:
    response = api.openai_chat_completion(
        model="openai",
        messages=[
            {"role": "system", "content": "You are an assistant."},
            {"role": "user", "content": "Who won the world cup in 2018?"}
        ]
    )
    print("Chat completion:", response['choices'][0]['message']['content'])
except PollinationsAPIError as e:
    print(e)

Usage Examples

1. Image Generation

api.generate_image(
    prompt="A cyberpunk cityscape at night",
    width=768,
    height=512,
    nologo=True,
    model="flux",
    save_to="cityscape.png"
)

2. Text Generation

  • Simple completion:

    result = api.generate_text("Write a haiku about AI")
    print(result)
    
  • Streaming responses:

    for chunk in api.generate_text("Tell a story about a robot", stream=True):
        print(chunk, end="", flush=True)
    

3. OpenAI Chat Completion

response = api.openai_chat_completion(
    model="openai",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Summarize the plot of Inception."}
    ]
)
print(response['choices'][0]['message']['content'])
  • Streaming chat:

    for chunk in api.openai_chat_completion(
        model="openai",
        messages=[...],
        stream=True
    ):
        print(chunk)
    

4. Vision (Image Analysis)

  • Analyze an image URL:

    result = api.analyze_image_url(
        image_url="https://example.com/cat.jpg",
        question="What is in this image?"
    )
    print(result)
    
  • Analyze a local image:

    result = api.analyze_local_image("local_image.png")
    print(result)
    

5. Speech-to-Text

transcript = api.transcribe_audio("sample.wav")
print("Transcription:", transcript)

6. Text-to-Speech

  • GET (short texts):

    api.tts_get("Hello world!", voice="nova", save_to="hello.mp3")
    
  • POST (longer texts):

    api.tts_post("This is a long text to synthesize.", voice="alloy", save_to="long.mp3")
    

7. List Models

print("Image models:", api.list_image_models())
print("Text models:", api.list_text_models())

8. Real-time Feeds (SSE)

  • Image feed:

    for event in api.connect_image_feed():
        print(event)
    
  • Text feed:

    for event in api.connect_text_feed():
        print(event)
    

Authentication

  • Backend Token: Pass a Bearer token to the constructor.
  • Frontend Referrer: Pass a referrer string to the constructor.

Error Handling

All API errors are raised as PollinationsAPIError. Use try-except blocks to handle errors gracefully.

Requirements

  • Python 3.7+
  • requests
  • sseclient-py (for streaming, feeds)

License

MIT


Notes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pollinations_api-0.1.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pollinations_api-0.1.3-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file pollinations_api-0.1.3.tar.gz.

File metadata

  • Download URL: pollinations_api-0.1.3.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pollinations_api-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ce83ec9ae6a461b79e660c86e962e6f25a194bf77a60872e69bbdba53fb32acb
MD5 94e5fadb6a1e85a88753e309ff532554
BLAKE2b-256 b9343eb7147680e6678c374a69c08371f4498fcb219b363ad6b0209c492a57a6

See more details on using hashes here.

File details

Details for the file pollinations_api-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pollinations_api-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 49a6c0c5edce41e0dd416d5b178bbddd91b76285f88a296b01aaff2fdb23e8c0
MD5 fceebdd13a7a5c2f829ece0beeb9c1b1
BLAKE2b-256 916a858018b8e8ab63c524428d5c4124fd2cf7ef8d318beb21d0ab6ca2e7af07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page