### Install Replicate Elixir Client Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Adds the Replicate Elixir client dependency to your project's `mix.exs` file. This is the standard way to include external libraries in Elixir projects. ```elixir def deps do [ {:replicate, "~> 1.3.0"} ] end ``` -------------------------------- ### Create Prediction from Deployment Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Explains how to create a prediction using a Replicate deployment. Deployments offer a stable, private API endpoint for a specific model configuration. The example demonstrates fetching a deployment and then creating a new prediction against it with specified inputs. ```elixir iex> {:ok, deployment} = Replicate.Deployments.get("test/model") iex> {:ok, prediction} = Replicate.Deployments.create_prediction(deployment, %{prompt: "a 19th century portrait of a wombat gentleman"}) ``` -------------------------------- ### List Model Versions Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Retrieves all available versions for a specific Replicate model. This allows developers to select a particular version for predictions. The example shows how to get the model first, then list its versions, and finally extract just the version IDs. ```elixir iex> model = Replicate.Models.get!("stability-ai/stable-diffusion") iex> versions = Replicate.Models.list_versions(model) iex> Replicate.Models.list_versions(model) |> Enum.map(& &1.id) |> Enum.slice(0..5) ["db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", "328bd9692d29d6781034e3acab8cf3fcb122161e6f5afb896a4ca9fd57090577", "f178fa7a1ae43a9a9af01b833b9d2ecf97b1bcb0acfd2dc5dd04895e042863f1", "0827b64897df7b6e8c04625167bbb275b9db0f14ab09e2454b9824141963c966", "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", "8abccf52e7cba9f6e82317253f4a3549082e966db5584e92c808ece132037776"] ``` -------------------------------- ### List Models Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Get a paginated list of all public models. ```APIDOC ## List models Get a paginated list of all public models. ### Method GET ### Endpoint /models ### Parameters #### Query Parameters - **page** (integer) - Optional - The page number to retrieve. - **page_size** (integer) - Optional - The number of results per page. ### Request Example ```elixir iex> %{next: next, previous: previous, results: results} = Replicate.Models.list() ``` ### Response #### Success Response (200) Returns a paginated list of Model objects. #### Response Example ```json { "next": "https://api.replicate.com/v1/trainings?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw", "previous": null, "results": [ { "url": "https://replicate.com/replicate/hello-world", "owner": "replicate", "name": "hello-world", "description": "A tiny model that says hello", "visibility": "public", "github_url": "https://github.com/replicate/cog-examples", "paper_url": null, "license_url": null, "run_count": 12345, "cover_image_url": null, "default_example": null, "latest_version": { "cog_version": "0.3.0", "created_at": "2022-03-21T13:01:04.418669Z", "id": "v2", "openapi_schema": {} } } ] } ``` ``` -------------------------------- ### Download Replicate Prediction Output Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Demonstrates how to download the output file from a Replicate prediction. It fetches the prediction output URL, makes an HTTP GET request to download the binary content, and then saves it to a local file. This method does not require any external dependencies beyond standard Elixir libraries. ```elixir iex> [url | _rest] = Replicate.run("stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", prompt: "a watercolor of the babadook by Picasso") iex> url "https://replicate.delivery/pbxt/LgZ5BLmMWzqODp4rzSERXDNglStBTltHpj0533i9385qSgQE/out-0.png" iex> {:ok, resp} = :httpc.request(:get, {url, []}, [], [body_format: :binary]) iex> {{_, 200, 'OK'}, _headers, body} = resp iex> File.write!("babadook_watercolor.jpg", body) ``` -------------------------------- ### Paginate Replicate API Results Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Illustrates how to paginate through results from a Replicate API endpoint using the `paginate` function. This function takes an endpoint function (like `Replicate.Models.list/0`) and returns a stream of results, making it easy to process large datasets in batches. The example shows how to get the first batch and inspect its contents. ```elixir iex> stream = Replicate.paginate(&Replicate.Models.list/0) iex> first_batch = stream |> Enum.at(0) iex> first_batch |> length() 25 iex> %Replicate.Models.Model{name: name} = first_batch |> Enum.at(0) iex> name "hello-world" ``` -------------------------------- ### Run Model with Webhook Notification in Elixir Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md An example of initiating a prediction that will trigger a webhook notification upon completion, instead of waiting for the process to finish. ```elixir Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"}, "https://example.com/webhook", ["completed"]) ``` -------------------------------- ### Run Model with Local File Input in Elixir Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Shows how to prepare local file content (binary for audio) by reading it, base64 encoding it, and creating a data URI for use as an input to a Replicate model. ```elixir iex> binary_content = File.read!("./audio.wav") <<79, 103, 103, 83, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, ...>> iex> base64_content = Base.encode64(binary_content) ... iex> data_uri = "data:audio/wav;base64," <> base64_content ... iex> Replicate.run( ...> "vaibhavs10/incredibly-fast-whisper:3ab86df6c8f54c11309d4d1f930ac292bad43ace52d10c80d87eb258b3c9f79c", ...> audio: data_uri, ...> task: "transcribe", ...> language: "None", ... timestamp: "chunk", ... batch_size: 64, ... diarize: false ... ) ... ``` -------------------------------- ### Create a Replicate Model Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Shows how to create a new model on Replicate for a user or organization. You need to provide the owner's username, the desired model name, visibility settings (public or private), and the hardware SKU to use for the model. This is the first step in publishing a custom model. ```elixir iex> {:ok, model} = Replicate.Models.create( owner: "your-username", name: "my-model", visibility: "public", hardware: "gpu-a40-large" ) ``` -------------------------------- ### Create and Monitor a Prediction in Elixir Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Demonstrates how to create a prediction using a model version, check its initial status, and retrieve its logs. This involves fetching model and version details and then calling the create function. ```elixir iex> model = Replicate.Models.get!("stability-ai/stable-diffusion") iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf") iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"}) iex> prediction.status "starting" ``` ```elixir iex> {:ok, prediction} = Replicate.Predictions.get(prediction.id) iex> prediction.logs |> String.split("\n") ["Using seed: 54144", "input_shape: torch.Size([1, 77])", " 0%| | 0/50 [00:00 {:ok, prediction} = Replicate.Predictions.wait(prediction) iex> prediction.status "succeeded" iex> prediction.output ["https://replicate.delivery/pbxt/xbT582euzFwqCiupR5PVyMUFemfgZHbPyAm5kenezBS3RDQIC/out-0.png"] ``` -------------------------------- ### Run Stable Diffusion Model Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Demonstrates how to synchronously run the Stable Diffusion model using the Replicate Elixir client. It takes a model identifier and a prompt as input, returning a URL to the generated image. ```elixir iex> Replicate.run("stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", prompt: "a watercolor of the babadook by Picasso") ["https://replicate.delivery/pbxt/LgZ5BLmMWzqODp4rzSERXDNglStBTltHpj0533i9385qSgQE/out-0.png"] ``` -------------------------------- ### Create Model Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Create a new model for a user or organization. ```APIDOC ## Create a model You can create a model for a user or organization with a given name, visibility, and hardware SKU: ### Method POST ### Endpoint /models ### Parameters #### Request Body - **owner** (string) - Required - The username or organization name. - **name** (string) - Required - The name of the model. - **visibility** (string) - Required - "public" or "private". - **hardware** (string) - Required - The hardware SKU (e.g., "gpu-a40-large"). ### Request Example ```elixir iex> {:ok, model} = Replicate.Models.create(owner: "your-username", name: "my-model", visibility: "public", hardware: "gpu-a40-large") ``` ### Response #### Success Response (200) Returns the created Model object. ``` -------------------------------- ### Load Output Files Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Loads output files from a prediction using their URLs. ```APIDOC ## Load output files Output files are returned as HTTPS URLs. Here's one way to load files without any dependencies: ### Method GET ### Endpoint [Output URL from prediction] ### Parameters None ### Request Example ```elixir iex> [url | _rest] = Replicate.run("stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", prompt: "a watercolor of the babadook by Picasso") iex> url "https://replicate.delivery/pbxt/LgZ5BLmMWzqODp4rzSERXDNglStBTltHpj0533i9385qSgQE/out-0.png" iex> {:ok, resp} = :httpc.request(:get, {url, []}, [], [body_format: :binary]) iex> {{_, 200, 'OK'}, _headers, body} = resp iex> File.write!("babadook_watercolor.jpg", body) ``` ### Response #### Success Response (200) Returns the binary content of the file. #### Response Example [Binary file content] ``` -------------------------------- ### List Public Replicate Models Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Fetches a paginated list of all public models available on Replicate. The response includes pagination information (next and previous cursors) and a list of Model structs. Each struct contains details like the model's URL, owner, name, description, and latest version information. ```elixir iex> %{next: next, previous: previous, results: results} = Replicate.Models.list() iex> next "https://api.replicate.com/v1/trainings?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw" iex> previous nil iex> results |> length() 25 iex> results |> Enum.at(0) %Replicate.Models.Model{ url: "https://replicate.com/replicate/hello-world", owner: "replicate", name: "hello-world", description: "A tiny model that says hello", visibility: "public", github_url: "https://github.com/replicate/cog-examples", paper_url: nil, license_url: nil, run_count: 12345, cover_image_url: nil, default_example: nil, latest_version: %{ "cog_version" => "0.3.0", "created_at" => "2022-03-21T13:01:04.418669Z", "id" => "v2", "openapi_schema" => %{} } } ``` -------------------------------- ### List Model Versions Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Retrieve a list of all versions for a given model. ```APIDOC ## List versions of a model You can list all the versions of a model: ### Method GET ### Endpoint /models/{owner}/{name}/versions ### Parameters #### Path Parameters - **owner** (string) - Required - The owner of the model. - **name** (string) - Required - The name of the model. ### Request Example ```elixir iex> model = Replicate.Models.get!("stability-ai/stable-diffusion") iex> versions = Replicate.Models.list_versions(model) ``` ### Response #### Success Response (200) Returns a list of version objects for the model. #### Response Example ```elixir ["db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", "328bd9692d29d6781034e3acab8cf3fcb122161e6f5afb896a4ca9fd57090577", "f178fa7a1ae43a9a9af01b833b9d2ecf97b1bcb0acfd2dc5dd04895e042863f1", "0827b64897df7b6e8c04625167bbb275b9db0f14ab09e2454b9824141963c966", "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", "8abccf52e7cba9f6e82317253f4a3549082e966db5584e92c808ece132037776"] ``` ``` -------------------------------- ### List Replicate Predictions Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Fetches a list of all predictions run by the user. The output is a list of Prediction structs, each containing details like ID, status, input, version, and output URLs. This is useful for monitoring ongoing or completed prediction jobs. ```elixir iex> Replicate.Predictions.list() [%Prediction{ id: "1234", status: "starting", input: %{"prompt" => "a 19th century portrait of a wombat gentleman"}, version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", output: ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"] }, %Prediction{ id: "1235", status: "starting", input: %{"prompt" => "a 19th century portrait of a wombat gentleman"}, version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", output: ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"]}] ``` -------------------------------- ### Create Prediction from Deployment Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Create a prediction using a specific deployment of a model. ```APIDOC ## Create prediction from deployment Deployments allow you to control the configuration of a model with a private, fixed API endpoint. You can control the version of the model, the hardware it runs on, and how it scales. Once you create a deployment on Replicate, you can make predictions like this: ### Method POST ### Endpoint /deployments/{deployment_name}/predictions ### Parameters #### Path Parameters - **deployment_name** (string) - Required - The name of the deployment. #### Request Body - **input** (object) - Required - Input parameters for the model prediction. ### Request Example ```elixir iex> {:ok, deployment} = Replicate.Deployments.get("test/model") iex> {:ok, prediction} = Replicate.Deployments.create_prediction(deployment, %{prompt: "a 19th century portrait of a wombat gentleman"}) ``` ### Response #### Success Response (200) Returns the created Prediction object. ``` -------------------------------- ### List Predictions Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Retrieve a list of all predictions that have been run. ```APIDOC ## List Predictions You can list all the predictions you've run: ### Method GET ### Endpoint /predictions ### Parameters None ### Request Example ```elixir iex> Replicate.Predictions.list() ``` ### Response #### Success Response (200) Returns a list of Prediction objects. #### Response Example ```json [ { "id": "1234", "status": "starting", "input": {"prompt": "a 19th century portrait of a wombat gentleman"}, "version": "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", "output": ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"] }, { "id": "1235", "status": "starting", "input": {"prompt": "a 19th century portrait of a wombat gentleman"}, "version": "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", "output": ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"] } ] ``` ``` -------------------------------- ### Configure Replicate API Token in Elixir Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Configures the Replicate client in Elixir by reading the API token from environment variables. This ensures the token is not exposed in the codebase. ```elixir config :replicate, replicate_api_token: System.get_env("REPLICATE_API_TOKEN") ``` -------------------------------- ### Set Replicate API Token Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Sets the Replicate API token as an environment variable. This is a security best practice to avoid hardcoding sensitive credentials directly in the source code. ```bash export REPLICATE_API_TOKEN= ``` -------------------------------- ### Paginate Results Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Paginate through results provided by an endpoint function, returning a stream of results. ```APIDOC ## Paginate Paginates through results provided by the `endpoint_func` function. Returns a stream of results. ### Method Depends on the `endpoint_func` ### Endpoint Depends on the `endpoint_func` ### Parameters - **endpoint_func** (function) - Required - The function that fetches results (e.g., `&Replicate.Models.list/0`). ### Request Example ```elixir iex> stream = Replicate.paginate(&Replicate.Models.list/0) iex> first_batch = stream |> Enum.at(0) iex> first_batch |> length() 25 iex> %Replicate.Models.Model{name: name} = first_batch |> Enum.at(0) iex> name "hello-world" ``` ### Response #### Success Response (200) Returns a stream of results from the paginated endpoint. ``` -------------------------------- ### Cancel a Prediction by ID in Elixir Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Demonstrates how to cancel a running prediction by providing its unique identifier to the `Replicate.Predictions.cancel/1` function, and verifying the status change. ```elixir iex> model = Replicate.Models.get!("stability-ai/stable-diffusion") iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf") iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "Watercolor painting of the Babadook"}) iex> prediction.status "starting" iex> {:ok, prediction} = Replicate.Predictions.cancel(prediction.id) iex> prediction.status "canceled" ``` -------------------------------- ### Cancel a Prediction Object in Elixir Source: https://github.com/cbh123/replicate-elixir/blob/main/README.md Illustrates canceling a prediction by passing the prediction object itself to the `Replicate.Predictions.cancel/1` function and confirming the cancellation. ```elixir iex> model = Replicate.Models.get!("stability-ai/stable-diffusion") iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf") iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"}) iex> {:ok, prediction} = Replicate.Predictions.cancel(prediction) iex> prediction.status "canceled" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.