|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/docker/model-runner/cmd/cli/commands/completion" |
| 9 | + "github.com/docker/model-runner/cmd/cli/desktop" |
| 10 | + "github.com/docker/model-runner/pkg/distribution/types" |
| 11 | + dmrm "github.com/docker/model-runner/pkg/inference/models" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +func newShowCmd() *cobra.Command { |
| 16 | + var remote bool |
| 17 | + c := &cobra.Command{ |
| 18 | + Use: "show MODEL", |
| 19 | + Short: "Show information for a model", |
| 20 | + Long: "Display detailed information about a model in a human-readable format.", |
| 21 | + Args: requireExactArgs(1, "show", "MODEL"), |
| 22 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 23 | + output, err := showModel(args[0], remote, desktopClient) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + cmd.Print(output) |
| 28 | + return nil |
| 29 | + }, |
| 30 | + ValidArgsFunction: completion.ModelNames(getDesktopClient, 1), |
| 31 | + } |
| 32 | + c.Flags().BoolVarP(&remote, "remote", "r", false, "Show info for remote models") |
| 33 | + return c |
| 34 | +} |
| 35 | + |
| 36 | +func showModel(modelName string, remote bool, desktopClient *desktop.Client) (string, error) { |
| 37 | + model, err := desktopClient.Inspect(modelName, remote) |
| 38 | + if err != nil { |
| 39 | + return "", handleClientError(err, "Failed to get model "+modelName) |
| 40 | + } |
| 41 | + return formatModelInfo(model), nil |
| 42 | +} |
| 43 | + |
| 44 | +func formatModelInfo(model dmrm.Model) string { |
| 45 | + var sb strings.Builder |
| 46 | + |
| 47 | + // Model ID |
| 48 | + sb.WriteString(fmt.Sprintf("Model: %s\n", model.ID)) |
| 49 | + |
| 50 | + // Tags |
| 51 | + if len(model.Tags) > 0 { |
| 52 | + sb.WriteString(fmt.Sprintf("Tags: %s\n", strings.Join(model.Tags, ", "))) |
| 53 | + } |
| 54 | + |
| 55 | + // Created date |
| 56 | + if model.Created > 0 { |
| 57 | + created := time.Unix(model.Created, 0) |
| 58 | + sb.WriteString(fmt.Sprintf("Created: %s\n", created.Format(time.RFC3339))) |
| 59 | + } |
| 60 | + |
| 61 | + // Config details |
| 62 | + if model.Config != nil { |
| 63 | + sb.WriteString("\n") |
| 64 | + |
| 65 | + if cfg, ok := model.Config.(*types.Config); ok { |
| 66 | + // Config fields using data-driven approach |
| 67 | + fields := []struct { |
| 68 | + label string |
| 69 | + value string |
| 70 | + }{ |
| 71 | + {"Format:", string(cfg.Format)}, |
| 72 | + {"Architecture:", cfg.Architecture}, |
| 73 | + {"Parameters:", cfg.Parameters}, |
| 74 | + {"Size:", cfg.Size}, |
| 75 | + {"Quantization:", cfg.Quantization}, |
| 76 | + } |
| 77 | + |
| 78 | + for _, field := range fields { |
| 79 | + if field.value != "" { |
| 80 | + sb.WriteString(fmt.Sprintf("%-14s%s\n", field.label, field.value)) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if cfg.ContextSize != nil { |
| 85 | + sb.WriteString(fmt.Sprintf("%-14s%d\n", "Context Size:", *cfg.ContextSize)) |
| 86 | + } |
| 87 | + |
| 88 | + // Helper function to print metadata sections |
| 89 | + printMetadata := func(title string, data map[string]string) { |
| 90 | + if len(data) > 0 { |
| 91 | + sb.WriteString(fmt.Sprintf("\n%s:\n", title)) |
| 92 | + for k, v := range data { |
| 93 | + sb.WriteString(fmt.Sprintf(" %s: %s\n", k, v)) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + printMetadata("GGUF Metadata", cfg.GGUF) |
| 99 | + printMetadata("Safetensors Metadata", cfg.Safetensors) |
| 100 | + printMetadata("Diffusers Metadata", cfg.Diffusers) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return sb.String() |
| 105 | +} |
0 commit comments