Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
560 changes: 304 additions & 256 deletions repli

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/commands/get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ else
fi

# download outputs
if verify_success_json "$outfile"; then
if json_verify_success_file "$outfile"; then
download_outputs "$prefix" "$outfile"
fi
13 changes: 9 additions & 4 deletions src/commands/template/new.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ if [[ -f "$outpath" && ! "$force" ]]; then
fi

mkdir -p "$templates_dir"
log debug "fetching example for $(blue "$model")"
template="$(get_example_from_replicate "$model")"
log info "building template for $(blue "$model")"
json=$(get_model_info "$model") || return 1
template="$(json_to_template "$json")"
if [[ -n "$template" ]]; then
log info "saving to $(blue "$outpath")"
printf "%s\n" "$template" >"$outpath"
else
log error "received an empty template"
fi

log info "saving to $(blue "$outpath")"
[[ -n "$template" ]] && printf "%s\n" "$template" >"$outpath"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## Get uploaded files list from replicate
get_files_list() {
curl -s -H "Authorization: Token $REPLICATE_API_TOKEN" "$replicate_host/v1/files"
}
3 changes: 3 additions & 0 deletions src/lib/get_model_info.sh → src/lib/core/get_model_info.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Call replicate and get the JSON for the model
## usage: json=$(get_model_info "$model") || return 1
## input: model name
get_model_info() {
local model="$1"
local body status
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions src/lib/json-parsers/json_get_model_properties.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## prints all model properties that have enum options
## input: a model info JSON
json_get_model_properties() {
local json="$1"

# iterate over properties
while read -r prop; do
enums=$(json_get_prop_enums "$json" "$prop")
[[ -n "$enums" ]] || enums='*'
echo "$prop: $enums"
done < <(json_get_model_properties_list "$json")
}
10 changes: 10 additions & 0 deletions src/lib/json-parsers/json_get_model_properties_list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## returns an array of all input properties from the JSON schema
## input: model info JSON
json_get_model_properties_list() {
local json="$1"

echo "$json" | jq -r '
.latest_version.openapi_schema.components.schemas.Input.properties
| keys[]
'
}
12 changes: 12 additions & 0 deletions src/lib/json-parsers/json_get_prop_enums.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## returns a space delimited string of allowed enum values for a property
## input: model info JSON, property name
json_get_prop_enums() {
local json="$1"
local prop="$2"

echo "$json" | jq -r --arg prop "$prop" '
.latest_version.openapi_schema.components.schemas[$prop]
| .. | objects | select(has("enum")) | .enum? // empty
| join(", ")
'
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
get_example_from_replicate() {
json=$(get_model_info "$model") || return 1
## generates a template YAML from model info
## input: model info JSON
json_to_template() {
json="$1"

# Determine whether the model is official
is_official=$(jq -r '.is_official // false' <<<"$json")

echo "# https://replicate.com/$model"
echo

if [[ "$is_official" == "true" ]]; then
log debug "model status: $(blue official)"
# Official image
Expand All @@ -19,4 +24,11 @@ get_example_from_replicate() {
'{version: ($model + ":" + $version), input: .default_example.input}' \
<<<"$json" | yq -P -
fi

echo
echo "# PROPERTIES"
mapfile -t props < <(json_get_model_properties "$json")
for propline in "${props[@]}"; do
echo "# $propline"
done
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
verify_success_json() {
json_verify_success_file() {
local json_file="$1"
local status

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions test/approvals/cat_banana_yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# https://replicate.com/google/nano-banana

model: google/nano-banana
input:
prompt: an image of a JSON file
output_format: jpg

# PROPERTIES
# aspect_ratio: match_input_image, 1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9
# image_input: *
# output_format: jpg, png
# prompt: *
2 changes: 1 addition & 1 deletion test/approvals/repli_get@error
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
• debug • repli_get_command → prefix: repli-1
• debug • repli_get_command → outfile: tmp/repli-1.json
• info • repli_get_command → calling API and saving tmp/repli-1.json
• error • verify_success_json → received invalid response:
• error • json_verify_success_file → received invalid response:
detail: |
- input.image_input.0: Invalid type. Expected: string, given: object
status: 422
Expand Down
29 changes: 28 additions & 1 deletion test/approvals/repli_info_google_nano_banana
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,31 @@ latest_version:
title: Input
required:
- prompt
properties: '... trimmed ...'
properties:
prompt: '... trimmed... '
image_input: '... trimmed... '
aspect_ratio: '... trimmed... '
output_format: '... trimmed... '
aspect_ratio:
enum:
- match_input_image
- 1:1
- 2:3
- 3:2
- 3:4
- 4:3
- 4:5
- 5:4
- 9:16
- 16:9
- 21:9
type: string
title: aspect_ratio
description: An enumeration.
output_format:
enum:
- jpg
- png
type: string
title: output_format
description: An enumeration.
34 changes: 33 additions & 1 deletion test/approvals/repli_info_google_nano_banana_json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,39 @@
"required": [
"prompt"
],
"properties": "... trimmed ..."
"properties": {
"prompt": "... trimmed... ",
"image_input": "... trimmed... ",
"aspect_ratio": "... trimmed... ",
"output_format": "... trimmed... "
}
},
"aspect_ratio": {
"enum": [
"match_input_image",
"1:1",
"2:3",
"3:2",
"3:4",
"4:3",
"4:5",
"5:4",
"9:16",
"16:9",
"21:9"
],
"type": "string",
"title": "aspect_ratio",
"description": "An enumeration."
},
"output_format": {
"enum": [
"jpg",
"png"
],
"type": "string",
"title": "output_format",
"description": "An enumeration."
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion test/approvals/repli_info_google_nano_banana_schema
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ type: object
title: Input
required:
- prompt
properties: '... trimmed ...'
properties:
prompt: '... trimmed... '
image_input: '... trimmed... '
aspect_ratio: '... trimmed... '
output_format: '... trimmed... '
4 changes: 2 additions & 2 deletions test/approvals/repli_template_new_google_nano_banana
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
• debug • repli_template_new_command → fetching example for google/nano-banana
• info • repli_template_new_command → building template for google/nano-banana
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: official
• debug • json_to_template → model status: official
• info • repli_template_new_command → saving to tmp/templates/nano-banana.yaml
4 changes: 2 additions & 2 deletions test/approvals/repli_template_new_google_nano_banana_force
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
• debug • repli_template_new_command → fetching example for google/nano-banana
• info • repli_template_new_command → building template for google/nano-banana
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: official
• debug • json_to_template → model status: official
• info • repli_template_new_command → saving to tmp/templates/nano-banana.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
• debug • repli_template_new_command → fetching example for google/nano-banana
• info • repli_template_new_command → building template for google/nano-banana
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: official
• debug • json_to_template → model status: official
• info • repli_template_new_command → saving to tmp/templates/banana.yaml
2 changes: 1 addition & 1 deletion test/approvals/repli_template_new_not_found
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
• debug • repli_template_new_command → fetching example for not/found
• info • repli_template_new_command → building template for not/found
• debug • get_model_info → calling replicate API
• error • get_model_info → failed getting model info for not/found
• error • get_model_info → (404)
4 changes: 2 additions & 2 deletions test/approvals/repli_template_new_unofficial_mymodel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
• debug • repli_template_new_command → fetching example for unofficial/mymodel
• info • repli_template_new_command → building template for unofficial/mymodel
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: unofficial
• debug • json_to_template → model status: unofficial
• info • repli_template_new_command → saving to tmp/templates/mymodel.yaml
34 changes: 33 additions & 1 deletion test/mockserver/mocks/v1/models/google/nano-banana/get.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,39 @@
"required": [
"prompt"
],
"properties": "... trimmed ..."
"properties": {
"prompt": "... trimmed... ",
"image_input": "... trimmed... ",
"aspect_ratio": "... trimmed... ",
"output_format": "... trimmed... "
}
},
"aspect_ratio": {
"enum": [
"match_input_image",
"1:1",
"2:3",
"3:2",
"3:4",
"4:3",
"4:5",
"5:4",
"9:16",
"16:9",
"21:9"
],
"type": "string",
"title": "aspect_ratio",
"description": "An enumeration."
},
"output_format": {
"enum": [
"jpg",
"png"
],
"type": "string",
"title": "output_format",
"description": "An enumeration."
}
}
}
Expand Down
34 changes: 33 additions & 1 deletion test/mockserver/mocks/v1/models/unofficial/mymodel/get.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,39 @@
"required": [
"prompt"
],
"properties": "... trimmed ..."
"properties": {
"prompt": "... trimmed... ",
"image_input": "... trimmed... ",
"aspect_ratio": "... trimmed... ",
"output_format": "... trimmed... "
}
},
"aspect_ratio": {
"enum": [
"match_input_image",
"1:1",
"2:3",
"3:2",
"3:4",
"4:3",
"4:5",
"5:4",
"9:16",
"16:9",
"21:9"
],
"type": "string",
"title": "aspect_ratio",
"description": "An enumeration."
},
"output_format": {
"enum": [
"jpg",
"png"
],
"type": "string",
"title": "output_format",
"description": "An enumeration."
}
}
}
Expand Down