Spaces:
Running
Running
Upload fso_hf_deployer.py with huggingface_hub
Browse files- fso_hf_deployer.py +67 -0
fso_hf_deployer.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi, create_repo, upload_file
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
class HFDeployer:
|
| 5 |
+
"""
|
| 6 |
+
Law XII Component: The Ascension Engine (Production Version)
|
| 7 |
+
Uses the Hugging Face Hub SDK to manage real TGI Spaces.
|
| 8 |
+
"""
|
| 9 |
+
def __init__(self, api_bridge):
|
| 10 |
+
self.api = api_bridge
|
| 11 |
+
self.hf_api = HfApi()
|
| 12 |
+
|
| 13 |
+
def deploy_space(self, space_name, files):
|
| 14 |
+
"""
|
| 15 |
+
Creates/Updates a Hugging Face Space and uploads all TGI core files.
|
| 16 |
+
"""
|
| 17 |
+
token = self.api.vault.access_data("HF_TOKEN", self.api.m, self.api.k)
|
| 18 |
+
if not token:
|
| 19 |
+
print("[!] HF DEPLOYER: Access Denied. Token not found.")
|
| 20 |
+
return False
|
| 21 |
+
|
| 22 |
+
# Get current user from token
|
| 23 |
+
try:
|
| 24 |
+
user_info = self.hf_api.whoami(token=token)
|
| 25 |
+
username = user_info['name']
|
| 26 |
+
except:
|
| 27 |
+
username = "LOOFYYLO"
|
| 28 |
+
|
| 29 |
+
repo_id = f"{username}/{space_name}"
|
| 30 |
+
print(f"\n[*] [HF DEPLOYER]: Synchronizing Space '{repo_id}'...")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# 1. Ensure Space exists
|
| 34 |
+
create_repo(
|
| 35 |
+
repo_id=repo_id,
|
| 36 |
+
token=token,
|
| 37 |
+
repo_type="space",
|
| 38 |
+
space_sdk="gradio",
|
| 39 |
+
exist_ok=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# 2. Upload Files
|
| 43 |
+
for file_path in files:
|
| 44 |
+
if not os.path.exists(file_path): continue
|
| 45 |
+
|
| 46 |
+
print(f" [>] Uploading {file_path}...")
|
| 47 |
+
upload_file(
|
| 48 |
+
path_or_fileobj=file_path,
|
| 49 |
+
path_in_repo=file_path,
|
| 50 |
+
repo_id=repo_id,
|
| 51 |
+
repo_type="space",
|
| 52 |
+
token=token
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
print(f"\n[✓] ASCENSION COMPLETE: TGI is live at huggingface.co/spaces/{repo_id}")
|
| 56 |
+
return True
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f" [-] DEPLOYMENT ERROR: {str(e)}")
|
| 59 |
+
return False
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
from fso_external_api_bridge import ExternalAPIBridge
|
| 63 |
+
from fso_token_vault_setup import setup_token_vault
|
| 64 |
+
v = setup_token_vault()
|
| 65 |
+
api = ExternalAPIBridge(v)
|
| 66 |
+
deployer = HFDeployer(api)
|
| 67 |
+
print("\n--- [ASCENSION ENGINE]: SDK Integrated & Ready ---")
|