Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,18 @@ import os
|
|
2 |
import shutil
|
3 |
import subprocess
|
4 |
import signal
|
|
|
5 |
import gradio as gr
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
7 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
|
|
8 |
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
9 |
from textwrap import dedent
|
10 |
|
11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
@@ -32,7 +40,7 @@ def generate_importance_matrix(model_path, train_data_path):
|
|
32 |
try:
|
33 |
process.wait(timeout=5) # grace period
|
34 |
except subprocess.TimeoutExpired:
|
35 |
-
print("Imatrix proc still didn't term.
|
36 |
process.kill()
|
37 |
|
38 |
os.chdir("..")
|
@@ -78,7 +86,7 @@ def split_upload_model(model_path, repo_id, oauth_token: gr.OAuthToken | None, s
|
|
78 |
|
79 |
print("Sharded model has been uploaded successfully!")
|
80 |
|
81 |
-
def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_repo, train_data_file, split_model, split_max_tensors, split_max_size, oauth_token: gr.
|
82 |
if oauth_token.token is None:
|
83 |
raise ValueError("You must be logged in to use GGUF-my-repo")
|
84 |
model_name = model_id.split('/')[-1]
|
@@ -87,8 +95,11 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
87 |
try:
|
88 |
api = HfApi(token=oauth_token.token)
|
89 |
|
90 |
-
dl_pattern = [
|
91 |
-
|
|
|
|
|
|
|
92 |
|
93 |
pattern = (
|
94 |
"*.safetensors"
|
@@ -103,7 +114,6 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
103 |
)
|
104 |
|
105 |
dl_pattern += pattern
|
106 |
-
dl_pattern += model_types
|
107 |
|
108 |
api.snapshot_download(repo_id=model_id, local_dir=model_name, local_dir_use_symlinks=False, allow_patterns=dl_pattern)
|
109 |
print("Model downloaded successfully!")
|
@@ -125,7 +135,7 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
125 |
if train_data_file:
|
126 |
train_data_path = train_data_file.name
|
127 |
else:
|
128 |
-
train_data_path = "groups_merged.txt"
|
129 |
|
130 |
print(f"Training data file path: {train_data_path}")
|
131 |
|
@@ -135,13 +145,16 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
135 |
generate_importance_matrix(fp16, train_data_path)
|
136 |
else:
|
137 |
print("Not using imatrix quantization.")
|
|
|
138 |
username = whoami(oauth_token.token)["name"]
|
139 |
quantized_gguf_name = f"{model_name.lower()}-{imatrix_q_method.lower()}-imat.gguf" if use_imatrix else f"{model_name.lower()}-{q_method.lower()}.gguf"
|
140 |
quantized_gguf_path = quantized_gguf_name
|
|
|
141 |
if use_imatrix:
|
142 |
quantise_ggml = f"./llama.cpp/llama-quantize --imatrix {imatrix_path} {fp16} {quantized_gguf_path} {imatrix_q_method}"
|
143 |
else:
|
144 |
quantise_ggml = f"./llama.cpp/llama-quantize {fp16} {quantized_gguf_path} {q_method}"
|
|
|
145 |
result = subprocess.run(quantise_ggml, shell=True, capture_output=True)
|
146 |
if result.returncode != 0:
|
147 |
raise Exception(f"Error quantizing: {result.stderr}")
|
@@ -196,77 +209,175 @@ def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_rep
|
|
196 |
```
|
197 |
cd llama.cpp && LLAMA_CURL=1 make
|
198 |
```
|
199 |
-
Step 3:
|
200 |
```
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
203 |
```
|
204 |
-
|
205 |
-
## Additional Notes:
|
206 |
-
To gain higher performance, ensure that you have aligned on llama.cpp's threading tips by having your CPU fully utilized and setting threads dynamically using `OMP_NUM_THREADS`.
|
207 |
"""
|
208 |
)
|
209 |
-
card.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
api.upload_file(
|
211 |
-
path_or_fileobj=
|
212 |
-
path_in_repo=
|
213 |
repo_id=new_repo_id,
|
214 |
)
|
215 |
-
if
|
216 |
-
split_upload_model(quantized_gguf_path, new_repo_id, oauth_token, split_max_tensors=split_max_tensors, split_max_size=split_max_size)
|
217 |
-
else:
|
218 |
-
print("Model split skipped by user.")
|
219 |
|
220 |
-
|
|
|
|
|
|
|
221 |
except Exception as e:
|
222 |
-
|
223 |
-
return False, str(e)
|
224 |
finally:
|
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 |
-
scheduler.start()
|
268 |
|
269 |
-
|
270 |
-
|
|
|
271 |
|
272 |
-
|
|
|
|
2 |
import shutil
|
3 |
import subprocess
|
4 |
import signal
|
5 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
6 |
import gradio as gr
|
7 |
+
|
8 |
+
from huggingface_hub import create_repo, HfApi
|
9 |
+
from huggingface_hub import snapshot_download
|
10 |
+
from huggingface_hub import whoami
|
11 |
+
from huggingface_hub import ModelCard
|
12 |
+
|
13 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
14 |
+
|
15 |
from apscheduler.schedulers.background import BackgroundScheduler
|
16 |
+
|
17 |
from textwrap import dedent
|
18 |
|
19 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
40 |
try:
|
41 |
process.wait(timeout=5) # grace period
|
42 |
except subprocess.TimeoutExpired:
|
43 |
+
print("Imatrix proc still didn't term. Forecfully terming process...")
|
44 |
process.kill()
|
45 |
|
46 |
os.chdir("..")
|
|
|
86 |
|
87 |
print("Sharded model has been uploaded successfully!")
|
88 |
|
89 |
+
def process_model(model_id, q_method, use_imatrix, imatrix_q_method, private_repo, train_data_file, split_model, split_max_tensors, split_max_size, oauth_token: gr.OAuthToken | None):
|
90 |
if oauth_token.token is None:
|
91 |
raise ValueError("You must be logged in to use GGUF-my-repo")
|
92 |
model_name = model_id.split('/')[-1]
|
|
|
95 |
try:
|
96 |
api = HfApi(token=oauth_token.token)
|
97 |
|
98 |
+
dl_pattern = [
|
99 |
+
"*.safetensors", "*.bin", "*.pt", "*.onnx", "*.h5", "*.tflite",
|
100 |
+
"*.ckpt", "*.pb", "*.tar", "*.xml", "*.caffemodel",
|
101 |
+
"*.md", "*.json", "*.model"
|
102 |
+
]
|
103 |
|
104 |
pattern = (
|
105 |
"*.safetensors"
|
|
|
114 |
)
|
115 |
|
116 |
dl_pattern += pattern
|
|
|
117 |
|
118 |
api.snapshot_download(repo_id=model_id, local_dir=model_name, local_dir_use_symlinks=False, allow_patterns=dl_pattern)
|
119 |
print("Model downloaded successfully!")
|
|
|
135 |
if train_data_file:
|
136 |
train_data_path = train_data_file.name
|
137 |
else:
|
138 |
+
train_data_path = "groups_merged.txt" # fallback calibration dataset
|
139 |
|
140 |
print(f"Training data file path: {train_data_path}")
|
141 |
|
|
|
145 |
generate_importance_matrix(fp16, train_data_path)
|
146 |
else:
|
147 |
print("Not using imatrix quantization.")
|
148 |
+
|
149 |
username = whoami(oauth_token.token)["name"]
|
150 |
quantized_gguf_name = f"{model_name.lower()}-{imatrix_q_method.lower()}-imat.gguf" if use_imatrix else f"{model_name.lower()}-{q_method.lower()}.gguf"
|
151 |
quantized_gguf_path = quantized_gguf_name
|
152 |
+
|
153 |
if use_imatrix:
|
154 |
quantise_ggml = f"./llama.cpp/llama-quantize --imatrix {imatrix_path} {fp16} {quantized_gguf_path} {imatrix_q_method}"
|
155 |
else:
|
156 |
quantise_ggml = f"./llama.cpp/llama-quantize {fp16} {quantized_gguf_path} {q_method}"
|
157 |
+
|
158 |
result = subprocess.run(quantise_ggml, shell=True, capture_output=True)
|
159 |
if result.returncode != 0:
|
160 |
raise Exception(f"Error quantizing: {result.stderr}")
|
|
|
209 |
```
|
210 |
cd llama.cpp && LLAMA_CURL=1 make
|
211 |
```
|
212 |
+
Step 3: Run inference through the main binary.
|
213 |
```
|
214 |
+
./llama-cli --hf-repo {new_repo_id} --hf-file {quantized_gguf_name} -p "The meaning to life and the universe is"
|
215 |
+
```
|
216 |
+
or
|
217 |
+
```
|
218 |
+
./llama-server --hf-repo {new_repo_id} --hf-file {quantized_gguf_name} -c 2048
|
219 |
```
|
|
|
|
|
|
|
220 |
"""
|
221 |
)
|
222 |
+
card.save(f"README.md")
|
223 |
+
|
224 |
+
if split_model:
|
225 |
+
split_upload_model(quantized_gguf_path, new_repo_id, oauth_token, split_max_tensors, split_max_size)
|
226 |
+
else:
|
227 |
+
try:
|
228 |
+
print(f"Uploading quantized model: {quantized_gguf_path}")
|
229 |
+
api.upload_file(
|
230 |
+
path_or_fileobj=quantized_gguf_path,
|
231 |
+
path_in_repo=quantized_gguf_name,
|
232 |
+
repo_id=new_repo_id,
|
233 |
+
)
|
234 |
+
except Exception as e:
|
235 |
+
raise Exception(f"Error uploading quantized model: {e}")
|
236 |
+
|
237 |
+
imatrix_path = "llama.cpp/imatrix.dat"
|
238 |
+
if os.path.isfile(imatrix_path):
|
239 |
+
try:
|
240 |
+
print(f"Uploading imatrix.dat: {imatrix_path}")
|
241 |
+
api.upload_file(
|
242 |
+
path_or_fileobj=imatrix_path,
|
243 |
+
path_in_repo="imatrix.dat",
|
244 |
+
repo_id=new_repo_id,
|
245 |
+
)
|
246 |
+
except Exception as e:
|
247 |
+
raise Exception(f"Error uploading imatrix.dat: {e}")
|
248 |
+
|
249 |
api.upload_file(
|
250 |
+
path_or_fileobj=f"README.md",
|
251 |
+
path_in_repo=f"README.md",
|
252 |
repo_id=new_repo_id,
|
253 |
)
|
254 |
+
print(f"Uploaded successfully with {imatrix_q_method if use_imatrix else q_method} option!")
|
|
|
|
|
|
|
255 |
|
256 |
+
return (
|
257 |
+
f'Find your repo <a href=\'{new_repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
|
258 |
+
"llama.png",
|
259 |
+
)
|
260 |
except Exception as e:
|
261 |
+
return (f"Error: {e}", "error.png")
|
|
|
262 |
finally:
|
263 |
+
shutil.rmtree(model_name, ignore_errors=True)
|
264 |
+
print("Folder cleaned up successfully!")
|
265 |
+
|
266 |
+
css="""/* Custom CSS to allow scrolling */
|
267 |
+
.gradio-container {overflow-y: auto;}
|
268 |
+
"""
|
269 |
+
# Create Gradio interface
|
270 |
+
with gr.Blocks(css=css) as demo:
|
271 |
+
gr.Markdown("You must be logged in to use GGUF-my-repo.")
|
272 |
+
gr.LoginButton(min_width=250)
|
273 |
+
|
274 |
+
model_id = HuggingfaceHubSearch(
|
275 |
+
label="Hub Model ID",
|
276 |
+
placeholder="Search for model id on Huggingface",
|
277 |
+
search_type="model",
|
278 |
+
)
|
279 |
+
|
280 |
+
q_method = gr.Dropdown(
|
281 |
+
["Q2_K", "Q3_K_S", "Q3_K_M", "Q3_K_L", "Q4_0", "Q4_K_S", "Q4_K_M", "Q5_0", "Q5_K_S", "Q5_K_M", "Q6_K", "Q8_0"],
|
282 |
+
label="Quantization Method",
|
283 |
+
info="GGML quantization type",
|
284 |
+
value="Q4_K_M",
|
285 |
+
filterable=False,
|
286 |
+
visible=True
|
287 |
+
)
|
288 |
+
|
289 |
+
imatrix_q_method = gr.Dropdown(
|
290 |
+
["IQ3_M", "IQ3_XXS", "Q4_K_M", "Q4_K_S", "IQ4_NL", "IQ4_XS", "Q5_K_M", "Q5_K_S"],
|
291 |
+
label="Imatrix Quantization Method",
|
292 |
+
info="GGML imatrix quants type",
|
293 |
+
value="IQ4_NL",
|
294 |
+
filterable=False,
|
295 |
+
visible=False
|
296 |
+
)
|
297 |
+
|
298 |
+
use_imatrix = gr.Checkbox(
|
299 |
+
value=False,
|
300 |
+
label="Use Imatrix Quantization",
|
301 |
+
info="Use importance matrix for quantization."
|
302 |
+
)
|
303 |
+
|
304 |
+
private_repo = gr.Checkbox(
|
305 |
+
value=False,
|
306 |
+
label="Private Repo",
|
307 |
+
info="Create a private repo under your username."
|
308 |
+
)
|
309 |
+
|
310 |
+
train_data_file = gr.File(
|
311 |
+
label="Training Data File",
|
312 |
+
file_types=["txt"],
|
313 |
+
visible=False
|
314 |
+
)
|
315 |
+
|
316 |
+
split_model = gr.Checkbox(
|
317 |
+
value=False,
|
318 |
+
label="Split Model",
|
319 |
+
info="Shard the model using gguf-split."
|
320 |
+
)
|
321 |
+
|
322 |
+
split_max_tensors = gr.Number(
|
323 |
+
value=256,
|
324 |
+
label="Max Tensors per File",
|
325 |
+
info="Maximum number of tensors per file when splitting model.",
|
326 |
+
visible=False
|
327 |
+
)
|
328 |
+
|
329 |
+
split_max_size = gr.Textbox(
|
330 |
+
label="Max File Size",
|
331 |
+
info="Maximum file size when splitting model (--split-max-size). May leave empty to use the default.",
|
332 |
+
visible=False
|
333 |
+
)
|
334 |
+
|
335 |
+
def update_visibility(use_imatrix):
|
336 |
+
return gr.update(visible=not use_imatrix), gr.update(visible=use_imatrix), gr.update(visible=use_imatrix)
|
337 |
+
|
338 |
+
use_imatrix.change(
|
339 |
+
fn=update_visibility,
|
340 |
+
inputs=use_imatrix,
|
341 |
+
outputs=[q_method, imatrix_q_method, train_data_file]
|
342 |
+
)
|
343 |
+
|
344 |
+
iface = gr.Interface(
|
345 |
+
fn=process_model,
|
346 |
+
inputs=[
|
347 |
+
model_id,
|
348 |
+
q_method,
|
349 |
+
use_imatrix,
|
350 |
+
imatrix_q_method,
|
351 |
+
private_repo,
|
352 |
+
train_data_file,
|
353 |
+
split_model,
|
354 |
+
split_max_tensors,
|
355 |
+
split_max_size,
|
356 |
+
],
|
357 |
+
outputs=[
|
358 |
+
gr.Markdown(label="output"),
|
359 |
+
gr.Image(show_label=False),
|
360 |
+
],
|
361 |
+
title="Create your own GGUF Quants, blazingly fast ⚡!",
|
362 |
+
description="The space takes an HF repo as an input, quantizes it and creates a Public repo containing the selected quant under your HF user namespace.",
|
363 |
+
api_name=False
|
364 |
+
)
|
365 |
+
|
366 |
+
def update_split_visibility(split_model):
|
367 |
+
return gr.update(visible=split_model), gr.update(visible=split_model)
|
368 |
+
|
369 |
+
split_model.change(
|
370 |
+
fn=update_split_visibility,
|
371 |
+
inputs=split_model,
|
372 |
+
outputs=[split_max_tensors, split_max_size]
|
373 |
+
)
|
374 |
|
375 |
+
def restart_space():
|
376 |
+
HfApi().restart_space(repo_id="ggml-org/gguf-my-repo", token=HF_TOKEN, factory_reboot=True)
|
|
|
377 |
|
378 |
+
scheduler = BackgroundScheduler()
|
379 |
+
scheduler.add_job(restart_space, "interval", seconds=21600)
|
380 |
+
scheduler.start()
|
381 |
|
382 |
+
# Launch the interface
|
383 |
+
demo.queue(default_concurrency_limit=1, max_size=5).launch(debug=True, show_api=False)
|