Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 4,825 Bytes
f75daf5 89d7a1e f75daf5 89d7a1e f75daf5 bdeb572 f75daf5 7804c1f 5fc4052 7804c1f f75daf5 bdeb572 f75daf5 6629271 bdeb572 8513f15 f75daf5 03f6f0b f1c3988 d181dd0 f1c3988 d181dd0 476a5d5 03f6f0b bf7786d 595bd49 03f6f0b 476a5d5 bdeb572 f75daf5 595bd49 f75daf5 bdeb572 f75daf5 bf7786d 03f6f0b bf7786d 8513f15 7567dc4 f1c3988 d181dd0 bf7786d d181dd0 bf7786d f75daf5 bf7786d f75daf5 bf7786d f75daf5 7804c1f bdeb572 f75daf5 bf7786d 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
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}"
TTILE = """
<img src="https://huggingface.co/spaces/optimum/exporters/resolve/main/clean_hf_onnx.png" class="center"/>
<div
style="
display: inline-flex;
align-items: center;
text-align: center;
max-width: 1400px;
gap: 0.8rem;
font-size: 2.2rem;
"
>
<h1 style="font-weight: 900; margin-bottom: 10px; margin-top: 10px;">
Convert transformers model to ONNX with π€ Optimum exporters ποΈ (Beta)
</h1>
</div>
"""
# for some reason https://huggingface.co/settings/tokens is not showing as a link by default?
DESCRIPTION = """
This Space allows to automatically convert to ONNX π€ transformers PyTorch 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 [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 "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.
"""
with gr.Blocks() as demo:
gr.HTML(TTILE)
gr.Markdown(DESCRIPTION)
with gr.Column():
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", max_lines=1, label="Task (can be left to \"auto\", will be automatically inferred)")
btn = gr.Button("Convert to ONNX")
output = gr.Markdown(label="Output")
btn.click(fn=onnx_export, inputs=[input_token, input_model, input_task], outputs=output)
"""
demo = gr.Interface(
title="",
description=DESCRIPTION,
allow_flagging="never",
article="Check out the [π€ Optimum repoository on GitHub](https://github.com/huggingface/optimum) as well!",
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() |