lee-ite commited on
Commit
5b73e0a
·
1 Parent(s): bdf9666
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -15,7 +15,6 @@ from apscheduler.schedulers.background import BackgroundScheduler
15
  from textwrap import dedent
16
 
17
  HF_TOKEN = os.environ.get("HF_TOKEN")
18
- items_to_keep = ['build', 'llama.cpp' 'venv', 'app.py', 'start.sh', 'Dockerfile', 'README.md']
19
 
20
 
21
  class bcolors:
@@ -101,6 +100,7 @@ def process_lora(model_id, lora_id, merged_name, methods, private_repo, oauth_to
101
  lora_name = lora_id.split('/')[-1]
102
  model_fp16 = f"{model_name}-f16.gguf"
103
  lora_fp16 = f"{lora_name}-fp16.gguf"
 
104
 
105
  try:
106
  api = HfApi(token=oauth_token)
@@ -127,12 +127,14 @@ def process_lora(model_id, lora_id, merged_name, methods, private_repo, oauth_to
127
 
128
  # Download Raw Model from HF
129
  api.snapshot_download(repo_id=model_id, local_dir=model_name, allow_patterns=dl_pattern)
 
130
  print("Model downloaded successfully!")
131
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
132
  print(f"{bcolors.OKBLUE}Model directory contents: {os.listdir(model_name)} {bcolors.ENDC}")
133
 
134
  # Download LoRA adapter from HF
135
  api.snapshot_download(repo_id=lora_id, local_dir=lora_name, allow_patterns=dl_pattern)
 
136
  print("LoRA downloaded successfully!")
137
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
138
  print(f"{bcolors.OKBLUE}LoRA directory contents: {os.listdir(lora_name)} {bcolors.ENDC}")
@@ -141,6 +143,7 @@ def process_lora(model_id, lora_id, merged_name, methods, private_repo, oauth_to
141
  lora_conversion_script = "convert_lora_to_gguf.py"
142
  lora_fp16_conversion = f"python llama.cpp/{lora_conversion_script} --base {model_name} {lora_name} --outtype f16 --outfile {lora_fp16}"
143
  lora_result = subprocess.run(lora_fp16_conversion, shell=True, capture_output=True)
 
144
  print(lora_result)
145
  if lora_result.returncode != 0:
146
  raise Exception(f"Error converting to fp16: {lora_result.stderr}")
@@ -162,6 +165,7 @@ def process_lora(model_id, lora_id, merged_name, methods, private_repo, oauth_to
162
  base_conversion_script = "convert_hf_to_gguf.py"
163
  base_fp16_conversion = f"python llama.cpp/{base_conversion_script} {model_name} --outtype f16 --outfile {model_fp16}"
164
  base_result = subprocess.run(base_fp16_conversion, shell=True, capture_output=True)
 
165
  print(base_result)
166
  if base_result.returncode != 0:
167
  raise Exception(f"Error converting to fp16: {base_result.stderr}")
@@ -172,31 +176,39 @@ def process_lora(model_id, lora_id, merged_name, methods, private_repo, oauth_to
172
  # Clean storage: hf-model & hf-lora
173
  shutil.rmtree(model_name, ignore_errors=True)
174
  shutil.rmtree(lora_name, ignore_errors=True)
 
 
175
  print("HF model & LoRA cleaned up successfully!")
176
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
177
 
178
  # Merge LoRA with Raw Model to GGUF-fp16
179
  print(f"Merging LoRA with Model => fp16")
180
  merged_fp16 = export_lora_to_gguf(model_fp16, lora_fp16, merged_name)
 
181
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
182
  # Upload Merged GGUF-fp16 to HF
183
  upload_file_to_hf(f"{merged_name}-fp16.gguf", new_repo_id, api)
184
  # Remove LoRA-GGUF & Model-GGUF
185
  os.remove(model_fp16)
186
  os.remove(lora_fp16)
 
 
187
 
188
  if methods is not None:
189
  # Quantize GGUF-fp16 one by one
190
  for method in methods:
191
  print(f"Quantizing merged fp16-gguf to {method}")
192
  quantized_name = quantize_merged_gguf(merged_fp16, method)
 
193
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
194
  upload_file_to_hf(quantized_name, new_repo_id, api)
195
  os.remove(quantized_name)
 
196
  print("Removed the uploaded model.")
197
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
198
 
199
  os.remove(f"{merged_fp16}-fp16.gguf")
 
200
  print("Remove the fp16 GGUF file.")
201
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
202
 
@@ -210,10 +222,10 @@ def process_lora(model_id, lora_id, merged_name, methods, private_repo, oauth_to
210
  all_items = os.listdir(current_directory)
211
  for item in all_items:
212
  item_path = os.path.join(current_directory, item)
213
- if os.path.isfile(item_path) and item not in items_to_keep:
214
  os.remove(item_path)
215
  print(f"Delete file: {item_path}")
216
- elif os.path.isdir(item_path) and item not in items_to_keep:
217
  shutil.rmtree(item_path, ignore_errors=True)
218
  print(f"Delete folder: {item_path}")
219
  print("Folder cleaned up successfully!")
 
15
  from textwrap import dedent
16
 
17
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
18
 
19
 
20
  class bcolors:
 
100
  lora_name = lora_id.split('/')[-1]
101
  model_fp16 = f"{model_name}-f16.gguf"
102
  lora_fp16 = f"{lora_name}-fp16.gguf"
103
+ items_to_remove = []
104
 
105
  try:
106
  api = HfApi(token=oauth_token)
 
127
 
128
  # Download Raw Model from HF
129
  api.snapshot_download(repo_id=model_id, local_dir=model_name, allow_patterns=dl_pattern)
130
+ items_to_remove.append(model_name)
131
  print("Model downloaded successfully!")
132
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
133
  print(f"{bcolors.OKBLUE}Model directory contents: {os.listdir(model_name)} {bcolors.ENDC}")
134
 
135
  # Download LoRA adapter from HF
136
  api.snapshot_download(repo_id=lora_id, local_dir=lora_name, allow_patterns=dl_pattern)
137
+ items_to_remove.append(lora_name)
138
  print("LoRA downloaded successfully!")
139
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
140
  print(f"{bcolors.OKBLUE}LoRA directory contents: {os.listdir(lora_name)} {bcolors.ENDC}")
 
143
  lora_conversion_script = "convert_lora_to_gguf.py"
144
  lora_fp16_conversion = f"python llama.cpp/{lora_conversion_script} --base {model_name} {lora_name} --outtype f16 --outfile {lora_fp16}"
145
  lora_result = subprocess.run(lora_fp16_conversion, shell=True, capture_output=True)
146
+ items_to_remove.append(lora_fp16)
147
  print(lora_result)
148
  if lora_result.returncode != 0:
149
  raise Exception(f"Error converting to fp16: {lora_result.stderr}")
 
165
  base_conversion_script = "convert_hf_to_gguf.py"
166
  base_fp16_conversion = f"python llama.cpp/{base_conversion_script} {model_name} --outtype f16 --outfile {model_fp16}"
167
  base_result = subprocess.run(base_fp16_conversion, shell=True, capture_output=True)
168
+ items_to_remove.append(model_fp16)
169
  print(base_result)
170
  if base_result.returncode != 0:
171
  raise Exception(f"Error converting to fp16: {base_result.stderr}")
 
176
  # Clean storage: hf-model & hf-lora
177
  shutil.rmtree(model_name, ignore_errors=True)
178
  shutil.rmtree(lora_name, ignore_errors=True)
179
+ items_to_remove.remove(model_name)
180
+ items_to_remove.remove(lora_name)
181
  print("HF model & LoRA cleaned up successfully!")
182
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
183
 
184
  # Merge LoRA with Raw Model to GGUF-fp16
185
  print(f"Merging LoRA with Model => fp16")
186
  merged_fp16 = export_lora_to_gguf(model_fp16, lora_fp16, merged_name)
187
+ items_to_remove.append(f"{merged_fp16}-fp16.gguf")
188
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
189
  # Upload Merged GGUF-fp16 to HF
190
  upload_file_to_hf(f"{merged_name}-fp16.gguf", new_repo_id, api)
191
  # Remove LoRA-GGUF & Model-GGUF
192
  os.remove(model_fp16)
193
  os.remove(lora_fp16)
194
+ items_to_remove.remove(model_fp16)
195
+ items_to_remove.remove(lora_fp16)
196
 
197
  if methods is not None:
198
  # Quantize GGUF-fp16 one by one
199
  for method in methods:
200
  print(f"Quantizing merged fp16-gguf to {method}")
201
  quantized_name = quantize_merged_gguf(merged_fp16, method)
202
+ items_to_remove.append(quantized_name)
203
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
204
  upload_file_to_hf(quantized_name, new_repo_id, api)
205
  os.remove(quantized_name)
206
+ items_to_remove.remove(quantized_name)
207
  print("Removed the uploaded model.")
208
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
209
 
210
  os.remove(f"{merged_fp16}-fp16.gguf")
211
+ items_to_remove.remove(f"{merged_fp16}-fp16.gguf")
212
  print("Remove the fp16 GGUF file.")
213
  print(f"{bcolors.OKGREEN}Files in current working directory: {os.listdir(current_directory)} {bcolors.ENDC}")
214
 
 
222
  all_items = os.listdir(current_directory)
223
  for item in all_items:
224
  item_path = os.path.join(current_directory, item)
225
+ if os.path.isfile(item_path) and item in items_to_remove:
226
  os.remove(item_path)
227
  print(f"Delete file: {item_path}")
228
+ elif os.path.isdir(item_path) and item in items_to_remove:
229
  shutil.rmtree(item_path, ignore_errors=True)
230
  print(f"Delete folder: {item_path}")
231
  print("Folder cleaned up successfully!")