anasfik / replicate

A community-maintained Dart client package for Replicate.com, this package let you interact with Replicate.com APIs and create predictions from the available machine learning models.
https://replicate.com/
MIT License
10 stars 10 forks source link

Error while creating prediction #4

Open Borys520 opened 1 year ago

Borys520 commented 1 year ago

Hi, Thanks for the wonderful package! I am trying to create prediction but it always throws an error :

[Replicate] request started from/to: https://api.replicate.com/v1/predictions. [Replicate] request from/to: https://api.replicate.com/v1/predictions end. [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>'

0 new Prediction.fromJson prediction.dart:75

1 ReplicatePrediction.create. predictions.dart:62

2 ReplicateHttpClient.post http_client.dart:54

3 _MyHomePageState.build. main.dart:61

image

ZYLIM0702 commented 1 year ago

Hi, do you get the solution? Can you share with me? Thanks

DavideSantoro commented 1 year ago

I got the same error. Did you find a solution?

alesspalma commented 1 year ago

same error

ZYLIM0702 commented 1 year ago

do you guys get the solution ? I still facing the same error

DavideSantoro commented 1 year ago
  Replicate.apiKey = dotenv.env['API_KEY']!;

  try {
    Prediction prediction = await Replicate.instance.predictions.create(
      version:
          "b96a2f33cc8e4b0aa23eacfce731b9c41a7d9466d9ed4e167375587b54db9423",
      input: {
        "prompt": "What is shown in the image?",
        "image":
            "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg"
      },
    );
  } catch (e) {
    //nothing
  }
  await Future.delayed(Duration(seconds: 7));
  PaginatedPredictions predictionsPageList =
      await Replicate.instance.predictions.list();

  Prediction prediction = await Replicate.instance.predictions.get(
    id: predictionsPageList.results.elementAt(0).id,
  );
  print(prediction.output);

This is the code I used. You need to install the dotenv plugin and to put your Replicate API key in a .env file

ZYLIM0702 commented 1 year ago

Hi, if I don't use dotenv, then will it still be working ? And this is my code, can you provide me ways to debug my code ? IMG_20230802_001229.png

bigjjjim commented 1 year ago

await Future.delayed(Duration(seconds: 7)); PaginatedPredictions predictionsPageList = await Replicate.instance.predictions.list();

Prediction prediction = await Replicate.instance.predictions.get( id: predictionsPageList.results.elementAt(0).id, ); print(prediction.output);

Yes this way it works, but the object prediction does return null right after creating it. Even after 20 seconds, the object created by Prediction prediction = await Replicate.instance.predictions.create() returns null making it impossible to check with a stream or in a while loop... What if the model takes more than the 7 seconds?

I suppose we could fetch the id from the list like you do then check the status but then my question is:

Will the prediction be added to the list even thought it is not fully completed? And will it always take less than 7 seconds to be added to this list?

Thank you for the code by the way, was really confused by this null prediction!

Edit** Actualyy, if we cannot fetch the id of the first created object directly how can we ensure we have the right prediction, given that 2 users may create a prediction at the same time!