Spaces:
Runtime error
Runtime error
File size: 3,610 Bytes
f75daf5 89d7a1e f75daf5 89d7a1e f75daf5 bdeb572 f75daf5 7804c1f 5fc4052 7804c1f f75daf5 bdeb572 f75daf5 8ffc66a bdeb572 f75daf5 bdeb572 f75daf5 bdeb572 f75daf5 bdeb572 f75daf5 bdeb572 f75daf5 7804c1f bdeb572 f75daf5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 |
import csv
import datetime
import os
from typing import Optional
import gradio as gr
from onnx_export import convert
from huggingface_hub import HfApi, Repository
DATASET_REPO_URL = "https://huggingface.co/datasets/optimum/exporters"
DATA_FILENAME = "data.csv"
DATA_FILE = os.path.join("data", DATA_FILENAME)
HF_TOKEN = os.environ.get("HF_TOKEN")
repo: Optional[Repository] = None
if HF_TOKEN:
repo = Repository(local_dir="data", clone_from=DATASET_REPO_URL, token=HF_TOKEN)
def onnx_export(token: str, model_id: str, task: str) -> str:
if token == "" or model_id == "":
return """
### Invalid input π
Please fill a token and model_id.
"""
try:
api = HfApi(token=token)
error, commit_info = convert(api=api, model_id=model_id, task=task)
if error != "0":
return error
print("[commit_info]", commit_info)
# save in a private dataset
if repo is not None:
repo.git_pull(rebase=True)
with open(DATA_FILE, "a") as csvfile:
writer = csv.DictWriter(
csvfile, fieldnames=["model_id", "pr_url", "time"]
)
writer.writerow(
{
"model_id": model_id,
"pr_url": commit_info.pr_url,
"time": str(datetime.now()),
}
)
commit_url = repo.push_to_hub()
print("[dataset]", commit_url)
return f"### Success π₯ Yay! This model was successfully converted and a PR was open using your token, here: {commit_info.pr_url}]({commit_info.pr_url})"
except Exception as e:
return f"### Error: {e}"
DESCRIPTION = """
<p align="center">
<img src="clean_hf_onnx.png"/>
</p>
This Space allows to automatically convert to ONNX transformers models hosted on the Hugging Face Hub. It opens a PR on the target model, and it is up to the owner of the original model
to merge the PR to allow people to leverage the ONNX standard to share and use the model on a wide range of devices!
Once converted, the model can for example be used in the [π€ Optimum](https://huggingface.co/docs/optimum/) library following closely the transormers API.
Check out [this guide](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/models) to see how!
The steps are the following:
- Paste a read-access token from hf.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:)
- If necessary, input the task for this model.
- Click "Convert to ONNX"
- That's it! You'll get feedback if it works or not, and if it worked, you'll get the URL of the opened PR!
Note: in case the model to convert 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.
"""
demo = gr.Interface(
title="Convert any PyTorch model to ONNX with π€ Optimum Exporters ποΈ",
description=DESCRIPTION,
allow_flagging="never",
article="Check out the [Optimum repo on GitHub](https://github.com/huggingface/optimum)",
inputs=[
gr.Text(max_lines=1, label="Hugging Face token"),
gr.Text(max_lines=1, label="Model name", placeholder="textattack/distilbert-base-cased-CoLA"),
gr.Text(value="auto", max_lines=1, label="Task (can be left blank, will be automatically inferred)")
],
outputs=[gr.Markdown(label="output")],
fn=onnx_export,
)
demo.launch() |