import gradio as gr import subprocess import os from huggingface_hub import whoami, HfApi, login import random import time api = HfApi() REPO_TYPES = ["model", "dataset", "space"] def upload_model(source_url, dst_repo, token,civitai_api_key, new_name, dst_repo_path, repo_type): try: _ = whoami(token) # Check token validity # Check if destination path ends with '/' if not dst_repo_path.endswith('/'): raise Exception("Your destination path must end with a /") # Check if source URL is provided if not source_url: raise Exception("You haven't provided a source URL for the model.") # Check if destination repository is provided if not dst_repo: raise Exception("You haven't provided a destination repository.") if not civitai_api_key: raise Exception("You haven't provided a Civitai Api Key.") # Login to Hugging Face Hub login(token=token) # Create a directory to store the downloaded model download_dir = "/home/user/apps/downloads/" + str(int(time.time())) + str(random.getrandbits(8)) + "/" subprocess.check_call(["mkdir", "-p", download_dir]) # Download the model using wget subprocess.check_call(["wget", f"{source_url}?token={civitai_api_key}", "-P", download_dir]) # List files in the download directory files = os.listdir(download_dir) # Set the destination path for the model if new_name: dst_repo_path = dst_repo_path.strip("/") + "/" + new_name.strip("/") else: dst_repo_path = dst_repo_path.strip("/") + "/" + files[0] # Upload the model file to the destination repository api.upload_file( path_or_fileobj=download_dir + files[0], path_in_repo=dst_repo_path, repo_id=dst_repo, repo_type=repo_type ) # Clean up: remove downloaded file and directory os.remove(download_dir + files[0]) os.rmdir(download_dir) # Generate the URL of the uploaded model if repo_type == "space": repo_url = f"https://huggingface.co/spaces/{dst_repo}" elif repo_type == "dataset": repo_url = f"https://huggingface.co/datasets/{dst_repo}" else: # Assuming repo_type is "model" repo_url = f"https://huggingface.co/{dst_repo}" return ( f'Your model has been successfully uploaded to your Hugging Face repository.', "success.jpg", ) except Exception as e: # Handle exceptions and provide an error message blames = ["grandma", "my boss", "your boss", "God", "you", "you. It's *all* your fault.", "the pope"] blameweights = (1, 1, 1, 1, 4, 2, 1) excuses = ["I blame it all on " + random.choices(blames, weights=blameweights)[0], "It's my fault, sorry.", "I did it on purpose.", "That file doesn't want to be downloaded.", "You nincompoop!"] excusesweights = (12, 1, 1, 2, 3) excuse = random.choices(excuses, weights=excusesweights)[0] return ( f""" ### Error 😢😢😢 {e} """ + excuse + "", None, ) # Interface setup interface = gr.Interface( fn=upload_model, inputs=[ gr.Textbox(placeholder="Source URL Civitai (e.g. https://civitai.com/api/download/models/486156?type=Model&format=SafeTensor)"), gr.Textbox(placeholder="Destination repository HF (e.g. username/repo-name)"), gr.Textbox(placeholder="Write access token HF", type="password"), gr.Textbox(placeholder="Civitai Api Key", type="password"), gr.Textbox(placeholder="New name for the model file (optional)"), gr.Textbox(placeholder="Destination path within your repository (e.g. /models/Stable-diffusion/)"), gr.Dropdown(choices=REPO_TYPES, value="model"), ], outputs=[ gr.Markdown(label="Output"), gr.Image(show_label=False), ], title="Upload a Civitai Model to Hugging Face Repository", description="Upload a model file to your Hugging Face repository. Provide the source URL of the model, your repository information, and your write access token.", article="
Find your write token at token settings
", allow_flagging="never", live=False, # Prevents automatic re-runs on input change )