Skip to content

Latest commit

 

History

History
366 lines (285 loc) · 7.97 KB

File metadata and controls

366 lines (285 loc) · 7.97 KB

Segmind Python SDK Examples

This document provides usage examples for all the modules in the Segmind Python SDK.

Setup

Set your API key as an environment variable:

export SEGMIND_API_KEY="your_api_key_here"
from segmind import SegmindClient

# Initialize the client
client = SegmindClient(api_key="your_api_key_here")
# Or use environment variable SEGMIND_API_KEY
client = SegmindClient()

# Initialize with custom timeout
client = SegmindClient(timeout=120)

Basic Model Inference

Text to Image

import segmind

# Generate an image from text
response = segmind.run_sync(
    "seedream-v3-text-to-image",
    prompt="A beautiful raining sunrise over dark fiery mountains",
    aspect_ratio="16:9",
)

# Save the image
with open("sunrise.jpg", "wb") as f:
    f.write(response.content)

Music Generation

import segmind

# Generate music
response = segmind.run_sync(
    "ace-step-music",
    genres="jazz",
    output_seconds=30,
)

# Save the audio
with open("music.mp3", "wb") as f:
    f.write(response.content)

Text-to-Speech

import segmind

# Convert text to speech
response = segmind.run_sync(
    "myshell-tts",
    voice="michael",
    language="EN_NEWEST",
    text="Did you ever hear a folk tale about a giant turtle?",
    speed=1,
)

# Save the audio
with open("tts.mp3", "wb") as f:
    f.write(response.content)

LLM Chat

import segmind

# Chat with a language model. `chat` is async by default (mirrors `run`);
# `.text` is normalized across OpenAI / Anthropic / Gemini response shapes.
messages = [
    {"role": "user", "content": "tell me a joke on cats"},
    {"role": "assistant", "content": "here is a joke about cats..."},
    {"role": "user", "content": "now a joke on dogs"},
]

reply = segmind.chat("qwen2p5-vl-32b-instruct", messages=messages)
print(reply.text)
print(reply.usage, reply.finish_reason)

# Single blocking call instead of the async path:
reply = segmind.chat_sync("qwen2p5-vl-32b-instruct", prompt="tell me a joke on cats")

# Multimodal: inline a local image as a base64 data-URI
msg = {"role": "user", "content": [
    {"type": "text", "text": "Describe this image"},
    segmind.image_url("photo.jpg"),
]}
reply = segmind.chat("qwen2p5-vl-32b-instruct", messages=[msg])

Webhooks

Get All Webhooks

import segmind

webhooks = segmind.webhooks.get()
print(webhooks)

Add a Webhook

import segmind

result = segmind.webhooks.add("https://your-endpoint.com", ["PIXELFLOW"])
print(result)

Update a Webhook

import segmind

result = segmind.webhooks.update(
    webhook_id="53a5fce9-11b7-4425-91da-47bd6515a8f9",
    webhook_url="https://newurl.com",
    event_types=["PIXELFLOW"]
)
print(result)

Delete a Webhook

import segmind

result = segmind.webhooks.delete("53a5fce9-11b7-4425-91da-47bd6515a8f9")
print(result)

Get Webhook Logs

import segmind

logs = segmind.webhooks.logs("53a5fce9-11b7-4425-91da-47bd6515a8f9")
print(logs)

Models

List All Models

import segmind

models = segmind.models.list()
print(models)

Files (Segmind Storage)

Upload files to Segmind Storage and receive persistent URLs that can be reused across multiple model runs and PixelFlow workflows.

Single File Upload

import segmind

result = segmind.files.upload("path/to/image.png")
print(result)
# {'file_urls': ['https://images.segmind.com/assets/...'], 'message': 'Files uploaded successfully'}

# Access the uploaded file URL
file_url = result["file_urls"][0]
print(file_url)

Batch Upload (Multiple Files)

import segmind

result = segmind.files.upload([
    "path/to/image1.png",
    "path/to/image2.jpg",
    "path/to/image3.webp"
])

# Access individual URLs
for url in result["file_urls"]:
    print(url)

Using Uploaded Files with Models

import segmind

# Upload an image and use it with a model
upload_result = segmind.files.upload("input_image.jpg")
uploaded_url = upload_result["file_urls"][0]

# Use the URL in a model request
response = segmind.run_sync(
    "seededit-v3",
    image=uploaded_url,
    prompt="Add a sunset background"
)

Using with PixelFlows

import segmind

# Upload files for use in a workflow
upload_result = segmind.files.upload(["image1.jpg", "image2.jpg"])
image_urls = upload_result["file_urls"]

# Use uploaded URLs in PixelFlow
result = segmind.pixelflows.run(
    workflow_id="your-workflow-id",
    data={
        "input_image_1": image_urls[0],
        "input_image_2": image_urls[1]
    },
    poll=True
)

Supported Formats

  • Images: png, jpg, jpeg, gif, bmp, webp, svg, ico, tif, tiff, jfif, pjp, apng, svgz, heif, heic, xbm
  • Audio: mp3, aiff, wma, au
  • Video: mp4, avi, mov, mkv, wmv, flv, webm, mpeg, mpg

Generations

Get Recent Generations

import segmind

recent = segmind.generations.recent("seededit-v3")
print(recent)

List Generations

import segmind

# Get all generations (page defaults to 1)
all_generations = segmind.generations.list()
print(all_generations)

# Get generations with pagination
page_2 = segmind.generations.list(page=2)

# Get generations filtered by model
model_generations = segmind.generations.list(model_name="seededit-v3")

# Get generations with date range filter
filtered_generations = segmind.generations.list(
    page=1,
    model_name="seededit-v3",
    start_date="2025-07-19",
    end_date="2025-08-19"
)

PixelFlows

Run a Workflow

import segmind

# Run a workflow by ID with polling (waits for completion)
result = segmind.pixelflows.run(
    workflow_id="6839bf53659263e69c7a567a-v1",
    data={"Text_Prompt": "I am happy with this client's services"},
    poll=True
)
print("Workflow result:", result)

# Run a workflow by URL
result = segmind.pixelflows.run(
    workflow_url="https://api.segmind.com/workflows/6839bf53659263e69c7a567a-v1",
    data={"prompt": "Generate an image"},
    poll=True
)

# Submit without polling (returns immediately)
result = segmind.pixelflows.run(
    workflow_id="your_workflow_id",
    data={"input_param": "value"},
    poll=False
)
poll_id = result.get("request_id")
print(f"Request submitted with ID: {poll_id}")

# Custom polling settings
result = segmind.pixelflows.run(
    workflow_id="your_workflow_id",
    data={"input_param": "value"},
    poll=True,
    poll_interval=5,  # Poll every 5 seconds
    max_wait_time=600  # Wait up to 10 minutes
)

Get Workflow Status

import segmind

# Check status by poll ID
status = segmind.pixelflows.get_status(poll_id="4f7471d8ac431cffe0468dc487b5d354")
print(f"Current status: {status}")

# Check status by poll URL
status = segmind.pixelflows.get_status(
    poll_url="https://api.segmind.com/workflows/request/4f7471d8ac431cffe0468dc487b5d354"
)

Poll for Results

import segmind

# Poll an existing request until completion
result = segmind.pixelflows.poll(
    poll_id="4f7471d8ac431cffe0468dc487b5d354",
    poll_interval=3,
    max_wait_time=600
)
print(f"Final result: {result}")

Response Formats

# The response will have different formats based on status:

# 1. QUEUED:
# {'message': '...', 'poll_url': '...', 'request_id': '...', 'status': 'QUEUED'}

# 2. PROCESSING:
# {'output': '', 'status': 'PROCESSING'}

# 3. COMPLETED:
# {'output': [{"keyname": "Infographic", "value": {"data": "image_url", "type": "image"}}], 'status': 'COMPLETED'}

# 4. FAILED:
# {'error_message': {...}, 'status': 'FAILED'}

Advanced Usage

For custom configuration (timeout, base URL, explicit API key), use SegmindClient directly:

from segmind import SegmindClient

# Custom client with longer timeout
client = SegmindClient(api_key="your_api_key", timeout=120.0)

# Run model (sync single call)
response = client.run_sync("seedream-v3-text-to-image", prompt="A sunset")

# Access namespaces
result = client.files.upload("image.png")
result = client.pixelflows.run(workflow_id="...", data={...})
webhooks = client.webhooks.get()