Official Pruna API client for synchronous and asynchronous image generation and editing
Project description
Pruna Client
Official Pruna API client for image and video generation and editing.
Installation
uv add pruna-client
Quick Start
from pruna_client import PrunaClient
from pruna_client.models import PredictionStatus
# Initialize client (uses PRUNA_API_KEY env var if api_key not provided)
client = PrunaClient(api_key="your_api_key")
# Generate image
response = client.generate_text_to_image(
model="p-image",
prompt="A beautiful sunset over a calm ocean",
sync=True,
)
# Access the generated content via generation_url
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
# Download the generated image
image_bytes = client.download_content(generation_url)
with open("generated_image.jpg", "wb") as f:
f.write(image_bytes)
Accessing Generated Content
Important: All successful generation responses contain a generation_url in response.response["generation_url"] that points to the generated content (image or video). You can download this content using the download_content() method.
from pruna_client.models import PredictionStatus
# After generating content
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
# Download the generated content
content = client.download_content(generation_url)
with open("output.jpg", "wb") as f: # or .mp4 for videos
f.write(content)
Basic Usage
General Generation
We support both batch and single generation for any model. You only need to call the generate or generate_batch method with the required parameters as shown on Pruna API Reference
from pruna_client.models import PredictionStatus
response = client.generate(
model="p-image",
input={"prompt": "A beautiful sunset over a calm ocean"},
sync=True
)
# Access the generated content via generation_url
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
content = client.download_content(generation_url)
with open("output.jpg", "wb") as f:
f.write(content)
# or batch
responses = client.generate_batch(
requests=[
{"model": "p-image", "input": {"prompt": "A sunset"}, "sync": True},
{"model": "p-image", "input": {"prompt": "A sunrise"}, "sync": True},
]
)
# Access generation URLs from batch responses
for i, response in enumerate(responses):
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
content = client.download_content(generation_url)
with open(f"output_{i}.jpg", "wb") as f:
f.write(content)
Specific Models
We support specific models for image and video generation and editing. You only need to call the generate_text_to_image, generate_image_edit, generate_text_to_video, generate_video_edit, generate_image_to_video method with the required parameters as shown on Pruna API Reference
Text to Image
from pruna_client.models import PredictionStatus
response = client.generate_text_to_image(
model="p-image",
prompt="A beautiful sunset over a calm ocean",
sync=True
)
# The response contains a generation_url with the generated content
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
# Download the generated image
image_bytes = client.download_content(generation_url)
with open("generated_image.jpg", "wb") as f:
f.write(image_bytes)
Image Editing
from pruna_client.models import PredictionStatus
response = client.generate_image_edit(
model="p-image-edit",
prompt="Edit the image to make it more beautiful",
images=["path/to/image.png"],
sync=True
)
# Access the generated content via generation_url
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
image_bytes = client.download_content(generation_url)
with open("edited_image.jpg", "wb") as f:
f.write(image_bytes)
Text to Video
from pruna_client.models import PredictionStatus
response = client.generate_text_to_video(
model="wan-t2v",
prompt="A beautiful sunset over a calm ocean",
sync=False
)
# Poll for completion and access generation_url
final_response = client.poll_status(response=response)
if final_response.status == PredictionStatus.SUCCEEDED:
generation_url = final_response.response.get("generation_url")
if generation_url:
video_bytes = client.download_content(generation_url)
with open("generated_video.mp4", "wb") as f:
f.write(video_bytes)
Async Usage
The client supports async operations for better performance when making multiple requests or integrating with async applications.
General Generation (Async)
import asyncio
from pruna_client import PrunaClient
from PIL import Image
async def main():
from pruna_client.models import PredictionStatus
client = PrunaClient(api_key="your_api_key")
response = await client.agenerate(
model="p-image",
input={"prompt": "A beautiful sunset over a calm ocean"},
sync=True
)
# Access the generated content via generation_url
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
content = client.download_content(generation_url)
with open("output.jpg", "wb") as f:
f.write(content)
await client.aclose()
asyncio.run(main())
# or batch
async def batch_example():
from pruna_client.models import PredictionStatus
client = PrunaClient(api_key="your_api_key")
responses = await client.agenerate_batch(
requests=[
{"model": "p-image", "input": {"prompt": "A beautiful sunset over a calm ocean"}, "sync": True},
{"model": "p-image", "input": {"prompt": "A beautiful sunrise over a calm ocean"}, "sync": True},
]
)
# Access generation URLs from batch responses
for i, response in enumerate(responses):
if response.status == PredictionStatus.SUCCEEDED:
generation_url = response.response.get("generation_url")
if generation_url:
content = client.download_content(generation_url)
with open(f"output_{i}.jpg", "wb") as f:
f.write(content)
await client.aclose()
asyncio.run(batch_example())
Additional methods
We support additional methods for file upload, polling status, and closing the client. You only need to call the upload_file, poll_status, close method with the required parameters as shown on Pruna API Reference
File Upload
We support string, pathlib.Path, PIL.Image.Image, and bytes as input.
url = client.upload_file("path/to/image.png")
# or batch
urls = client.upload_file_batch(["path/to/image.png", "path/to/image2.png"])
Polling Status
This method is used to poll the status of a generation request and return the final response.
response = client.poll_status(response=response)
# or status URL
response = client.poll_status(status_url=response.response["get_url"])
Closing the Client
client.close()
Running Tests
uv run pytest tests/integration/test_general.py -v
uv run pytest tests/integration/test_text_to_image.py -v
uv run pytest tests/integration/test_image_edit.py -v
uv run pytest tests/integration/test_text_to_video.py -v
uv run pytest tests/integration/test_video_edit.py -v
uv run pytest tests/integration/test_image_to_video.py -v
uv run pytest tests/integration/test_batch_generation.py -v
Tests require PRUNA_API_KEY environment variable to be set.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pruna_client-0.0.8.1.tar.gz.
File metadata
- Download URL: pruna_client-0.0.8.1.tar.gz
- Upload date:
- Size: 30.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c50aa37195e3043fe24760596e81a588d23305d53ab25f0800c409538ff87b9a
|
|
| MD5 |
d63f9f2aa5be9a70d39534dd63731d63
|
|
| BLAKE2b-256 |
0eefe4a18ddd54d373aa64a776dc18d4878ed55a337168a9a522ae26a32c7308
|
File details
Details for the file pruna_client-0.0.8.1-py3-none-any.whl.
File metadata
- Download URL: pruna_client-0.0.8.1-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d923502934fde53013980ea419372caa3c8866550478b2404a9e132dd2f77ad
|
|
| MD5 |
7aaeb83849eecd1e0363be47526cf4cc
|
|
| BLAKE2b-256 |
31494dc99df0b59e6341dd4019f018edfc01541ba7810c2fcf2f11dfde94750b
|