Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,24 +5,25 @@ import shutil
|
|
| 5 |
|
| 6 |
def download_and_upload(token, source_repo_id, repo_type, target_repo_id, path_in_repo=""):
|
| 7 |
try:
|
| 8 |
-
# Use HF_TOKEN secret from environment if
|
| 9 |
if token.strip() == "":
|
| 10 |
token = os.environ.get("HF_TOKEN")
|
| 11 |
-
|
| 12 |
if not token:
|
| 13 |
-
return "β Token missing.
|
| 14 |
|
| 15 |
-
# Login
|
| 16 |
login(token=token)
|
| 17 |
api = HfApi()
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
| 20 |
if repo_type == "model":
|
| 21 |
path = snapshot_download(repo_id=source_repo_id)
|
| 22 |
else:
|
| 23 |
path = snapshot_download(repo_id=source_repo_id, repo_type=repo_type)
|
| 24 |
|
| 25 |
-
# Upload
|
| 26 |
api.upload_folder(
|
| 27 |
folder_path=path,
|
| 28 |
repo_id=target_repo_id,
|
|
@@ -30,15 +31,12 @@ def download_and_upload(token, source_repo_id, repo_type, target_repo_id, path_i
|
|
| 30 |
path_in_repo=path_in_repo,
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
if os.path.exists(path):
|
| 35 |
-
shutil.rmtree(path)
|
| 36 |
-
|
| 37 |
-
return f"β
Downloaded from {source_repo_id}, uploaded to {target_repo_id}, and cleaned up local files."
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
return f"β Failed: {e}"
|
| 41 |
|
|
|
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("## π€ Auto Download & Upload HF Repo (With Auto-Cleanup)")
|
| 44 |
|
|
|
|
| 5 |
|
| 6 |
def download_and_upload(token, source_repo_id, repo_type, target_repo_id, path_in_repo=""):
|
| 7 |
try:
|
| 8 |
+
# Use HF_TOKEN secret from environment if input is empty
|
| 9 |
if token.strip() == "":
|
| 10 |
token = os.environ.get("HF_TOKEN")
|
|
|
|
| 11 |
if not token:
|
| 12 |
+
return "β Token missing. Provide one or set HF_TOKEN in secrets."
|
| 13 |
|
|
|
|
| 14 |
login(token=token)
|
| 15 |
api = HfApi()
|
| 16 |
|
| 17 |
+
# Try to create target repo (will skip if already exists)
|
| 18 |
+
api.create_repo(repo_id=target_repo_id, repo_type=repo_type, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# Download
|
| 21 |
if repo_type == "model":
|
| 22 |
path = snapshot_download(repo_id=source_repo_id)
|
| 23 |
else:
|
| 24 |
path = snapshot_download(repo_id=source_repo_id, repo_type=repo_type)
|
| 25 |
|
| 26 |
+
# Upload
|
| 27 |
api.upload_folder(
|
| 28 |
folder_path=path,
|
| 29 |
repo_id=target_repo_id,
|
|
|
|
| 31 |
path_in_repo=path_in_repo,
|
| 32 |
)
|
| 33 |
|
| 34 |
+
return f"β
Downloaded from {source_repo_id} and uploaded to {target_repo_id} (created if not exist)!"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
return f"β Failed: {e}"
|
| 38 |
|
| 39 |
+
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("## π€ Auto Download & Upload HF Repo (With Auto-Cleanup)")
|
| 42 |
|