Skip to content
Open
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
25 changes: 23 additions & 2 deletions lib/classifier/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def parse_options
@options[:type] = type
end

opts.on(
'--search TEXT',
'Search remote models by name/description, and local models by name only. Use quotes for multiword search'
) do |text|
@options[:search] = text
end

opts.on('-r', '--remote MODEL', 'Use remote model: name or @user/repo:name') do |model|
@options[:remote] = model
end
Expand Down Expand Up @@ -376,12 +383,20 @@ def list_remote_models

return if @exit_code != 0

if index['models'].empty?
models = index['models']

if @options[:search]
models = models.filter do |name, info|
[name, info['description']].any?(/#{Regexp.escape(@options[:search])}/i)
end
end

if models.empty?
@output << 'No models found in registry'
return
end

index['models'].each do |name, info|
models.each do |name, info|
type = info['type'] || 'unknown'
size = info['size'] || 'unknown'
desc = info['description'] || ''
Expand Down Expand Up @@ -413,6 +428,12 @@ def list_local_models

models = default_models + custom_models #: Array[{name: String, registry: String?, path: String}]

if @options[:search]
models = models.filter do |model|
model[:name] =~ /#{Regexp.escape(@options[:search])}/i
end
end

if models.empty?
@output << 'No local models found'
return
Expand Down
58 changes: 58 additions & 0 deletions test/cli/registry_commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,64 @@ def test_models_local_shows_no_models_when_cache_dir_missing
assert_match(/no local models found/i, result[:output])
end

def test_models_remote_search_by_name
stub_request(:get, 'https://raw.githubusercontent.com/cardmagic/classifier-models/main/models.json')
.to_return(status: 200, body: @models_json)

result = run_cli('models', '--search', 'spam-filter')

assert_equal 0, result[:exit_code]
assert_match(/spam-filter/, result[:output])
refute_match(/sentiment/, result[:output])
end

def test_models_remote_search_by_description
stub_request(:get, 'https://raw.githubusercontent.com/cardmagic/classifier-models/main/models.json')
.to_return(status: 200, body: @models_json)

result = run_cli('models', '--search', 'Spam Detection')

assert_equal 0, result[:exit_code]
assert_match(/spam-filter/, result[:output])
refute_match(/sentiment/, result[:output])
end

def test_models_remote_search_no_found
stub_request(:get, 'https://raw.githubusercontent.com/cardmagic/classifier-models/main/models.json')
.to_return(status: 200, body: @models_json)

result = run_cli('models', '--search', '[a-z]+')

assert_equal 0, result[:exit_code]
assert_match(/No models found in registry/, result[:output])
end

def test_models_local_search_by_name
# Create some cached models
models_dir = File.join(@cache_dir, 'models')
FileUtils.mkdir_p(models_dir)
File.write(File.join(models_dir, 'spam-filter.json'), @model_json)
File.write(File.join(models_dir, 'sentiment.json'), @model_json)

result = run_cli('models', '--local', '--search', 'spam-filter')

assert_equal 0, result[:exit_code]
assert_match(/spam-filter/, result[:output])
refute_match(/sentiment/, result[:output])
end

def test_models_local_search_no_found
# Create some cached models
models_dir = File.join(@cache_dir, 'models')
FileUtils.mkdir_p(models_dir)
File.write(File.join(models_dir, 'spam-filter.json'), @model_json)

result = run_cli('models', '--local', '--search', '[a-z]+')

assert_equal 0, result[:exit_code]
assert_match(/No local models found/, result[:output])
end

#
# Pull Command
#
Expand Down
Loading