Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Felix Marty commited on
Commit ·
7804c1f
1
Parent(s): e27b4eb
fix
Browse filesFiles changed (2)
- app.py+14-7
- onnx_export.py+10-4
7 unmodified lines89101112133 unmodified lines171819202122234 unmodified lines28293031323334353614 unmodified lines51525354555657586 unmodified lines65666768697071727374757677787 unmodified linesfrom huggingface_hub import HfApi, RepositoryDATASET_REPO_URL = "https://huggingface.co/datasets/safetensors/conversions"DATA_FILENAME = "data.csv"DATA_FILE = os.path.join("data", DATA_FILENAME)3 unmodified linesrepo: Optional[Repository] = Noneif 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 == "":4 unmodified lines"""try:api = HfApi(token=token)commit_info = convert(api=api, model_id=model_id, task=task)print("[commit_info]", commit_info)"""# save in a private dataset:if repo is not None:14 unmodified linesreturn 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 = """6 unmodified lines"""demo = gr.Interface(title="Convert any model to Safetensors and open a PR",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="your_hf_token"),gr.Text(max_lines=1, label="model_id"),gr.Text(max_lines=1, label="task")],outputs=[gr.Markdown(label="output")],fn=onnx_export,7 unmodified lines891011121314153 unmodified lines192021222324254 unmodified lines3031323334353637383940414243444514 unmodified lines6061626364656 unmodified lines72737475767778798081828384857 unmodified linesfrom huggingface_hub import HfApi, Repository# TODO: save stats about the Space?"""DATASET_REPO_URL = "https://huggingface.co/datasets/safetensors/conversions"DATA_FILENAME = "data.csv"DATA_FILE = os.path.join("data", DATA_FILENAME)3 unmodified linesrepo: Optional[Repository] = Noneif 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 == "":4 unmodified lines"""try:api = HfApi(token=token)error, commit_info = convert(api=api, model_id=model_id, task=task)if error != "":return errorprint("[commit_info]", commit_info)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}""""# save in a private dataset:if repo is not None:14 unmodified linesreturn 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})""""DESCRIPTION = """6 unmodified lines"""demo = gr.Interface(title="POC to convert any PyTorch model to ONNX",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_id"),gr.Text(value="auto", max_lines=1, label="task (can be left blank, will auto-infer)")],outputs=[gr.Markdown(label="output")],fn=onnx_export,
11 unmodified lines121314151617187 unmodified lines2627282930313244 unmodified lines777879808182838485868717 unmodified lines10510610710810911011111 unmodified linesimport shutilimport argparsefrom typing import Optionalfrom huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download, get_repo_discussionsfrom huggingface_hub.file_download import repo_folder_name7 unmodified linesif discussion.status == "open" and discussion.is_pull_request and discussion.title == pr_title:return discussiondef convert_onnx(model_id: str, task: str, folder: str):# Allocate the modelmodel = TasksManager.get_model_from_task(task, model_id, framework="pt")44 unmodified linesreturn operationsdef convert(api: "HfApi", model_id: str, task:str, force: bool=False) -> Optional["CommitInfo"]:pr_title = "Adding ONNX file of this model"info = api.model_info(model_id)filenames = set(s.rfilename for s in info.siblings)with TemporaryDirectory() as d:folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))os.makedirs(folder)17 unmodified lines)finally:shutil.rmtree(folder)return new_prif __name__ == "__main__":11 unmodified lines121314151617187 unmodified lines2627282930313244 unmodified lines777879808182838485868788899091929317 unmodified lines11111211311411511611711 unmodified linesimport shutilimport argparsefrom typing import Optional, Tuple, Listfrom huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download, get_repo_discussionsfrom huggingface_hub.file_download import repo_folder_name7 unmodified linesif discussion.status == "open" and discussion.is_pull_request and discussion.title == pr_title:return discussiondef convert_onnx(model_id: str, task: str, folder: str) -> List:# Allocate the modelmodel = TasksManager.get_model_from_task(task, model_id, framework="pt")44 unmodified linesreturn operationsdef convert(api: "HfApi", model_id: str, task: str, force: bool = False) -> Tuple[int, "CommitInfo"]:pr_title = "Adding ONNX file of this model"info = api.model_info(model_id)filenames = set(s.rfilename for s in info.siblings)if task == "auto":try:task = TasksManager.infer_task_from_model(model_id)except Exception as e:return f"### Error: {e}. Please pass explicitely the task as it could not be infered.", Nonewith TemporaryDirectory() as d:folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))os.makedirs(folder)17 unmodified lines)finally:shutil.rmtree(folder)return "0", new_prif __name__ == "__main__":
app.py+14-7
onnx_export.py+10-4