-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support for list in inputs #1561
Conversation
f02b3e9
to
641da4c
Compare
641da4c
to
c8c1906
Compare
Signed-off-by: Dmitri Khokhlov <dkhokhlov@gmail.com>
c8c1906
to
4a16aa1
Compare
…ct -> path-project, as it is about path Signed-off-by: Dmitri Khokhlov <dkhokhlov@gmail.com>
Signed-off-by: Dmitri Khokhlov <dkhokhlov@gmail.com>
Signed-off-by: Dmitri Khokhlov <dkhokhlov@gmail.com>
f4b617a
to
c8b9353
Compare
This is awesome, @dkhokhlov! Can you take a shot at documenting this in docs/python.md ? Let me know if you want help with that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't finished going through this yet, but I figure I should release the comments I've made already to get started :)
"github.com/vincent-petithory/dataurl" | ||
|
||
"github.com/mitchellh/go-homedir" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import moved, and it should move back :)
@@ -3,6 +3,7 @@ | |||
|
|||
This prints a JSON object describing the inputs of the model. | |||
""" | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change is required by one of new linters in make lint
:
python -m ruff format --check python
and reformatted by:
python -m ruff format python/cog/command/openapi_schema.py
webhook_events_filter: t.Optional[ | ||
t.List[WebhookEvent] | ||
] = WebhookEvent.default_events() | ||
webhook_events_filter: t.Optional[t.List[WebhookEvent]] = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes like this suggest that you don't have your editor set up to do autoformatting the same as script/format
. Would you look into that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change is required by one of new linters in our make lint
:
python -m ruff python/cog
and reformatted by:
python -m ruff format python/cog/schema.py
pkg/predict/input.go
Outdated
) | ||
|
||
type Input struct { | ||
String *string | ||
File *string | ||
Array *[]interface{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two small points:
- it's nice to use
any
rather thaninterface{}
these days - this doesn't need to be a pointer -- slices are already pointer types
pkg/predict/input.go
Outdated
var dataURLs []string | ||
for _, elem := range *input.Array { | ||
if str, ok := elem.(string); ok && strings.HasPrefix(str, "@") { | ||
filePath := str[1:] // Remove '@' prefix | ||
dataURL, err := fileToDataURL(filePath) | ||
if err != nil { | ||
return keyVals, err | ||
} | ||
dataURLs = append(dataURLs, dataURL) | ||
} else if ok { | ||
// Directly use the string if it's not a file path | ||
dataURLs = append(dataURLs, str) | ||
} | ||
} | ||
keyVals[key] = dataURLs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a big deal, but it would be more conventional when you know the length of the slice beforehand to do something like
var dataURLs []string | |
for _, elem := range *input.Array { | |
if str, ok := elem.(string); ok && strings.HasPrefix(str, "@") { | |
filePath := str[1:] // Remove '@' prefix | |
dataURL, err := fileToDataURL(filePath) | |
if err != nil { | |
return keyVals, err | |
} | |
dataURLs = append(dataURLs, dataURL) | |
} else if ok { | |
// Directly use the string if it's not a file path | |
dataURLs = append(dataURLs, str) | |
} | |
} | |
keyVals[key] = dataURLs | |
dataURLs := make([]string, len(input.Array)) | |
for i, elem := range input.Array { | |
if str, ok := elem.(string); ok && strings.HasPrefix(str, "@") { | |
filePath := str[1:] // Remove '@' prefix | |
dataURL, err := fileToDataURL(filePath) | |
if err != nil { | |
return keyVals, err | |
} | |
dataURLs[i] = dataURL | |
} else if ok { | |
// Directly use the string if it's not a file path | |
dataURLs[i] = str | |
} | |
} | |
keyVals[key] = dataURLs |
pkg/predict/input.go
Outdated
} else if strings.HasPrefix(val, "[") && strings.HasSuffix(val, "]") { | ||
// Handle array of strings | ||
var arr []interface{} | ||
if err := json.Unmarshal([]byte(val), &arr); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is JSON unmarshalling right here? That means the strings have to be quoted, which is inconsistent with the other types.
For example, I can do
cog predict hello-world -i text=world
or
cog predict resnet -i image=@input.jpg
but with this implementation, the following will throw an error
cog predict mymodel -i names=[foo,bar]
I think there are two possibilities here. Either:
- do the work to allow individual strings to be either quoted or unquoted, so that they're consistent with the rest of the interface, or
- change approach to do arrays by just repeating inputs, e.g.
cog predict ... -i names=one -i names=two
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose repeated inputs. Good idea. Thanks.
dd10b81
to
1b36ac1
Compare
Signed-off-by: Dmitri Khokhlov <dkhokhlov@gmail.com>
1b36ac1
to
71d0d7c
Compare
@zeke |
Ref: