Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
onnx-export-fix
#13
by echarlaix HF Staff - opened
Files changed (3)
- app.py+21-25
- onnx_export.py+3-1
- requirements.txt+6-2
6 unmodified lines789101112139 unmodified lines232425262728293031323334353637383940414257 unmodified lines1001011021031041051061071081091101111121131141151161171181191201212 unmodified lines1241251261271281291301311321331341351361375 unmodified lines1431441451461471481491501511521531541551561571581591601616 unmodified linesfrom huggingface_hub import HfApi, Repositoryfrom onnx_export import convertfrom apscheduler.schedulers.background import BackgroundSchedulerDATASET_REPO_URL = "https://huggingface.co/datasets/optimum/exporters"9 unmodified lines# repo = Repository(local_dir=DATADIR, clone_from=DATASET_REPO_URL, token=HF_TOKEN)def onnx_export(token: str, model_id: str, task: str, opset: Union[int, str]) -> str:if token == "" or model_id == "":return """### Invalid input 🐞Please fill a token and model name."""try:if opset == "":opset = Noneelse:opset = int(opset)api = HfApi(token=token)error, commit_info = convert(api=api, model_id=model_id, task=task, opset=opset)if error != "0":57 unmodified lines# for some reason https://huggingface.co/settings/tokens is not showing as a link by default?DESCRIPTION = """This Space allows you to automatically export 🤗 transformers PyTorch models hosted on the Hugging Face Hub to [ONNX](https://onnx.ai/). It opens a PR on the target model, and it is up to the owner of the original modelto merge the PR to allow people to leverage the ONNX standard to share and use the model on a wide range of devices!Once exported, the model can, for example, be used in the [🤗 Optimum](https://huggingface.co/docs/optimum/) library closely following the transformers API.Check out [this guide](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/models) to see how!The steps are as following:- Paste a read-access token from [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens). Read access is enough given that we will open a PR against the source repo.- Input a model id from the Hub (for example: [textattack/distilbert-base-cased-CoLA](https://huggingface.co/textattack/distilbert-base-cased-CoLA))- Click "Export to ONNX"- That's it! You'll get feedback on if the export was successful or not, and if it was, you'll get the URL of the opened PR!Note: in case the model to export is larger than 2 GB, it will be saved in a subfolder called `onnx/`. To load it from Optimum, the argument `subfolder="onnx"` should be provided."""with gr.Blocks() as demo:gr.HTML(TTILE_IMAGE)gr.HTML(TITLE)2 unmodified linesgr.Markdown(DESCRIPTION)with gr.Column(scale=50):input_token = gr.Textbox(max_lines=1,label="Hugging Face token",)input_model = gr.Textbox(max_lines=1,label="Model name",placeholder="textattack/distilbert-base-cased-CoLA",)input_task = gr.Textbox(value="auto",5 unmodified linesmax_lines=1,label="ONNX opset (optional, can be left blank)",)btn = gr.Button("Export to ONNX")output = gr.Markdown(label="Output")btn.click(fn=onnx_export,inputs=[input_token, input_model, input_task, onnx_opset],outputs=output,)def restart_space():HfApi().restart_space(repo_id="onnx/export", token=HF_TOKEN, factory_reboot=True)scheduler = BackgroundScheduler()scheduler.add_job(restart_space, "interval", seconds=21600)scheduler.start()6 unmodified lines789101112139 unmodified lines2324252627282930313233343536373839404142434457 unmodified lines1021031041051061071081091101111121131141151161171181191202 unmodified lines1231241251261271281291301311325 unmodified lines1381391401411421431441451461471481491501511521531541551561576 unmodified linesfrom huggingface_hub import HfApi, Repositoryfrom onnx_export import convertfrom gradio_huggingfacehub_search import HuggingfaceHubSearchfrom apscheduler.schedulers.background import BackgroundSchedulerDATASET_REPO_URL = "https://huggingface.co/datasets/optimum/exporters"9 unmodified lines# repo = Repository(local_dir=DATADIR, clone_from=DATASET_REPO_URL, token=HF_TOKEN)def onnx_export(model_id: str, task: str, opset: Union[int, str], oauth_token: gr.OAuthToken) -> str:if oauth_token.token is None:return "You must be logged in to use this space"if not model_id:return f"### Invalid input 🐞 Please specify a model name, got {model_id}"try:if opset == "":opset = Noneelse:opset = int(opset)api = HfApi(token=oauth_token.token)error, commit_info = convert(api=api, model_id=model_id, task=task, opset=opset)if error != "0":57 unmodified lines# for some reason https://huggingface.co/settings/tokens is not showing as a link by default?DESCRIPTION = """This Space allows you to automatically export 🤗 transformers, diffusers, timm and sentence-transformers PyTorch models hosted on the Hugging Face Hub to [ONNX](https://onnx.ai/). It opens a PR on the target model, and it is up to the owner of the original modelto merge the PR to allow people to leverage the ONNX standard to share and use the model on a wide range of devices!Once exported, the model can, for example, be used in the [🤗 Optimum](https://huggingface.co/docs/optimum/) library closely following the transformers API.Check out [this guide](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/models) to see how!Note: in case the model to export is larger than 2 GB, it will be saved in a subfolder called `onnx/`. To load it from Optimum, the argument `subfolder="onnx"` should be provided."""with gr.Blocks() as demo:gr.Markdown("You must be logged to use this space")gr.LoginButton(min_width=250)gr.HTML(TTILE_IMAGE)gr.HTML(TITLE)2 unmodified linesgr.Markdown(DESCRIPTION)with gr.Column(scale=50):input_model = HuggingfaceHubSearch(label="Hub model ID",placeholder="Search for model ID on the hub",search_type="model",)input_task = gr.Textbox(value="auto",5 unmodified linesmax_lines=1,label="ONNX opset (optional, can be left blank)",)btn = gr.Button("Export to ONNX")output = gr.Markdown(label="Output")btn.click(fn=onnx_export,inputs=[input_model, input_task, onnx_opset],outputs=output,)def restart_space():HfApi().restart_space(repo_id="onnx/export", token=HF_TOKEN, factory_reboot=True)scheduler = BackgroundScheduler()scheduler.add_job(restart_space, "interval", seconds=21600)scheduler.start()
81 unmodified lines8283848586878881 unmodified linestry:pr = previous_pr(api, model_id, pr_title)if "model.onnx" in filenames and not force:raise Exception(f"Model {model_id} is already converted, skipping the export.")elif pr is not None and not force:url = f"https://huggingface.co/{model_id}/discussions/{pr.num}"new_pr = pr81 unmodified lines82838485868788899081 unmodified linestry:pr = previous_pr(api, model_id, pr_title)if "model.onnx" in filenames and not force:raise Exception(f"Model {model_id} is already converted, skipping the export.")elif pr is not None and not force:url = f"https://huggingface.co/{model_id}/discussions/{pr.num}"new_pr = pr
12345torch==2.1.2transformers==4.37.2huggingface_hub==0.20.3optimum[onnxruntime,exporters]==1.16.2APScheduler123456789torch==2.1.2huggingface_hub==0.20.3optimum[onnxruntime,exporters]==1.25diffuserssentence-transformerstimmAPSchedulergradio[oauth]>=4.37.2gradio_huggingfacehub_search==0.0.6
app.py+21-25
onnx_export.py+3-1
requirements.txt+6-2