Spaces:
Sleeping
Sleeping
File size: 3,603 Bytes
db1233f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #!/usr/bin/env python3
"""Try to deploy using available authentication methods."""
import os
import subprocess
import sys
def try_deploy():
print("π Attempting deployment with available credentials...")
print()
# Try to get token from various sources
token = None
# Check environment
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
if token:
print("β
Found token in environment")
else:
# Check git credentials
try:
with open(os.path.expanduser("~/.git-credentials"), "r") as f:
for line in f:
if "huggingface.co" in line:
# Extract token from URL
parts = line.strip().replace("https://", "").split("@")[0]
if ":" in parts:
token = parts.split(":")[1]
print("β
Found token in git credentials")
break
except:
pass
if not token:
# Try to use HF CLI login
try:
from huggingface_hub import whoami
user_info = whoami()
print(f"β
Logged in as: {user_info.get('name', 'Unknown')}")
# Token might be in cache
token = "cached" # Will use default authentication
except:
print("β No authentication found")
print()
print("Please run one of these:")
print(" 1. huggingface-cli login")
print(" 2. export HF_TOKEN=your_token")
print(" 3. Or manually create space and push")
return False
# Try to create space and push
try:
from huggingface_hub import create_repo, HfApi
repo_id = "Anshrathore01/opinion-summarizer"
print(f"π¦ Creating/verifying Space: {repo_id}")
try:
create_repo(
repo_id=repo_id,
repo_type="space",
space_sdk="docker",
private=False,
exist_ok=True
)
print("β
Space ready")
except Exception as e:
if "409" in str(e) or "already exists" in str(e).lower():
print("β
Space already exists")
else:
raise
# Set up git and push
print()
print("π Setting up git remote...")
subprocess.run(["git", "remote", "remove", "space"], capture_output=True)
if token and token != "cached":
remote_url = f"https://Anshrathore01:{token}@huggingface.co/spaces/{repo_id}"
else:
remote_url = f"https://huggingface.co/spaces/{repo_id}"
subprocess.run(["git", "remote", "add", "space", remote_url], check=True)
print("β
Git remote configured")
print()
print("π€ Pushing code...")
result = subprocess.run(["git", "push", "space", "main"])
if result.returncode == 0:
print()
print("=" * 50)
print("β
DEPLOYMENT SUCCESSFUL!")
print("=" * 50)
print()
print(f"π Your app: https://huggingface.co/spaces/{repo_id}")
return True
else:
print("β Push failed - authentication required")
return False
except Exception as e:
print(f"β Error: {e}")
return False
if __name__ == "__main__":
success = try_deploy()
sys.exit(0 if success else 1)
|