Module: Rllama::Loader
- Defined in:
- lib/rllama/loader.rb
Constant Summary collapse
- HUGGINGFACE_BASE_URL =
'https://huggingface.co'- DEFAULT_DIR =
File.join(Dir.home, '.rllama')
- UNITS =
%w[B KB MB GB TB].freeze
Class Method Summary collapse
- .download_file(url, local_path, description) ⇒ Object
- .download_from_huggingface(hf_path, dir) ⇒ Object
- .download_from_url(url, dir) ⇒ Object
- .format_bytes(bytes) ⇒ Object
- .huggingface_path?(path) ⇒ Boolean
- .local_file?(path) ⇒ Boolean
- .resolve(path_or_name, dir: nil) ⇒ Object
- .url?(path) ⇒ Boolean
- .verify_download(local_path, expected_size) ⇒ Object
Class Method Details
.download_file(url, local_path, description) ⇒ Object
86 87 88 89 90 91 92 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rllama/loader.rb', line 86 def download_file(url, local_path, description) FileUtils.mkdir_p(File.dirname(local_path)) temp_path = File.join(File.dirname(local_path), "~#{File.basename(local_path)}") existing_size = File.exist?(temp_path) ? File.size(temp_path) : 0 uri = URI.parse(url) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| request = Net::HTTP::Get.new(uri.request_uri) request['Range'] = "bytes=#{existing_size}-" if existing_size.positive? http.request(request) do |response| case response when Net::HTTPSuccess, Net::HTTPPartialContent if response['Content-Range'] total_size = response['Content-Range'].split('/').last.to_i else total_size = response['content-length'].to_i if existing_size.positive? && response.code == '200' puts "\nServer doesn't support resume, starting from beginning..." existing_size = 0 FileUtils.rm_f(temp_path) end end downloaded = existing_size file_mode = existing_size.positive? ? 'ab' : 'wb' File.open(temp_path, file_mode) do |file| response.read_body do |chunk| file.write(chunk) downloaded += chunk.size if total_size.positive? progress = (downloaded.to_f / total_size * 100).round total_str = format_bytes(total_size) downloaded_str = format_bytes(downloaded) padding = total_str.length formatted_downloaded = format("%#{padding}s", downloaded_str) print format("\rProgress: %<progress>6d%% (%<downloaded>s / %<total>s)", progress: progress, downloaded: formatted_downloaded, total: total_str) else print "\rDownloaded: #{format_bytes(downloaded)}" end end end unless verify_download(temp_path, total_size) FileUtils.rm_f(temp_path) raise Error, 'Download verification failed - file size mismatch' end File.rename(temp_path, local_path) puts when Net::HTTPRedirection redirect_url = response['location'] redirect_url = URI.join(url, redirect_url).to_s unless redirect_url.start_with?('http://', 'https://') return download_file(redirect_url, local_path, description) when Net::HTTPRequestedRangeNotSatisfiable if File.exist?(temp_path) uri = URI.parse(url) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |check_http| head_request = Net::HTTP::Head.new(uri.request_uri) head_response = check_http.request(head_request) if head_response.is_a?(Net::HTTPSuccess) expected_size = head_response['content-length'].to_i actual_size = File.size(temp_path) if expected_size.positive? && expected_size == actual_size File.rename(temp_path, local_path) return local_path end end end File.delete(temp_path) return download_file(url, local_path, description) end raise Error, "Range request failed: #{response.code} #{response.}" else raise Error, "Failed to download model: #{response.code} #{response.}" end end end local_path end |
.download_from_huggingface(hf_path, dir) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rllama/loader.rb', line 52 def download_from_huggingface(hf_path, dir) parts = hf_path.split('/') raise Error, "Invalid HuggingFace path: #{hf_path}" if parts.length < 3 org = parts[0] repo = parts[1] file_path = parts[2..].join('/') url = "#{HUGGINGFACE_BASE_URL}/#{org}/#{repo}/resolve/main/#{file_path}" local_path = File.join(dir, org, repo, file_path) return local_path if File.exist?(local_path) puts "Destination: #{local_path}" download_file(url, local_path, "HuggingFace model: #{hf_path}") end |
.download_from_url(url, dir) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rllama/loader.rb', line 72 def download_from_url(url, dir) uri = URI.parse(url) filename = File.basename(uri.path) local_path = File.join(dir, filename) return local_path if File.exist?(local_path) puts "Destination: #{local_path}" download_file(url, local_path, "URL: #{url}") end |
.format_bytes(bytes) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rllama/loader.rb', line 196 def format_bytes(bytes) return '0 B' if bytes.zero? exp = (Math.log(bytes) / Math.log(1024)).floor exp = [exp, UNITS.length - 1].min value = bytes.to_f / (1024**exp) if exp >= 3 format('%<val>.2f %<unit>s', val: value, unit: UNITS[exp]) else format('%<val>d %<unit>s', val: value.round, unit: UNITS[exp]) end end |
.huggingface_path?(path) ⇒ Boolean
44 45 46 47 48 49 50 |
# File 'lib/rllama/loader.rb', line 44 def huggingface_path?(path) return false if path.start_with?('/') || path.include?('://') parts = path.split('/') parts.length >= 3 && parts.last.end_with?('.gguf') end |
.local_file?(path) ⇒ Boolean
32 33 34 |
# File 'lib/rllama/loader.rb', line 32 def local_file?(path) File.exist?(path) end |
.resolve(path_or_name, dir: nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rllama/loader.rb', line 16 def resolve(path_or_name, dir: nil) dir ||= DEFAULT_DIR dir = File.join(dir, 'models') return path_or_name if local_file?(path_or_name) if url?(path_or_name) download_from_url(path_or_name, dir) elsif huggingface_path?(path_or_name) download_from_huggingface(path_or_name, dir) else raise Error, "Invalid model path or name: #{path_or_name}" end end |
.url?(path) ⇒ Boolean
36 37 38 39 40 41 42 |
# File 'lib/rllama/loader.rb', line 36 def url?(path) uri = URI.parse(path) uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) rescue URI::InvalidURIError false end |
.verify_download(local_path, expected_size) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/rllama/loader.rb', line 189 def verify_download(local_path, expected_size) return true if expected_size <= 0 actual_size = File.size(local_path) actual_size == expected_size end |