-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update to replicate-go v0.20.0 * Add deployments subcommand
- Loading branch information
Showing
10 changed files
with
614 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package deployment | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/cli/browser" | ||
"github.com/replicate/replicate-go" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/replicate/cli/internal/client" | ||
"github.com/replicate/cli/internal/identifier" | ||
"github.com/replicate/cli/internal/util" | ||
) | ||
|
||
// createCmd represents the create command | ||
var createCmd = &cobra.Command{ | ||
Use: "create <[owner/]name> [flags]", | ||
Short: "Create a new deployment", | ||
Example: `replicate deployment create text-to-image --model=stability-ai/sdxl --hardware=gpu-a100-large`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
r8, err := client.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
opts := &replicate.CreateDeploymentOptions{} | ||
|
||
opts.Name = args[0] | ||
|
||
flags := cmd.Flags() | ||
|
||
modelFlag, _ := flags.GetString("model") | ||
id, err := identifier.ParseIdentifier(modelFlag) | ||
if err != nil { | ||
return fmt.Errorf("expected <owner>/<name>[:version] but got %s", args[0]) | ||
} | ||
opts.Model = fmt.Sprintf("%s/%s", id.Owner, id.Name) | ||
if id.Version != "" { | ||
opts.Version = id.Version | ||
} else { | ||
model, err := r8.GetModel(cmd.Context(), id.Owner, id.Name) | ||
if err != nil { | ||
return fmt.Errorf("failed to get model: %w", err) | ||
} | ||
opts.Version = model.LatestVersion.ID | ||
} | ||
|
||
opts.Hardware, _ = flags.GetString("hardware") | ||
|
||
flagMap := map[string]*int{ | ||
"min-instances": &opts.MinInstances, | ||
"max-instances": &opts.MaxInstances, | ||
} | ||
for flagName, optPtr := range flagMap { | ||
if flags.Changed(flagName) { | ||
value, _ := flags.GetInt(flagName) | ||
*optPtr = value | ||
} | ||
} | ||
|
||
deployment, err := r8.CreateDeployment(cmd.Context(), *opts) | ||
if err != nil { | ||
return fmt.Errorf("failed to create deployment: %w", err) | ||
} | ||
|
||
if flags.Changed("json") || !util.IsTTY() { | ||
bytes, err := json.MarshalIndent(deployment, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("failed to serialize model: %w", err) | ||
} | ||
fmt.Println(string(bytes)) | ||
return nil | ||
} | ||
|
||
url := fmt.Sprintf("https://replicate.com/deployments/%s/%s", deployment.Owner, deployment.Name) | ||
if flags.Changed("web") { | ||
if util.IsTTY() { | ||
fmt.Println("Opening in browser...") | ||
} | ||
|
||
err := browser.OpenURL(url) | ||
if err != nil { | ||
return fmt.Errorf("failed to open browser: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
fmt.Printf("Deployment created: %s\n", url) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
addCreateFlags(createCmd) | ||
} | ||
|
||
func addCreateFlags(cmd *cobra.Command) { | ||
cmd.Flags().String("model", "", "Model to deploy") | ||
_ = cmd.MarkFlagRequired("model") | ||
|
||
cmd.Flags().String("hardware", "", "SKU of the hardware to run the model") | ||
_ = cmd.MarkFlagRequired("hardware") | ||
|
||
cmd.Flags().Int("min-instances", 0, "Minimum number of instances to run the model") | ||
cmd.Flags().Int("max-instances", 0, "Maximum number of instances to run the model") | ||
|
||
cmd.Flags().Bool("json", false, "Emit JSON") | ||
cmd.Flags().Bool("web", false, "View on web") | ||
cmd.MarkFlagsMutuallyExclusive("json", "web") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package deployment | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/replicate/cli/internal/client" | ||
"github.com/replicate/cli/internal/util" | ||
|
||
"github.com/charmbracelet/bubbles/table" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
var baseStyle = lipgloss.NewStyle(). | ||
BorderStyle(lipgloss.NormalBorder()). | ||
BorderForeground(lipgloss.Color("240")) | ||
|
||
type model struct { | ||
table table.Model | ||
} | ||
|
||
func (m model) Init() tea.Cmd { return nil } | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
switch msg := msg.(type) { //nolint:gocritic | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "esc": | ||
if m.table.Focused() { | ||
m.table.Blur() | ||
} else { | ||
m.table.Focus() | ||
} | ||
case "q", "ctrl+c": | ||
return m, tea.Quit | ||
case "enter": | ||
selected := m.table.SelectedRow() | ||
if len(selected) == 0 { | ||
return m, nil | ||
} | ||
url := fmt.Sprintf("https://replicate.com/deployments/%s", selected[0]) | ||
return m, tea.ExecProcess(exec.Command("open", url), nil) | ||
} | ||
} | ||
m.table, cmd = m.table.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
return baseStyle.Render(m.table.View()) + "\n" | ||
} | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List deployments", | ||
Example: "replicate deployment list", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
ctx := cmd.Context() | ||
|
||
r8, err := client.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
deployments, err := r8.ListDeployments(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to get deployments: %w", err) | ||
} | ||
|
||
if cmd.Flags().Changed("json") || !util.IsTTY() { | ||
bytes, err := json.MarshalIndent(deployments, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal deployments: %w", err) | ||
} | ||
fmt.Println(string(bytes)) | ||
return nil | ||
} | ||
|
||
columns := []table.Column{ | ||
{Title: "Name", Width: 20}, | ||
{Title: "Release #", Width: 10}, | ||
{Title: "Model Version", Width: 60}, | ||
} | ||
|
||
rows := []table.Row{} | ||
|
||
for _, deployment := range deployments.Results { | ||
rows = append(rows, table.Row{ | ||
deployment.Owner + "/" + deployment.Name, | ||
strconv.Itoa(deployment.CurrentRelease.Number), | ||
fmt.Sprintf("%s:%s", deployment.CurrentRelease.Model, deployment.CurrentRelease.Version), | ||
}) | ||
} | ||
|
||
t := table.New( | ||
table.WithColumns(columns), | ||
table.WithRows(rows), | ||
table.WithFocused(true), | ||
table.WithHeight(30), | ||
) | ||
|
||
s := table.DefaultStyles() | ||
s.Header = s.Header. | ||
BorderStyle(lipgloss.NormalBorder()). | ||
BorderForeground(lipgloss.Color("240")). | ||
BorderBottom(true). | ||
Bold(false) | ||
s.Selected = s.Selected. | ||
Foreground(lipgloss.Color("229")). | ||
Background(lipgloss.Color("57")). | ||
Bold(false) | ||
t.SetStyles(s) | ||
|
||
m := model{t} | ||
if _, err := tea.NewProgram(m).Run(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
addListFlags(listCmd) | ||
} | ||
|
||
func addListFlags(cmd *cobra.Command) { | ||
cmd.Flags().Bool("json", false, "Emit JSON") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package deployment | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var RootCmd = &cobra.Command{ | ||
Use: "deployments [subcommand]", | ||
Short: "Interact with deployments", | ||
Aliases: []string{"deployments", "d"}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddGroup(&cobra.Group{ | ||
ID: "subcommand", | ||
Title: "Subcommands:", | ||
}) | ||
for _, cmd := range []*cobra.Command{ | ||
listCmd, | ||
showCmd, | ||
schemaCmd, | ||
createCmd, | ||
updateCmd, | ||
} { | ||
RootCmd.AddCommand(cmd) | ||
cmd.GroupID = "subcommand" | ||
} | ||
|
||
// RootCmd.AddGroup(&cobra.Group{ | ||
// ID: "alias", | ||
// Title: "Alias commands:", | ||
// }) | ||
// for _, cmd := range []*cobra.Command{ | ||
// runCmd, | ||
// } { | ||
// RootCmd.AddCommand(cmd) | ||
// cmd.GroupID = "alias" | ||
// } | ||
} |
Oops, something went wrong.