Creating a Replicate Action with more options

Evening

I am trying to get my action working - it is 98% there, but get a 401 error. When I ask chatgpt about the error, it says it has to do with my API Key - which is current.

I need to be able to send the aspect ratio as well as the style type with the prompt

Any ideas to get this working would be appreciated.

Here is my code

import os
import requests

def image_generation_with_ideogram(prompt: str, aspect_ratio: str, style_type: str):
“”"
Triggers image generation using the Ideogram model on Replicate based on user inputs.

Args:
    prompt (string): The text prompt to generate the image.
    aspect_ratio (string): The desired aspect ratio for the image
    style_type (string): The style in which the image should be generated
Envs:
    MODEL (string): The model to use for image generation.
    REPLICATE_API_KEY (string): Your API key for accessing Replicate services
"""

# Insert your PYTHON code below. You can access environment variables using os.environ[].
# Currently, only the requests library is supported, but more libraries will be available soon.
# Use print statements or return values to display results to the user.
# If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
# You do not have to call this function as the bot will automatically call and fill in the parameters.

# Insert your code below. You can access environment variables using os.environ[].
# Currently, only the requests library is supported, but more libraries will be available soon.

# Your Replicate API endpoint
url = "https://api.replicate.com/v1/predictions"

# Construct the headers for authentication
headers = {
    "Authorization": f"Token {os.environ['REPLICATE_API_KEY']}",
    "Content-Type": "application/json"
}

# Payload to send with the request
payload = {
    "version": os.environ["MODEL"],
    "input": {
        "prompt": prompt,
        "aspect_ratio": aspect_ratio,
        "style_type": style_type
        
    }
}

# Log payload to verify it
print("Payload being sent:", payload)

# Sending the POST request to the Replicate API
try:
    response = requests.post(url, headers=headers, json=payload, timeout=10)
    
    # Log status code and response content
    print(f"Status code: {response.status_code}")
    print(f"Response content: {response.text}")

    # Check if the request was successful
    if response.status_code == 201:
        # Capture response data
        response_data = response.json()
        print("Image generated successfully!")
        # Return the generated image URL or any other relevant info
        image_url = response_data.get("output", {}).get("image_url", "No URL returned")
        print("Generated Image URL:", image_url)
    else:
        print(f"Failed to generate image. Status code: {response.status_code}, Response: {response.text}")

except requests.exceptions.Timeout:
    print("The request timed out")
except requests.exceptions.RequestException as e:
    # Handle any request exceptions
    print(f"An error occurred: {e}")
1 Like

Any help would be appreciated.

Hello, Pickaxe Team? I am using Replicate in a project I want to move to Pickaxe, and without some of these controls, it limits the utility of the action. In my case, I don’t mind 1:1, althoug more rectangular would be better…I am more interested to control the pixel size of the output.
Could you please answer this gentleman?

Did you get this working?

Yes, I did.

The action has been updated, so all good now.

Could you please help me out of my ignorance?
I click on the Replicate Action and I only see Trigger, API KEY and Model.
Is yours a private model? Is it for sale or rent? Would you consider or have you donated it back to the project?

You have to clone the action, then modify to suit your requirements based upon the replicate API.

Did the base action work for you? When I try to call it to generate images from a Form, it’s throwing an error.

I havent had any issues - but I am not using a form to do this, but I don’t think that would make a difference.