Class: Kotoshu::ModelCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/kotoshu/commands/model_command.rb

Instance Method Summary collapse

Instance Method Details

#convert(input, output) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kotoshu/commands/model_command.rb', line 19

def convert(input, output)
  puts "Converting #{input} to #{output}..."

  # Check if input file exists
  unless File.exist?(input)
    puts "Error: Input file not found: #{input}"
    exit 1
  end

  # Build Python command
  script_path = File.join(File.dirname(__FILE__), '../../scripts/convert_fasttext_to_onnx.py')

  unless File.exist?(script_path)
    puts "Error: Conversion script not found: #{script_path}"
    exit 1
  end

  # Build command
  cmd = [
    'python3',
    script_path,
    '--input', input,
    '--output', output,
    '--language', options[:language],
    '--max-vectors', options[:max_vectors].to_s
  ]

  cmd << '--validate' if options[:validate]

  puts "Running: #{cmd.join(' ')}"

  # Execute conversion
  system(*cmd)

  if $?.success?
    puts "\n✓ Conversion successful!"
    puts "  Model: #{output}"
    puts "  Vocab: #{output.sub('.onnx', '.vocab.json')}"
    puts "  Metadata: #{output.sub('.onnx', '.metadata.json')}"
    puts "  Optimized: #{output.sub('.onnx', '.ort.onnx')}"
  else
    puts "\n✗ Conversion failed!"
    exit 1
  end
end

#download(language) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kotoshu/commands/model_command.rb', line 72

def download(language)
  puts "Downloading #{options[:type]} model for #{language}..."

  cache = Cache::ModelCache.new

  case options[:type]
  when 'fasttext'
    vec_file = cache.get_fasttext_model(language, force_download: options[:force])
    puts "✓ Downloaded to: #{vec_file}"
  when 'onnx'
    onnx_file = cache.get_onnx_model(language, force_download: options[:force])
    puts "✓ Downloaded to: #{onnx_file}"
  end

  # Show file info
  show_model_info(language)
end

#info(language) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/kotoshu/commands/model_command.rb', line 93

def info(language)
  cache = Cache::ModelCache.new

  puts "Model information for #{language}:"
  puts ""

  if options[:type].nil? || options[:type] == 'fasttext'
    model_info = cache.model_info(language, :fasttext)
    if model_info
      puts "FastText:"
      puts "  File: #{model_info[:file]}"
      puts "  Size: #{model_info[:size].to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse} vectors"
      puts "  Source: #{model_info[:source]}"
      puts ""
    end
  end

  if options[:type].nil? || options[:type] == 'onnx'
    model_info = cache.model_info(language, :onnx)
    if model_info
      puts "ONNX:"
      puts "  File: #{model_info[:file]}"
      puts "  Source: #{model_info[:source]}"
      puts ""
    end
  end
end

#listObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/kotoshu/commands/model_command.rb', line 122

def list
  cache = Cache::ModelCache.new
  all_models = cache.all_available_models

  puts "Available models:"
  puts ""

  all_models.each do |model_type, languages|
    puts "#{model_type.to_s.capitalize}:"

    languages.each do |code, info|
      puts "  #{code}:"
      puts "    File: #{info[:file]}"
      puts "    Source: #{info[:source]}"
    end

    puts ""
  end
end

#upload(language, model_file) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/kotoshu/commands/model_command.rb', line 182

def upload(language, model_file)
  puts "Uploading #{model_file} to #{options[:repo]}..."

  # Check if file exists
  unless File.exist?(model_file)
    puts "Error: File not found: #{model_file}"
    exit 1
  end

  # Determine model type and destination path
  if model_file.end_with?('.vec')
    model_type = 'fasttext'
    filename = File.basename(model_file)
    dest_path = "#{language}/models/fasttext/#{filename}"
  elsif model_file.end_with?('.onnx')
    model_type = 'onnx'
    filename = File.basename(model_file)
    dest_path = "#{language}/models/onnx/#{filename}"

    # Also upload vocab and metadata files
    vocab_file = model_file.sub('.onnx', '.vocab.json')
     = model_file.sub('.onnx', '.metadata.json')
    ort_file = model_file.sub('.onnx', '.ort.onnx')
  else
    puts "Error: Unknown file type. Expected .vec or .onnx"
    exit 1
  end

  # Build gh command
  cmd = [
    'gh', 'repo', 'clone', options[:repo], '/tmp/kotoshu-dictionaries'
  ]

  puts "Cloning repository..."
  system(*cmd)

  unless $?.success?
    puts "Error: Failed to clone repository"
    exit 1
  end

  # Copy files to destination
  target_dir = File.join('/tmp/kotoshu-dictionaries', File.dirname(dest_path))
  FileUtils.mkdir_p(target_dir)

  FileUtils.cp(model_file, File.join('/tmp/kotoshu-dictionaries', dest_path))

  if model_type == 'onnx'
    if File.exist?(vocab_file)
      FileUtils.cp(vocab_file, File.join('/tmp/kotoshu-dictionaries', dest_path.sub('.onnx', '.vocab.json')))
    end
    if File.exist?()
      FileUtils.cp(, File.join('/tmp/kotoshu-dictionaries', dest_path.sub('.onnx', '.metadata.json')))
    end
    if File.exist?(ort_file)
      FileUtils.cp(ort_file, File.join('/tmp/kotoshu-dictionaries', dest_path.sub('.onnx', '.ort.onnx')))
    end
  end

  # Commit and push
  Dir.chdir('/tmp/kotoshu-dictionaries') do
    system('git', 'add', '.')

    message = "Add #{model_type} model for #{language}\n\n"
    message += "Model: #{filename}\n"
    message += "Language: #{language}\n"

    system('git', 'commit', '-m', message)

    if options[:create_pr]
      # Create branch and PR
      branch_name = "add-#{model_type}-#{language}"
      system('git', 'checkout', '-b', branch_name)
      system('git', 'push', 'origin', branch_name)
      system('gh', 'pr', 'create', '--title', "Add #{model_type} model for #{language}", '--body', message)
    else
      # Direct push
      system('git', 'push')
    end
  end

  if $?.success?
    puts "✓ Upload successful!"
    puts "  Path: #{dest_path}"
    puts "  Repository: #{options[:repo]}"
  else
    puts "✗ Upload failed!"
    exit 1
  end
end

#validate(model_path) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/kotoshu/commands/model_command.rb', line 143

def validate(model_path)
  puts "Validating #{model_path}..."

  unless File.exist?(model_path)
    puts "Error: Model file not found: #{model_path}"
    exit 1
  end

  # Try to load the model
  begin
    model = Models::OnnxModel.from_file(model_path)

    puts "✓ Model loaded successfully"
    puts "  Language: #{model.language_code}"
    puts "  Dimension: #{model.dimension}"
    puts "  Vocabulary: #{model.vocabulary_size} words"

    # Test lookup
    test_word = model.vocabulary.first
    if test_word
      embedding = model.embedding_for(test_word)
      puts "  Test lookup: '#{test_word}' -> vector of size #{embedding.vector.size}"
    end

    puts "\n✓ Model is valid!"

  rescue StandardError => e
    puts "✗ Validation failed: #{e.message}"
    exit 1
  end
end