TensorVizion commited on
Commit
9038112
·
verified ·
1 Parent(s): 3eab6cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -28
app.py CHANGED
@@ -6,24 +6,48 @@ from tempfile import TemporaryDirectory
6
  import gradio as gr
7
 
8
 
9
- def export_to_onnx(model_id: str, task: str, opset: int, progress=gr.Progress()):
 
 
 
 
 
 
 
 
10
  model_id = model_id.strip()
 
11
 
12
  if not model_id:
13
- raise gr.Error("Enter a Hugging Face model ID, for example: distilbert/distilbert-base-uncased.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  if not task:
16
  task = "auto"
17
 
18
- progress(0.05, desc="Preparing export...")
19
-
20
  try:
21
- # Import here so a dependency issue produces a UI error instead of
22
- # preventing the Space from starting.
23
  from optimum.exporters.onnx import main_export
24
 
 
 
25
  with TemporaryDirectory() as temp_dir:
26
- output_dir = Path(temp_dir) / "onnx"
 
27
 
28
  progress(0.15, desc="Downloading model and exporting to ONNX...")
29
 
@@ -34,38 +58,62 @@ def export_to_onnx(model_id: str, task: str, opset: int, progress=gr.Progress())
34
  "opset": int(opset),
35
  }
36
 
37
- # "auto" is handled by not supplying task.
38
  if task == "auto":
39
  export_args.pop("task")
40
 
41
  main_export(**export_args)
42
 
43
- progress(0.85, desc="Creating download archive...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  safe_name = model_id.replace("/", "--").replace("\\", "--")
46
- archive_base = Path(temp_dir) / f"{safe_name}-onnx"
 
47
  zip_path = shutil.make_archive(
48
  base_name=str(archive_base),
49
  format="zip",
50
  root_dir=str(output_dir),
51
  )
52
 
53
- # TemporaryDirectory is deleted after this function returns,
54
- # so copy the ZIP to a persistent Gradio temp location.
55
  final_path = Path("/tmp") / f"{safe_name}-onnx.zip"
56
  shutil.copy2(zip_path, final_path)
57
 
58
- progress(1.0, desc="Finished.")
59
 
60
- return (
61
- f"Successfully exported `{model_id}` to ONNX.",
62
- str(final_path),
63
- )
 
 
64
 
65
  except Exception as error:
66
- raise gr.Error(
67
- f"Export failed: {type(error).__name__}: {error}"
68
- )
69
 
70
 
71
  with gr.Blocks(title="Hugging Face to ONNX") as demo:
@@ -73,9 +121,11 @@ with gr.Blocks(title="Hugging Face to ONNX") as demo:
73
  """
74
  # Hugging Face Model to ONNX
75
 
76
- Enter a public Hugging Face model ID, choose a task, and download its ONNX export.
 
77
 
78
- **Examples:** `distilbert/distilbert-base-uncased`, `google-bert/bert-base-uncased`, or `gpt2`
 
79
  """
80
  )
81
 
@@ -113,6 +163,27 @@ Enter a public Hugging Face model ID, choose a task, and download its ONNX expor
113
  scale=1,
114
  )
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  export_button = gr.Button("Export to ONNX", variant="primary")
117
 
118
  status = gr.Markdown()
@@ -120,18 +191,60 @@ Enter a public Hugging Face model ID, choose a task, and download its ONNX expor
120
 
121
  export_button.click(
122
  fn=export_to_onnx,
123
- inputs=[model_id, task, opset],
 
 
 
 
 
 
 
124
  outputs=[status, download],
125
  )
126
 
127
  gr.Examples(
128
  examples=[
129
- ["distilbert/distilbert-base-uncased", "feature-extraction", 17],
130
- ["google-bert/bert-base-uncased", "feature-extraction", 17],
131
- ["distilbert/distilbert-base-cased-distilled-squad", "question-answering", 17],
132
- ["gpt2", "text-generation", 17],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  ],
134
- inputs=[model_id, task, opset],
135
  )
136
 
137
  if __name__ == "__main__":
 
6
  import gradio as gr
7
 
8
 
9
+ def export_to_onnx(
10
+ model_id: str,
11
+ task: str,
12
+ opset: int,
13
+ push_to_hub: bool,
14
+ repo_id: str,
15
+ private_repo: bool,
16
+ progress=gr.Progress(),
17
+ ):
18
  model_id = model_id.strip()
19
+ repo_id = repo_id.strip()
20
 
21
  if not model_id:
22
+ raise gr.Error(
23
+ "Enter a Hugging Face model ID, for example: "
24
+ "distilbert/distilbert-base-uncased."
25
+ )
26
+
27
+ if push_to_hub and not repo_id:
28
+ raise gr.Error(
29
+ "Enter a destination model repository, for example: "
30
+ "your-username/my-model-onnx."
31
+ )
32
+
33
+ if push_to_hub and not os.getenv("HF_TOKEN"):
34
+ raise gr.Error(
35
+ "This Space does not have an HF_TOKEN secret configured. "
36
+ "Add a Hugging Face write token in Settings → Variables and secrets."
37
+ )
38
 
39
  if not task:
40
  task = "auto"
41
 
 
 
42
  try:
43
+ from huggingface_hub import HfApi
 
44
  from optimum.exporters.onnx import main_export
45
 
46
+ progress(0.05, desc="Preparing export...")
47
+
48
  with TemporaryDirectory() as temp_dir:
49
+ temp_dir = Path(temp_dir)
50
+ output_dir = temp_dir / "onnx"
51
 
52
  progress(0.15, desc="Downloading model and exporting to ONNX...")
53
 
 
58
  "opset": int(opset),
59
  }
60
 
 
61
  if task == "auto":
62
  export_args.pop("task")
63
 
64
  main_export(**export_args)
65
 
66
+ hub_url = None
67
+
68
+ if push_to_hub:
69
+ progress(0.80, desc="Creating or updating Hub repository...")
70
+
71
+ api = HfApi(token=os.environ["HF_TOKEN"])
72
+
73
+ api.create_repo(
74
+ repo_id=repo_id,
75
+ repo_type="model",
76
+ private=private_repo,
77
+ exist_ok=True,
78
+ )
79
+
80
+ api.upload_folder(
81
+ folder_path=str(output_dir),
82
+ repo_id=repo_id,
83
+ repo_type="model",
84
+ commit_message=(
85
+ f"Export {model_id} to ONNX "
86
+ f"(task={task}, opset={int(opset)})"
87
+ ),
88
+ )
89
+
90
+ hub_url = f"https://huggingface.co/{repo_id}"
91
+
92
+ progress(0.90, desc="Creating download archive...")
93
 
94
  safe_name = model_id.replace("/", "--").replace("\\", "--")
95
+ archive_base = temp_dir / f"{safe_name}-onnx"
96
+
97
  zip_path = shutil.make_archive(
98
  base_name=str(archive_base),
99
  format="zip",
100
  root_dir=str(output_dir),
101
  )
102
 
 
 
103
  final_path = Path("/tmp") / f"{safe_name}-onnx.zip"
104
  shutil.copy2(zip_path, final_path)
105
 
106
+ progress(1.0, desc="Finished.")
107
 
108
+ message = f"Successfully exported `{model_id}` to ONNX."
109
+
110
+ if hub_url:
111
+ message += f"\n\nPushed to [{repo_id}]({hub_url})."
112
+
113
+ return message, str(final_path)
114
 
115
  except Exception as error:
116
+ raise gr.Error(f"Export failed: {type(error).__name__}: {error}")
 
 
117
 
118
 
119
  with gr.Blocks(title="Hugging Face to ONNX") as demo:
 
121
  """
122
  # Hugging Face Model to ONNX
123
 
124
+ Enter a public Hugging Face model ID, choose a task, then download the ONNX
125
+ export or optionally push it to a Hugging Face model repository.
126
 
127
+ **Examples:** `distilbert/distilbert-base-uncased`,
128
+ `google-bert/bert-base-uncased`, or `gpt2`
129
  """
130
  )
131
 
 
163
  scale=1,
164
  )
165
 
166
+ gr.Markdown("## Optional Hub upload")
167
+
168
+ with gr.Row():
169
+ push_to_hub = gr.Checkbox(
170
+ label="Push ONNX export to Hugging Face Hub",
171
+ value=False,
172
+ scale=1,
173
+ )
174
+
175
+ repo_id = gr.Textbox(
176
+ label="Destination model repository",
177
+ placeholder="your-username/model-name-onnx",
178
+ scale=3,
179
+ )
180
+
181
+ private_repo = gr.Checkbox(
182
+ label="Private repository",
183
+ value=False,
184
+ scale=1,
185
+ )
186
+
187
  export_button = gr.Button("Export to ONNX", variant="primary")
188
 
189
  status = gr.Markdown()
 
191
 
192
  export_button.click(
193
  fn=export_to_onnx,
194
+ inputs=[
195
+ model_id,
196
+ task,
197
+ opset,
198
+ push_to_hub,
199
+ repo_id,
200
+ private_repo,
201
+ ],
202
  outputs=[status, download],
203
  )
204
 
205
  gr.Examples(
206
  examples=[
207
+ [
208
+ "distilbert/distilbert-base-uncased",
209
+ "feature-extraction",
210
+ 17,
211
+ False,
212
+ "",
213
+ False,
214
+ ],
215
+ [
216
+ "google-bert/bert-base-uncased",
217
+ "feature-extraction",
218
+ 17,
219
+ False,
220
+ "",
221
+ False,
222
+ ],
223
+ [
224
+ "distilbert/distilbert-base-cased-distilled-squad",
225
+ "question-answering",
226
+ 17,
227
+ False,
228
+ "",
229
+ False,
230
+ ],
231
+ [
232
+ "gpt2",
233
+ "text-generation",
234
+ 17,
235
+ False,
236
+ "",
237
+ False,
238
+ ],
239
+ ],
240
+ inputs=[
241
+ model_id,
242
+ task,
243
+ opset,
244
+ push_to_hub,
245
+ repo_id,
246
+ private_repo,
247
  ],
 
248
  )
249
 
250
  if __name__ == "__main__":