ngxson HF staff commited on
Commit
a84add4
1 Parent(s): 798439c

better isolation

Browse files
Files changed (3) hide show
  1. .dockerignore +2 -1
  2. .gitignore +1 -0
  3. 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
- with tempfile.TemporaryDirectory(dir="downloads") as tmpdir:
47
- # Keep the model name as the dirname so the model name metadata is populated correctly
48
- local_dir = Path(tmpdir)/model_name
49
- print(local_dir)
50
- api.snapshot_download(repo_id=peft_model_id, local_dir=local_dir, local_dir_use_symlinks=False, allow_patterns=dl_pattern)
51
- print("Model downloaded successfully!")
52
- print(f"Current working directory: {os.getcwd()}")
53
- print(f"Model directory contents: {os.listdir(local_dir)}")
54
-
55
- adapter_config_dir = local_dir/"adapter_config.json"
56
- if not os.path.exists(adapter_config_dir):
57
- 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.')
58
-
59
- fp16_conversion = f"python llama.cpp/{CONVERSION_SCRIPT} {local_dir} --outtype {q_method.lower()} --outfile {gguf_output_name}"
60
- result = subprocess.run(fp16_conversion, shell=True, capture_output=True)
61
- print(result)
62
- if result.returncode != 0:
63
- raise Exception(f"Error converting to GGUF {q_method}: {result.stderr}")
64
- print("Model converted to GGUF successfully!")
65
- print(f"Converted model path: {gguf_output_name}")
66
-
67
- # Create empty repo
68
- username = whoami(oauth_token.token)["name"]
69
- new_repo_url = api.create_repo(repo_id=f"{username}/{model_name}-{q_method}-GGUF", exist_ok=True, private=private_repo)
70
- new_repo_id = new_repo_url.repo_id
71
- print("Repo created successfully!", new_repo_url)
72
-
73
- # Upload the GGUF model
74
- api.upload_file(
75
- path_or_fileobj=gguf_output_name,
76
- path_in_repo=gguf_output_name,
77
- repo_id=new_repo_id,
78
- )
79
- print("Uploaded", gguf_output_name)
80
-
81
- try:
82
- card = ModelCard.load(peft_model_id, token=oauth_token.token)
83
- except:
84
- card = ModelCard("")
85
- if card.data.tags is None:
86
- card.data.tags = []
87
- card.data.tags.append("llama-cpp")
88
- card.data.tags.append("gguf-my-lora")
89
- card.data.base_model = peft_model_id
90
- card.text = dedent(
91
- f"""
92
- # {new_repo_id}
93
- 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.
94
- Refer to the [original adapter repository](https://huggingface.co/{peft_model_id}) for more details.
95
-
96
- ## Use with llama.cpp
97
-
98
- ```bash
99
- # with cli
100
- llama-cli -m base_model.gguf --lora {gguf_output_name} (...other args)
101
-
102
- # with server
103
- llama-server -m base_model.gguf --lora {gguf_output_name} (...other args)
104
- ```
105
-
106
- 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).
107
- """
108
- )
109
- card.save(f"README.md")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- api.upload_file(
112
- path_or_fileobj=f"README.md",
113
- path_in_repo=f"README.md",
114
- repo_id=new_repo_id,
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>'