@@ -141,34 +141,31 @@ def apply_model_source!
141141 config . model = download_default_model
142142 end
143143
144+ def client
145+ @client ||= HTTPX . plugin ( :follow_redirects )
146+ end
147+
144148 def download_default_model
145149 model_file = model_file_for_variant ( config . variant )
146- root = File . expand_path ( '..' , __dir__ )
147- dir = File . join ( root , '.cache' , 'models' , HF_REPO . tr ( '/' , '__' ) )
150+ dir = File . join ( Dir . pwd , '.cache' , 'gliner' , HF_REPO . tr ( '/' , '__' ) )
148151
149152 FileUtils . mkdir_p ( dir )
150153
151154 files = [ 'tokenizer.json' , 'config.json' , model_file ]
152- client = HTTPX . plugin ( :follow_redirects )
153155
154156 files . each do |file |
155- dest = File . join ( dir , file )
156- next if File . exist? ( dest ) && File . size? ( dest )
157+ target = File . join ( dir , file )
157158
158- download_file! ( client , "#{ DEFAULT_MODEL_BASE } /#{ file } " , dest )
159+ next if File . exist? ( target ) && File . size? ( target )
160+
161+ client
162+ . get ( "#{ DEFAULT_MODEL_BASE } /#{ file } " )
163+ . copy_to ( target )
159164 end
160165
161166 dir
162167 end
163168
164- def download_file! ( client , url , dest )
165- response = client . get ( url )
166-
167- raise Error , "Download failed: #{ url } (status: #{ response . error } )" if response . error
168-
169- File . binwrite ( dest , response . body . to_s )
170- end
171-
172169 def env_model_dir
173170 dir = ENV . fetch ( 'GLINER_MODEL_DIR' , nil )
174171 return nil if dir . nil? || dir . empty?
0 commit comments