github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/dashboard/api/pkg/server/client.go (about)

     1  package server
     2  
     3  // client code for accessing bacalhau, literally ripped straight from the CLI
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/filecoin-project/bacalhau/pkg/job"
    11  	"github.com/filecoin-project/bacalhau/pkg/model"
    12  	"github.com/filecoin-project/bacalhau/pkg/requester/publicapi"
    13  	"github.com/filecoin-project/bacalhau/pkg/system"
    14  )
    15  
    16  const maxWaitTime = 900
    17  
    18  func init() { //nolint:gochecknoinits
    19  	err := system.InitConfig()
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  }
    24  
    25  var realSpec model.Spec = model.Spec{
    26  	Engine:    model.EngineDocker,
    27  	Verifier:  model.VerifierNoop,
    28  	Publisher: model.PublisherIpfs,
    29  	Docker: model.JobSpecDocker{
    30  		Image:      "ghcr.io/bacalhau-project/examples/stable-diffusion-gpu:0.0.1",
    31  		Entrypoint: []string{"python", "main.py", "--o", "./outputs", "--p"},
    32  	},
    33  	Resources: model.ResourceUsageConfig{
    34  		GPU: "1",
    35  	},
    36  	Outputs: []model.StorageSpec{
    37  		{
    38  			Name: "outputs",
    39  			Path: "/outputs",
    40  		},
    41  	},
    42  	Deal: model.Deal{
    43  		Concurrency: 1,
    44  	},
    45  }
    46  
    47  var testSpec model.Spec = model.Spec{
    48  	Engine:    model.EngineDocker,
    49  	Verifier:  model.VerifierNoop,
    50  	Publisher: model.PublisherIpfs,
    51  	Docker: model.JobSpecDocker{
    52  		Image:      "ubuntu",
    53  		Entrypoint: []string{"echo"},
    54  	},
    55  	Outputs: []model.StorageSpec{
    56  		{
    57  			Name: "outputs",
    58  			Path: "/outputs",
    59  		},
    60  	},
    61  	Deal: model.Deal{
    62  		Concurrency: 1,
    63  	},
    64  }
    65  
    66  func runGenericJob(s model.Spec) (string, error) {
    67  	j, err := model.NewJobWithSaneProductionDefaults()
    68  	if err != nil {
    69  		return err.Error(), err
    70  	}
    71  	j.Spec = s
    72  
    73  	env := system.EnvironmentProd
    74  	baseURI := fmt.Sprintf("http://%s:%d", system.Envs[env].APIHost, system.Envs[env].APIPort)
    75  	client := publicapi.NewRequesterAPIClient(baseURI)
    76  
    77  	submittedJob, err := client.Submit(context.Background(), j)
    78  	if err != nil {
    79  		return err.Error(), err
    80  	}
    81  
    82  	resolver := client.GetJobStateResolver()
    83  	resolver.SetWaitTime(maxWaitTime, time.Second)
    84  
    85  	err = resolver.Wait(context.Background(), submittedJob.Metadata.ID, job.WaitForSuccessfulCompletion())
    86  	if err != nil {
    87  		return err.Error(), err
    88  	}
    89  
    90  	results, err := client.GetResults(context.Background(), submittedJob.Metadata.ID)
    91  	if err != nil {
    92  		return err.Error(), err
    93  	}
    94  
    95  	for _, result := range results {
    96  		if result.Data.CID != "" {
    97  			return result.Data.CID, nil
    98  		}
    99  	}
   100  
   101  	return "", fmt.Errorf("no results found?")
   102  }
   103  
   104  func runStableDiffusion(prompt string, testing bool) (string, error) {
   105  	var s model.Spec
   106  	if testing {
   107  		s = testSpec
   108  	} else {
   109  		s = realSpec
   110  	}
   111  	s.Docker.Entrypoint = append(s.Docker.Entrypoint, prompt)
   112  	return runGenericJob(s)
   113  }