arabic-tts / deploy_to_hf.py
melakio's picture
Upload deploy_to_hf.py with huggingface_hub
d7a14fb verified
#!/usr/bin/env python3
"""
Arabic TTS β†’ Hugging Face Spaces Deployer
==========================================
CrΓ©e et dΓ©ploie le serveur TTS sur HF Spaces en une commande.
Usage:
python3 deploy_to_hf.py --token hf_xxxxxxxxxxxx --username YOUR_HF_USERNAME
Obtenez votre token sur: https://huggingface.co/settings/tokens
(Type: Write access)
"""
import argparse
import os
import sys
import shutil
import tempfile
from pathlib import Path
# ─────────────────────────────────────────────────────────────────────────────
SPACE_NAME = "arabic-tts"
SPACE_SDK = "docker"
SPACE_TITLE = "Arabic TTS - FastPitch Neural"
# ─────────────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(description="Deploy Arabic TTS to HF Spaces")
parser.add_argument("--token", required=True, help="HF Write token (hf_...)")
parser.add_argument("--username", required=True, help="HF username")
parser.add_argument("--space", default=SPACE_NAME, help="Space name (default: arabic-tts)")
args = parser.parse_args()
try:
from huggingface_hub import HfApi, create_repo
except ImportError:
print("❌ huggingface_hub not installed in this venv")
sys.exit(1)
api = HfApi(token=args.token)
repo_id = f"{args.username}/{args.space}"
print(f"πŸš€ Deploying Arabic TTS to: https://huggingface.co/spaces/{repo_id}")
print()
# 1. Create the Space if it doesn't exist
print("πŸ“¦ Creating HF Space (if not exists)...")
try:
create_repo(
repo_id=repo_id,
repo_type="space",
space_sdk=SPACE_SDK,
token=args.token,
exist_ok=True,
private=False,
)
print(f" βœ… Space ready: https://huggingface.co/spaces/{repo_id}")
except Exception as e:
print(f" ❌ Failed to create space: {e}")
sys.exit(1)
# 2. Prepare files to upload
script_dir = Path(__file__).parent
files_to_upload = {
# source path (local) : dest path (in HF Space repo)
script_dir / "tts_server.py" : "tts_server.py",
script_dir / "hf_space/Dockerfile" : "Dockerfile",
script_dir / "hf_space/requirements.txt" : "requirements.txt",
script_dir / "hf_space/README.md" : "README.md",
}
print(f"\nπŸ“€ Uploading {len(files_to_upload)} files...")
for src, dest in files_to_upload.items():
if not src.exists():
print(f" ⚠️ Missing: {src}")
continue
try:
api.upload_file(
path_or_fileobj=str(src),
path_in_repo=dest,
repo_id=repo_id,
repo_type="space",
token=args.token,
commit_message=f"Deploy: {dest}",
)
print(f" βœ… {dest}")
except Exception as e:
print(f" ❌ {dest}: {e}")
space_url = f"https://huggingface.co/spaces/{repo_id}"
tts_url = f"https://{args.username}-{args.space}.hf.space"
print()
print("=" * 60)
print("βœ… DEPLOYMENT COMPLETE")
print("=" * 60)
print()
print(f"πŸ”— Space URL: {space_url}")
print(f"πŸŽ™οΈ TTS API: {tts_url}/tts?text=Ψ§Ω„Ψ³ΩŽΩ‘Ω„ΩŽΨ§Ω…Ω")
print(f"❀️ Health: {tts_url}/health")
print()
print("⏳ HF is building the Docker image (~3-5 minutes)")
print(" Watch build logs at:")
print(f" {space_url}?logs=build")
print()
print("πŸ“ NEXT STEP β€” Add to your frontend .env.production:")
print()
print(f" VITE_TTS_URL={tts_url}")
print()
print("Then rebuild your frontend:")
print(" cd front && npm run build")
print()
if __name__ == "__main__":
main()