File size: 7,345 Bytes
52510e8 | 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import os
import sys
import shutil
import tempfile
from huggingface_hub import create_repo, HfApi
def print_banner():
banner = """
======================================================================
HUGGING FACE HUB DEPLOYMENT WIZARD | PROFILE: ARAVINDHAN11
======================================================================
"""
print(banner)
def write_docker_assets(directory_path, space_title):
"""
Writes a Dockerfile and README.md with spaces metadata for Hugging Face Spaces.
"""
dockerfile_content = """FROM python:3.12-slim
# Install essential system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \\
build-essential \\
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Install Python packages
RUN pip install --no-cache-dir torch transformers datasets rich huggingface_hub
# Copy all project files
COPY . .
# Expose port (HF Spaces defaults to 7860)
EXPOSE 7860
ENV PORT=7860
ENV HOME=/tmp
# Run command center server
CMD ["python", "server.py"]
"""
readme_content = f"""---
title: {space_title}
emoji: 🧠
colorFrom: indigo
colorTo: purple
sdk: docker
pinned: false
---
# {space_title}
An advanced and intelligent distributed transformer training and inference framework.
## Key Features
- **Hugging Face Hub Connection**: Convert, load, and push models.
- **Playground**: High-performance streaming chat.
- **Fine-Tuning**: Live loss chart.
- **Parallelism**: 3D Parallelism status.
Developed by **Aravindhan11**.
"""
with open(os.path.join(directory_path, "Dockerfile"), "w", encoding="utf-8") as f:
f.write(dockerfile_content)
with open(os.path.join(directory_path, "README.md"), "w", encoding="utf-8") as f:
f.write(readme_content)
def deploy_space(token, space_name):
"""
Deploys the entire framework dashboard as a Docker Space on Hugging Face.
"""
repo_id = f"Aravindhan11/{space_name}"
print(f"\nInitializing Hugging Face Space repository: {repo_id}...")
try:
# Create Hugging Face Space repository
create_repo(
repo_id=repo_id,
token=token,
repo_type="space",
space_sdk="docker",
private=False,
exist_ok=True
)
print("Space repository initialized successfully.")
# Create a temporary directory to bundle files, avoiding copying large .git folders
print("Bundling project assets for container building...")
current_dir = os.path.dirname(os.path.abspath(__file__))
with tempfile.TemporaryDirectory() as temp_dir:
# Copy source files
for item in os.listdir(current_dir):
s = os.path.join(current_dir, item)
d = os.path.join(temp_dir, item)
# Exclude large cache directories, virtual environments, and checkpoints
if item in [".git", ".venv", "__pycache__", "exported_checkpoints", "node_modules", ".gemini"]:
continue
if os.path.isdir(s):
shutil.copytree(s, d)
else:
shutil.copy(s, d)
# Write Docker assets into bundle
write_docker_assets(temp_dir, "Distributed LLaMA Framework")
print("Uploading bundled files to Hugging Face Spaces (this might take a minute)...")
api = HfApi()
api.upload_folder(
folder_path=temp_dir,
repo_id=repo_id,
repo_type="space",
token=token,
commit_message="Deploy Intelligent Distributed LLaMA Framework"
)
space_url = f"https://huggingface.co/spaces/{repo_id}"
print(f"\n🎉 SUCCESS! Your Intelligent Framework is now building and hosting on Hugging Face Spaces!")
print(f"👉 Access your public dashboard here: {space_url}")
print("Hugging Face will automatically build the Docker image and deploy it. Enjoy!")
except Exception as e:
print(f"\n❌ Error during deployment: {e}")
def deploy_model(token, repo_name):
"""
Converts and deploys a small LLaMA model configuration and pre-trained weights to Hugging Face Hub.
"""
repo_id = f"Aravindhan11/{repo_name}"
print(f"\nPreparing to upload pre-trained custom model weights to Model Hub: {repo_id}...")
try:
from hf_converter import HFWeightConverter
# Download and map a small SmolLM model locally to convert it
base_model_name = "HuggingFaceTB/SmolLM-135M-Instruct"
print(f"Downloading pre-trained base model ({base_model_name}) to convert keys...")
model, config, tokenizer = HFWeightConverter.load_and_convert_hf(base_model_name)
# Save custom checkpoints locally
local_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "exported_checkpoints", repo_name)
print(f"Reversing weight names and writing standard config files to {local_dir}...")
HFWeightConverter.save_custom_to_hf_format(
custom_model=model,
hf_config=config,
tokenizer=tokenizer,
save_directory=local_dir
)
# Push folder to HF
repo_url = HFWeightConverter.push_to_huggingface(
local_dir=local_dir,
repo_id=repo_id,
token=token
)
print(f"\n🎉 SUCCESS! Your custom LLaMA model has been converted and hosted on Hugging Face Model Hub!")
print(f"👉 View your model repository here: {repo_url}")
except Exception as e:
print(f"\n❌ Error during model deployment: {e}")
def main():
print_banner()
# 1. Ask for credentials (use env var HF_TOKEN if set)
token = os.getenv("HF_TOKEN")
if not token:
token = input("Enter your Hugging Face User Write Token (hf_...): ").strip()
if not token:
print("Error: Hugging Face Token is required.")
sys.exit(1)
# 2. Ask for deployment mode (use env var DEPLOY_MODE if set)
print("\nWhat would you like to host on Hugging Face?")
print(" [1] Entire Intelligent Web Dashboard (as an interactive Hugging Face Space)")
print(" [2] Custom LLaMA Model Config & Weights (as a Hugging Face Model Repository)")
choice = os.getenv("DEPLOY_MODE")
if not choice:
choice = input("\nSelect option (1 or 2): ").strip()
if choice == "1":
space_name = os.getenv("SPACE_NAME")
if not space_name:
space_name = input("Enter Space Name (default: Distributed-Transformer-Framework): ").strip()
if not space_name:
space_name = "Distributed-Transformer-Framework"
deploy_space(token, space_name)
elif choice == "2":
repo_name = os.getenv("REPO_NAME")
if not repo_name:
repo_name = input("Enter Model Repository Name (default: Distributed-Llama-Model): ").strip()
if not repo_name:
repo_name = "Distributed-Llama-Model"
deploy_model(token, repo_name)
else:
print("Invalid option selected. Exiting.")
if __name__ == "__main__":
main()
|