fffiloni's picture
Update app.py
1091c7c verified
raw
history blame
2.73 kB
import gradio as gr
import tempfile
import os
from git import Repo
from huggingface_hub import HfApi, create_repo, upload_folder
from huggingface_hub.utils import HfHubHTTPError
api = HfApi()
def process(git_repo_url, space_destination, user_token):
your_username = api.whoami(token=user_token)["name"]
# Check if space ID is correct including username
if str(your_username) in str(space_destination):
print "username is present: good to continue"
else:
space_destination = f"{your_username}/{space_destination}"
print("user forgot to mention his username in space_destination")
# Create a temporary directory to clone the repository
tmp_dir = tempfile.mkdtemp()
# Clone the Hugging Face Spaces repository
repo = Repo.clone_from(git_repo_url, tmp_dir)
# Log the contents of the tmp_dir with its subfolders
print("Contents of the cloned repository:")
for root, dirs, files in os.walk(tmp_dir):
level = root.replace(tmp_dir, '').count(os.sep)
indent = ' ' * 4 * level
print(f"{indent}{os.path.basename(root)}/")
sub_indent = ' ' * 4 * (level + 1)
for f in files:
print(f"{sub_indent}{f}")
try:
api.upload_folder(
folder_path=tmp_dir,
repo_id=space_destination,
repo_type="space",
token=user_token
)
return "Done"
except HfHubHTTPError as e:
# Check if the error is a "Repository Not Found" error
if "404 Client Error" in str(e) and "Repository Not Found" in str(e):
print("Repository not found. Maybe we should Create the repository...")
return "Repository not found. Maybe we should Create the repository..."
else:
# Re-raise the exception if it's not a "Repository Not Found" error
raise e
css = """
div#col-container{
margin: 0 auto;
max-width: 720px;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("# Clone GitHub repo to Space")
git_repo_url = gr.Textbox(
label="Git repo to clone"
)
with gr.Row():
space_destination = gr.Textbox(
label="Space ID"
)
user_token = gr.Textbox(
label="Write permissions token",
type="password"
)
submit_btn = gr.Button("Clone git repo to my space")
status = gr.Textbox(
label="Status"
)
submit_btn.click(
fn=process,
inputs=[git_repo_url, space_destination, user_token],
outputs=[status]
)
demo.launch(show_error=True)