Spaces:
Sleeping
Sleeping
better isolation
Browse files- .dockerignore +2 -1
- .gitignore +1 -0
- app.py +83 -69
.dockerignore
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
/downloads
|
2 |
/llama.cpp
|
3 |
-
*.gguf
|
|
|
|
1 |
/downloads
|
2 |
/llama.cpp
|
3 |
+
*.gguf
|
4 |
+
/outputs
|
.gitignore
CHANGED
@@ -162,6 +162,7 @@ cython_debug/
|
|
162 |
#.idea/
|
163 |
|
164 |
/downloads
|
|
|
165 |
!/downloads/.keep
|
166 |
/llama.cpp
|
167 |
*.gguf
|
|
|
162 |
#.idea/
|
163 |
|
164 |
/downloads
|
165 |
+
/outputs
|
166 |
!/downloads/.keep
|
167 |
/llama.cpp
|
168 |
*.gguf
|
app.py
CHANGED
@@ -43,76 +43,90 @@ def process_model(peft_model_id: str, q_method: str, private_repo, oauth_token:
|
|
43 |
if not os.path.exists("downloads"):
|
44 |
os.makedirs("downloads")
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
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 |
return (
|
118 |
f'<h1>✅ DONE</h1><br/><br/>Find your repo here: <a href="{new_repo_url}" target="_blank" style="text-decoration:underline">{new_repo_id}</a>'
|
|
|
43 |
if not os.path.exists("downloads"):
|
44 |
os.makedirs("downloads")
|
45 |
|
46 |
+
if not os.path.exists("outputs"):
|
47 |
+
os.makedirs("outputs")
|
48 |
+
|
49 |
+
with tempfile.TemporaryDirectory(dir="outputs") as outputdir:
|
50 |
+
gguf_output_path = Path(outputdir)/gguf_output_name
|
51 |
+
readme_output_path = Path(outputdir)/"README.md"
|
52 |
+
|
53 |
+
with tempfile.TemporaryDirectory(dir="downloads") as tmpdir:
|
54 |
+
# Keep the model name as the dirname so the model name metadata is populated correctly
|
55 |
+
local_dir = Path(tmpdir)/model_name
|
56 |
+
print(local_dir)
|
57 |
+
api.snapshot_download(repo_id=peft_model_id, local_dir=local_dir, local_dir_use_symlinks=False, allow_patterns=dl_pattern)
|
58 |
+
print("Model downloaded successfully!")
|
59 |
+
print(f"Current working directory: {os.getcwd()}")
|
60 |
+
print(f"Model directory contents: {os.listdir(local_dir)}")
|
61 |
+
|
62 |
+
adapter_config_dir = local_dir/"adapter_config.json"
|
63 |
+
if not os.path.exists(adapter_config_dir):
|
64 |
+
raise Exception('adapter_config.json not found. Please ensure the selected repo is a PEFT LoRA model.<br/><br/>If you are converting a model (not a LoRA adapter), please use <a href="https://huggingface.co/spaces/ggml-org/gguf-my-repo" target="_blank" style="text-decoration:underline">GGUF-my-repo</a> instead.')
|
65 |
+
|
66 |
+
result = subprocess.run([
|
67 |
+
"python",
|
68 |
+
f"llama.cpp/{CONVERSION_SCRIPT}",
|
69 |
+
local_dir,
|
70 |
+
"--outtype",
|
71 |
+
q_method.lower(),
|
72 |
+
"--outfile",
|
73 |
+
gguf_output_path,
|
74 |
+
], shell=False, capture_output=True)
|
75 |
+
print(result)
|
76 |
+
if result.returncode != 0:
|
77 |
+
raise Exception(f"Error converting to GGUF {q_method}: {result.stderr}")
|
78 |
+
print("Model converted to GGUF successfully!")
|
79 |
+
print(f"Converted model path: {gguf_output_path}")
|
80 |
+
|
81 |
+
# Create empty repo
|
82 |
+
username = whoami(oauth_token.token)["name"]
|
83 |
+
new_repo_url = api.create_repo(repo_id=f"{username}/{model_name}-{q_method}-GGUF", exist_ok=True, private=private_repo)
|
84 |
+
new_repo_id = new_repo_url.repo_id
|
85 |
+
print("Repo created successfully!", new_repo_url)
|
86 |
+
|
87 |
+
# Upload the GGUF model
|
88 |
+
api.upload_file(
|
89 |
+
path_or_fileobj=gguf_output_path,
|
90 |
+
path_in_repo=gguf_output_name,
|
91 |
+
repo_id=new_repo_id,
|
92 |
+
)
|
93 |
+
print("Uploaded", gguf_output_name)
|
94 |
+
|
95 |
+
try:
|
96 |
+
card = ModelCard.load(peft_model_id, token=oauth_token.token)
|
97 |
+
except:
|
98 |
+
card = ModelCard("")
|
99 |
+
if card.data.tags is None:
|
100 |
+
card.data.tags = []
|
101 |
+
card.data.tags.append("llama-cpp")
|
102 |
+
card.data.tags.append("gguf-my-lora")
|
103 |
+
card.data.base_model = peft_model_id
|
104 |
+
card.text = dedent(
|
105 |
+
f"""
|
106 |
+
# {new_repo_id}
|
107 |
+
This LoRA adapter was converted to GGUF format from [`{peft_model_id}`](https://huggingface.co/{peft_model_id}) via the ggml.ai's [GGUF-my-lora](https://huggingface.co/spaces/ggml-org/gguf-my-lora) space.
|
108 |
+
Refer to the [original adapter repository](https://huggingface.co/{peft_model_id}) for more details.
|
109 |
+
|
110 |
+
## Use with llama.cpp
|
111 |
+
|
112 |
+
```bash
|
113 |
+
# with cli
|
114 |
+
llama-cli -m base_model.gguf --lora {gguf_output_name} (...other args)
|
115 |
+
|
116 |
+
# with server
|
117 |
+
llama-server -m base_model.gguf --lora {gguf_output_name} (...other args)
|
118 |
+
```
|
119 |
+
|
120 |
+
To know more about LoRA usage with llama.cpp server, refer to the [llama.cpp server documentation](https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md).
|
121 |
+
"""
|
122 |
+
)
|
123 |
+
card.save(readme_output_path)
|
124 |
|
125 |
+
api.upload_file(
|
126 |
+
path_or_fileobj=readme_output_path,
|
127 |
+
path_in_repo="README.md",
|
128 |
+
repo_id=new_repo_id,
|
129 |
+
)
|
130 |
|
131 |
return (
|
132 |
f'<h1>✅ DONE</h1><br/><br/>Find your repo here: <a href="{new_repo_url}" target="_blank" style="text-decoration:underline">{new_repo_id}</a>'
|