Spaces:
Sleeping
Sleeping
Upload hf_push.py via API
Browse files- hf_push.py +66 -0
hf_push.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
def run(cmd, check=True, capture=False):
|
| 7 |
+
if isinstance(cmd, list):
|
| 8 |
+
pass
|
| 9 |
+
if capture:
|
| 10 |
+
p = subprocess.run(cmd, capture_output=True, text=True)
|
| 11 |
+
return p.stdout.strip()
|
| 12 |
+
else:
|
| 13 |
+
subprocess.run(cmd, check=check)
|
| 14 |
+
|
| 15 |
+
def main():
|
| 16 |
+
token = os.environ.get("HF_TOKEN")
|
| 17 |
+
if not token:
|
| 18 |
+
print("HF_TOKEN not set. Set HF_TOKEN environment variable and retry.")
|
| 19 |
+
sys.exit(1)
|
| 20 |
+
|
| 21 |
+
repo_id = "JoaquinZ/DesgrabadorAI"
|
| 22 |
+
repo_git = f"https://huggingface.co/spaces/{repo_id}.git"
|
| 23 |
+
auth_url = f"https://{token}@huggingface.co/spaces/{repo_id}.git"
|
| 24 |
+
|
| 25 |
+
# Get current origin URL
|
| 26 |
+
try:
|
| 27 |
+
orig = run(["git","remote","get-url","origin"], capture=True)
|
| 28 |
+
except Exception:
|
| 29 |
+
print("Failed to read current origin URL. Is this a git repo?")
|
| 30 |
+
sys.exit(1)
|
| 31 |
+
|
| 32 |
+
print("Original origin:", orig)
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
print("Setting temporary auth remote URL (token in memory only)")
|
| 36 |
+
run(["git","remote","set-url","origin", auth_url])
|
| 37 |
+
|
| 38 |
+
status = run(["git","status","--porcelain"], capture=True)
|
| 39 |
+
if not status:
|
| 40 |
+
print("No local changes to commit. Pushing existing branches...")
|
| 41 |
+
else:
|
| 42 |
+
print("Staging and committing local changes...")
|
| 43 |
+
run(["git","add","-A"])
|
| 44 |
+
# commit may fail if no user.name/email set; allow failing gracefully
|
| 45 |
+
try:
|
| 46 |
+
run(["git","commit","-m","Update README frontmatter and setup files for HF Spaces"])
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print("Warning: git commit failed (maybe no changes or git config). Continuing.")
|
| 49 |
+
|
| 50 |
+
print("Pushing to remote...")
|
| 51 |
+
run(["git","push","origin","HEAD"], check=True)
|
| 52 |
+
print("Push succeeded.")
|
| 53 |
+
|
| 54 |
+
except subprocess.CalledProcessError as e:
|
| 55 |
+
print("Git command failed:", e)
|
| 56 |
+
sys.exit(1)
|
| 57 |
+
finally:
|
| 58 |
+
# restore original origin URL
|
| 59 |
+
try:
|
| 60 |
+
run(["git","remote","set-url","origin", orig])
|
| 61 |
+
print("Restored original origin URL.")
|
| 62 |
+
except Exception:
|
| 63 |
+
print("Warning: failed to restore original origin URL. Please check git remote manually.")
|
| 64 |
+
|
| 65 |
+
if __name__ == '__main__':
|
| 66 |
+
main()
|