import csv import os from datetime import datetime from typing import Optional, Union import gradio as gr from huggingface_hub import HfApi, Repository from export import convert from gradio_huggingfacehub_search import HuggingfaceHubSearch DATASET_REPO_URL = "https://huggingface.co/datasets/optimum/exporters" DATA_FILENAME = "data.csv" DATA_FILE = os.path.join("openvino", DATA_FILENAME) HF_TOKEN = os.environ.get("HF_WRITE_TOKEN") DATA_DIR = "exporters_data" repo = None if HF_TOKEN: repo = Repository(local_dir=DATA_DIR, clone_from=DATASET_REPO_URL, token=HF_TOKEN) def export(model_id: str, task: str, oauth_token: gr.OAuthToken) -> str: if oauth_token.token is None: raise ValueError("You must be logged in to use this space") token = oauth_token.token if model_id == "" or token == "": return """ ### Invalid input 🐞 Please fill a token and model name. """ try: api = HfApi(token=token) error, commit_info = convert(api=api, model_id=model_id, task=task, force=False) 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(os.path.join(DATA_DIR, 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 exported 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_IMAGE = """