Upload deploy_to_hf.py with huggingface_hub
Browse files- deploy_to_hf.py +101 -0
deploy_to_hf.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Trend Hunter AI β Hugging Face Spaces Deploy Script
|
| 3 |
+
Run this once: python deploy_to_hf.py
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
def install(pkg):
|
| 9 |
+
import subprocess
|
| 10 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg, "-q"])
|
| 11 |
+
|
| 12 |
+
# Install HF hub if needed
|
| 13 |
+
try:
|
| 14 |
+
from huggingface_hub import HfApi, login
|
| 15 |
+
except ImportError:
|
| 16 |
+
print("Installing huggingface_hub...")
|
| 17 |
+
install("huggingface_hub")
|
| 18 |
+
from huggingface_hub import HfApi, login
|
| 19 |
+
|
| 20 |
+
print("\n" + "="*55)
|
| 21 |
+
print(" TREND HUNTER AI β Hugging Face Spaces Deployer")
|
| 22 |
+
print("="*55)
|
| 23 |
+
print("\nYou need a FREE Hugging Face account.")
|
| 24 |
+
print("Get a token at: https://huggingface.co/settings/tokens")
|
| 25 |
+
print(" β Click 'New token' β Role: Write β Copy token\n")
|
| 26 |
+
|
| 27 |
+
token = input("Paste your HF token here: ").strip()
|
| 28 |
+
if not token:
|
| 29 |
+
print("No token provided. Exiting.")
|
| 30 |
+
sys.exit(1)
|
| 31 |
+
|
| 32 |
+
login(token=token)
|
| 33 |
+
api = HfApi()
|
| 34 |
+
|
| 35 |
+
# Get username
|
| 36 |
+
user = api.whoami()["name"]
|
| 37 |
+
space_name = f"{user}/trend-hunter-ai"
|
| 38 |
+
|
| 39 |
+
print(f"\nCreating Space: {space_name}")
|
| 40 |
+
|
| 41 |
+
# Create the Space (Docker type)
|
| 42 |
+
try:
|
| 43 |
+
api.create_repo(
|
| 44 |
+
repo_id=space_name,
|
| 45 |
+
repo_type="space",
|
| 46 |
+
space_sdk="docker",
|
| 47 |
+
private=False,
|
| 48 |
+
exist_ok=True,
|
| 49 |
+
)
|
| 50 |
+
print(f"β
Space created: https://huggingface.co/spaces/{space_name}")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Space may already exist, continuing... ({e})")
|
| 53 |
+
|
| 54 |
+
# Files to upload (exclude secrets and junk)
|
| 55 |
+
EXCLUDE = {".env", "__pycache__", "node_modules", ".git", ".tmp",
|
| 56 |
+
"credentials.json", "token.json", "frontend"}
|
| 57 |
+
|
| 58 |
+
print("\nUploading files...")
|
| 59 |
+
project_root = os.path.dirname(os.path.abspath(__file__))
|
| 60 |
+
|
| 61 |
+
uploaded = []
|
| 62 |
+
for root, dirs, files in os.walk(project_root):
|
| 63 |
+
# Skip excluded dirs
|
| 64 |
+
dirs[:] = [d for d in dirs if d not in EXCLUDE and not d.startswith('.')]
|
| 65 |
+
for fname in files:
|
| 66 |
+
if fname.startswith('.') and fname not in ['.dockerignore']:
|
| 67 |
+
continue
|
| 68 |
+
if fname in EXCLUDE:
|
| 69 |
+
continue
|
| 70 |
+
full_path = os.path.join(root, fname)
|
| 71 |
+
rel_path = os.path.relpath(full_path, project_root).replace("\\", "/")
|
| 72 |
+
try:
|
| 73 |
+
api.upload_file(
|
| 74 |
+
path_or_fileobj=full_path,
|
| 75 |
+
path_in_repo=rel_path,
|
| 76 |
+
repo_id=space_name,
|
| 77 |
+
repo_type="space",
|
| 78 |
+
)
|
| 79 |
+
print(f" β {rel_path}")
|
| 80 |
+
uploaded.append(rel_path)
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f" β {rel_path}: {e}")
|
| 83 |
+
|
| 84 |
+
# Set environment variables reminder
|
| 85 |
+
print(f"""
|
| 86 |
+
{"="*55}
|
| 87 |
+
β
UPLOAD COMPLETE β {len(uploaded)} files uploaded!
|
| 88 |
+
|
| 89 |
+
NEXT STEP β Add your API keys (required):
|
| 90 |
+
1. Go to: https://huggingface.co/spaces/{space_name}/settings
|
| 91 |
+
2. Scroll to 'Variables and Secrets'
|
| 92 |
+
3. Add:
|
| 93 |
+
YOUTUBE_API_KEY β your YouTube Data API v3 key
|
| 94 |
+
GEMINI_API_KEY β your Gemini key (optional)
|
| 95 |
+
4. Click Save β Space will rebuild automatically (~2 min)
|
| 96 |
+
|
| 97 |
+
YOUR LIVE URL:
|
| 98 |
+
https://{user}-trend-hunter-ai.hf.space
|
| 99 |
+
|
| 100 |
+
{"="*55}
|
| 101 |
+
""")
|