0
点赞
收藏
分享

微信扫一扫

Replicate

J简文 2023-06-03 阅读 75

You can use Replicate to run machine learning models in the cloud from your own code, without having to set up any servers. Our community has published hundreds of open-source models that you can run, or you can run your own models.

Run a model from Python

Learn how to run a model on Replicate from within your Python code. It could be an app, a notebook, an evaluation script, or anywhere else you want to use machine learning.

👋 Check out an interactive version of this tutorial on Google Colab.

Contents

  • Install the Python library
  • Authenticate
  • Run predictions
  • Next steps

Install the Python library

We maintain an open-source Python client for the API. Install it with pip:

pip install replicate

Authenticate

Authenticate by setting your token in an environment variable:

export REPLICATE_API_TOKEN=<paste-your-token-here>

Find your API token in your account settings.

Run predictions

You can run any public model on Replicate from your Python code. The following example runs stability-ai/stable-diffusion:

import replicate
replicate.run(
  "stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
  input={"prompt": "a 19th century portrait of a wombat gentleman"}
)

# ['https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png']

Some models, like replicate/resnet in the following example, receive images as inputs. To pass a file as an input, use a file handle or URL:

image = open("mystery.jpg", "rb")
# or...
image = "https://example.com/mystery.jpg"

replicate.run(
  "replicate/resnet:dd782a3d531b61af491d1026434392e8afb40bfb53b8af35f727e80661489767",
  input={"image": image}
)

URLs are more efficient if your file is already in the cloud somewhere, or it is a large file. Files are output as URLs.

Some models stream output as the model is running. They will return an iterator, and you can iterate over that output:

iterator = replicate.run(
  "pixray/text2image:5c347a4bfa1d4523a58ae614c2194e15f2ae682b57e3797a5bb468920aa70ebf",
  input={"prompts": ["robots talking to robots"]},
)
for image in iterator:
      print(image)

# https://...
# https://...
# https://...

Next steps

Read the full Python client documentation on GitHub.

You can also run models with the raw HTTP API. Refer to the HTTP API reference for more details.

 



举报

相关推荐

0 条评论