replicate / replicate-python

Python client for Replicate
https://replicate.com
Apache License 2.0
759 stars 219 forks source link

Cannot trainings.create with API due to JSON file that is not properly formatted #337

Closed mckunkel closed 1 month ago

mckunkel commented 2 months ago

Greetings,

In this code

import replicate
from dotenv import load_dotenv
import os
load_dotenv()

def get_model(space:str, model:str)->replicate:
    model = replicate.models.get(f"{space}/{model}")
    return model

def train_model(model:replicate, zip_path:str):
  training = replicate.trainings.create(
    version="ostris/flux-dev-lora-trainer:1296f0ab2d695af5a1b5eeee6e8ec043145bef33f1675ce1a2cdb0f81ec43f02",
    input={
        "input_images": open(zip_path, "rb"),
        "steps": 1000,
        "trigger_word": "TOK",
    },
    destination=f"{model.owner}/{model.name}"
  )
  print(training)
  #print(f"Training started: {training.status}")
  #print(f"Training URL: https://replicate.com/p/{training.id}")

if __name__ == "__main__":
    model = get_model("mckunkel","mk")
    zip_path = os.path.expanduser('train.zip')
    train_model(model,zip_path)

I get this error

python train.py
gTraceback (most recent call last):
  File "/Users/Mike/Documents/GitHub/replicate/train.py", line 41, in <module>
    train_model(model,zip_path)
  File "/Users/Mike/Documents/GitHub/replicate/train.py", line 20, in train_model
    training = replicate.trainings.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mike/Documents/GitHub/replicate/venv/lib/python3.12/site-packages/replicate/training.py", line 287, in create
    return _json_to_training(self._client, resp.json())
                                           ^^^^^^^^^^^
  File "/Users/Mike/Documents/GitHub/replicate/venv/lib/python3.12/site-packages/httpx/_models.py", line 764, in json
    return jsonlib.loads(self.content, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mike/.pyenv/versions/3.12.2/lib/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mike/.pyenv/versions/3.12.2/lib/python3.12/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mike/.pyenv/versions/3.12.2/lib/python3.12/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 2818051 (char 2818050)

Any ideas how I might get past this? Thanks

mattt commented 2 months ago

Hi @mckunkel. Do you see any trainings created on your dashboard on replicate.com? If so, what do you see in the logs there?

mckunkel commented 2 months ago

Greetings, On the dashboard, I see the training failed with no logs to indicate why.

mattt commented 2 months ago

Hi @mckunkel. It could be a problem with how the input images are passed as base64-encoded data URLs. I just released a new version yesterday, 0.32.0 that supports our new files API, which is more robust. Could you please try again using that?

mattt commented 1 month ago

Closing this for now. Please let me know if you're still having this problem, and I'd be happy to take another look.