silveroxides commited on
Commit
13e5408
·
verified ·
1 Parent(s): de0e007

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +53 -35
app.py CHANGED
@@ -17,20 +17,17 @@ def run_quantization(
17
  layer_filter,
18
  exclude_layers_regex,
19
  full_precision_matrix_mult,
20
- oauth_token: gr.OAuthToken | None = None
21
  ):
22
- if not oauth_token:
23
- yield "Please sign in with Hugging Face using the login button in the top right.", ""
24
- return
25
-
26
  if not all([source_repo, source_file, target_repo, target_filename_base]):
27
- yield "Please fill in all repository and filename fields.", ""
28
  return
29
 
30
  try:
31
  # Download
32
- yield f"Downloading {source_file} from {source_repo}...", ""
33
- local_input_path = hf_hub_download(repo_id=source_repo, filename=source_file, token=oauth_token.token)
 
34
 
35
  # Setup quant arguments based on UI
36
  quant_args = {
@@ -74,40 +71,45 @@ def run_quantization(
74
  if exclude_layers_regex:
75
  quant_args["exclude-layers"] = exclude_layers_regex
76
 
77
- yield f"Quantizing to {output_filename}...\nThis may take a few minutes.", ""
78
 
79
- # We need to print output or capture it, convert_to_quant probably prints. We'll just run it.
80
  do_quantize(quant_args)
81
 
82
- yield f"Uploading {output_filename} to {target_repo}...", ""
83
-
84
- # Upload
85
- api = HfApi(token=oauth_token.token)
86
-
87
- commit_info = api.upload_file(
88
- path_or_fileobj=output_path,
89
- path_in_repo=output_filename,
90
- repo_id=target_repo,
91
- commit_message=f"Add {output_filename} quantized model",
92
- create_pr=True
93
- )
94
-
95
- pr_url = commit_info.pr_url if hasattr(commit_info, 'pr_url') else f"https://huggingface.co/{target_repo}"
96
-
97
- yield f"Complete! Uploaded to {target_repo}", f"<a href='{pr_url}' target='_blank' style='color: #3b82f6; text-decoration: underline; font-weight: bold;'>Click here to view the Pull Request</a>"
 
 
 
98
 
99
  except Exception as e:
100
- yield f"Error: {str(e)}", ""
101
 
102
 
103
  # Build UI
104
- with gr.Blocks(css_paths=["assets/responsive.css"], theme=gr.themes.Soft()) as demo:
105
  with gr.Row(elem_id="topbar"):
106
  gr.Markdown("## 🤗 Model Quantizer", elem_classes=["brand"])
107
- gr.LoginButton()
108
 
109
  with gr.Row(elem_id="main-row", equal_height=True):
110
  with gr.Column(scale=4, min_width=280, elem_id="input-panel"):
 
 
 
 
111
  gr.Markdown("### Input Model")
112
  source_repo = gr.Textbox(label="Source HF Repo (e.g. author/model)")
113
  source_file = gr.Textbox(label="Source Filename (e.g. model.safetensors)")
@@ -131,22 +133,38 @@ with gr.Blocks(css_paths=["assets/responsive.css"], theme=gr.themes.Soft()) as d
131
 
132
  exclude_layers = gr.Textbox(label="Exclude Layers Regex (Optional)", placeholder="(substring_1|substring_2)")
133
 
134
- run_btn = gr.Button("Quantize & Upload PR", variant="primary", size="lg")
135
 
136
  gr.Markdown("ℹ️ *For more advanced quantization modes, install and use [convert-to-quant](https://pypi.org/project/convert-to-quant/) locally.*", elem_classes=["text-sm", "mt-4"])
137
 
138
  with gr.Column(scale=8, elem_id="output-panel"):
139
  status_text = gr.Textbox(label="Status Log", lines=10, interactive=False)
140
- pr_link = gr.HTML("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
  run_btn.click(
143
- fn=run_quantization,
144
  inputs=[
145
  source_repo, source_file, target_repo, target_file_base,
146
- quant_format, layer_filter, exclude_layers, full_precision
 
147
  ],
148
- outputs=[status_text, pr_link]
149
  )
150
 
151
  if __name__ == "__main__":
152
- demo.launch()
 
17
  layer_filter,
18
  exclude_layers_regex,
19
  full_precision_matrix_mult,
20
+ hf_token
21
  ):
 
 
 
 
22
  if not all([source_repo, source_file, target_repo, target_filename_base]):
23
+ yield "Please fill in all repository and filename fields.", gr.update(visible=False)
24
  return
25
 
26
  try:
27
  # Download
28
+ yield f"Downloading {source_file} from {source_repo}...", gr.update(visible=False)
29
+ # We use token for download if provided, otherwise anonymous
30
+ local_input_path = hf_hub_download(repo_id=source_repo, filename=source_file, token=hf_token if hf_token else None)
31
 
32
  # Setup quant arguments based on UI
33
  quant_args = {
 
71
  if exclude_layers_regex:
72
  quant_args["exclude-layers"] = exclude_layers_regex
73
 
74
+ yield f"Quantizing to {output_filename}...\nThis may take a few minutes.", gr.update(visible=False)
75
 
 
76
  do_quantize(quant_args)
77
 
78
+ if hf_token:
79
+ yield f"Uploading {output_filename} to {target_repo}...", gr.update(visible=False)
80
+
81
+ # Upload
82
+ api = HfApi(token=hf_token)
83
+
84
+ commit_info = api.upload_file(
85
+ path_or_fileobj=output_path,
86
+ path_in_repo=output_filename,
87
+ repo_id=target_repo,
88
+ commit_message=f"Add {output_filename} quantized model",
89
+ create_pr=True
90
+ )
91
+
92
+ pr_url = commit_info.pr_url if hasattr(commit_info, 'pr_url') else f"https://huggingface.co/{target_repo}"
93
+
94
+ yield f"Complete! Uploaded to {target_repo}", gr.update(value=f"<a href='{pr_url}' target='_blank' style='color: #3b82f6; text-decoration: underline; font-weight: bold;'>Click here to view the Pull Request</a>", visible=True)
95
+ else:
96
+ yield f"Complete! Ready for download below.", gr.update(value=output_path, visible=True)
97
 
98
  except Exception as e:
99
+ yield f"Error: {str(e)}", gr.update(visible=False)
100
 
101
 
102
  # Build UI
103
+ with gr.Blocks() as demo:
104
  with gr.Row(elem_id="topbar"):
105
  gr.Markdown("## 🤗 Model Quantizer", elem_classes=["brand"])
 
106
 
107
  with gr.Row(elem_id="main-row", equal_height=True):
108
  with gr.Column(scale=4, min_width=280, elem_id="input-panel"):
109
+ gr.Markdown("### Authentication (Optional)")
110
+ hf_token = gr.Textbox(label="HF Token (WRITE)", type="password", placeholder="Paste your WRITE token for PR upload")
111
+ gr.Markdown("*If no token is provided, the quantized model will be available for direct download instead of uploading as a PR.*", elem_classes=["text-sm"])
112
+
113
  gr.Markdown("### Input Model")
114
  source_repo = gr.Textbox(label="Source HF Repo (e.g. author/model)")
115
  source_file = gr.Textbox(label="Source Filename (e.g. model.safetensors)")
 
133
 
134
  exclude_layers = gr.Textbox(label="Exclude Layers Regex (Optional)", placeholder="(substring_1|substring_2)")
135
 
136
+ run_btn = gr.Button("Quantize Model", variant="primary", size="lg")
137
 
138
  gr.Markdown("ℹ️ *For more advanced quantization modes, install and use [convert-to-quant](https://pypi.org/project/convert-to-quant/) locally.*", elem_classes=["text-sm", "mt-4"])
139
 
140
  with gr.Column(scale=8, elem_id="output-panel"):
141
  status_text = gr.Textbox(label="Status Log", lines=10, interactive=False)
142
+ output_link = gr.HTML(visible=False)
143
+ output_file = gr.File(label="Download Quantized Model", visible=False)
144
+
145
+ def route_output(*args):
146
+ # We handle routing the result to HTML vs File based on whether token is provided.
147
+ # However, it's easier to just yield both appropriately.
148
+ for status, result in run_quantization(*args):
149
+ # If the result is a string with <a href=, it's HTML. Otherwise it's a file path.
150
+ if result and isinstance(result, dict) and "value" in result:
151
+ val = result["value"]
152
+ if isinstance(val, str) and val.startswith("<a"):
153
+ yield status, result, gr.update(visible=False)
154
+ else:
155
+ yield status, gr.update(visible=False), result
156
+ else:
157
+ yield status, gr.update(visible=False), gr.update(visible=False)
158
 
159
  run_btn.click(
160
+ fn=route_output,
161
  inputs=[
162
  source_repo, source_file, target_repo, target_file_base,
163
+ quant_format, layer_filter, exclude_layers, full_precision,
164
+ hf_token
165
  ],
166
+ outputs=[status_text, output_link, output_file]
167
  )
168
 
169
  if __name__ == "__main__":
170
+ demo.launch(css_paths=["assets/responsive.css"], theme=gr.themes.Soft())