Spaces:
Running
Running
TeleologyHI
commited on
Commit
·
790877f
1
Parent(s):
3c5e81f
Add secure deployment script with no hardcoded secrets
Browse files- scripts/deploy_to_hf.sh +222 -0
scripts/deploy_to_hf.sh
ADDED
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# ===============================================================
|
4 |
+
# Hugging Face Deployment Script
|
5 |
+
# ===============================================================
|
6 |
+
# This script deploys your model to Hugging Face.
|
7 |
+
#
|
8 |
+
# SECURITY NOTICE:
|
9 |
+
# - NEVER hardcode tokens or secrets in this script
|
10 |
+
# - Use environment variables for sensitive information
|
11 |
+
# - Store secrets in a secure location, not in your code
|
12 |
+
#
|
13 |
+
# Required environment variables:
|
14 |
+
# - HUGGING_FACE_TOKEN: Your Hugging Face API token
|
15 |
+
# - HUGGING_FACE_SPACE (optional): Your Hugging Face space name (default: TeleologyHI/HIM-self)
|
16 |
+
#
|
17 |
+
# How to use:
|
18 |
+
# 1. Set your Hugging Face token:
|
19 |
+
# export HUGGING_FACE_TOKEN="your_token_here"
|
20 |
+
#
|
21 |
+
# 2. Run the script:
|
22 |
+
# ./scripts/deploy_to_hf.sh
|
23 |
+
# ===============================================================
|
24 |
+
|
25 |
+
# Color definitions for better readability
|
26 |
+
RED="\033[0;31m"
|
27 |
+
GREEN="\033[0;32m"
|
28 |
+
YELLOW="\033[0;33m"
|
29 |
+
BLUE="\033[0;34m"
|
30 |
+
PURPLE="\033[0;35m"
|
31 |
+
CYAN="\033[0;36m"
|
32 |
+
NC="\033[0m" # No Color
|
33 |
+
|
34 |
+
# Function for logging messages with color
|
35 |
+
log_info() {
|
36 |
+
echo -e "${BLUE}[INFO]${NC} $1"
|
37 |
+
}
|
38 |
+
|
39 |
+
log_success() {
|
40 |
+
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
41 |
+
}
|
42 |
+
|
43 |
+
log_warning() {
|
44 |
+
echo -e "${YELLOW}[WARNING]${NC} $1"
|
45 |
+
}
|
46 |
+
|
47 |
+
log_error() {
|
48 |
+
echo -e "${RED}[ERROR]${NC} $1"
|
49 |
+
}
|
50 |
+
|
51 |
+
log_section() {
|
52 |
+
echo -e "\n${PURPLE}=== $1 ===${NC}"
|
53 |
+
}
|
54 |
+
|
55 |
+
# Function to check if a command exists
|
56 |
+
command_exists() {
|
57 |
+
command -v "$1" >/dev/null 2>&1
|
58 |
+
}
|
59 |
+
|
60 |
+
# Function to validate token format (basic check)
|
61 |
+
validate_token() {
|
62 |
+
local token="$1"
|
63 |
+
# Check if token starts with "hf_" prefix (typical for Hugging Face tokens)
|
64 |
+
if [[ ! "$token" =~ ^hf_[a-zA-Z0-9]+$ ]]; then
|
65 |
+
return 1
|
66 |
+
fi
|
67 |
+
return 0
|
68 |
+
}
|
69 |
+
|
70 |
+
# Exit on any error
|
71 |
+
set -e
|
72 |
+
|
73 |
+
# Script directory
|
74 |
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
75 |
+
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
76 |
+
|
77 |
+
# Change to project root directory
|
78 |
+
cd "$PROJECT_ROOT" || {
|
79 |
+
log_error "Failed to change to project root directory: $PROJECT_ROOT"
|
80 |
+
exit 1
|
81 |
+
}
|
82 |
+
|
83 |
+
log_section "Starting Hugging Face Deployment"
|
84 |
+
|
85 |
+
# Check for required dependencies
|
86 |
+
log_info "Checking dependencies..."
|
87 |
+
|
88 |
+
MISSING_DEPS=0
|
89 |
+
for cmd in git huggingface-cli python pip; do
|
90 |
+
if ! command_exists "$cmd"; then
|
91 |
+
log_error "Required command not found: $cmd"
|
92 |
+
MISSING_DEPS=1
|
93 |
+
fi
|
94 |
+
done
|
95 |
+
|
96 |
+
if [ $MISSING_DEPS -eq 1 ]; then
|
97 |
+
log_error "Please install missing dependencies and try again."
|
98 |
+
exit 1
|
99 |
+
fi
|
100 |
+
|
101 |
+
# Check for required environment variables
|
102 |
+
log_info "Checking environment variables..."
|
103 |
+
|
104 |
+
if [ -z "$HUGGING_FACE_TOKEN" ]; then
|
105 |
+
log_error "HUGGING_FACE_TOKEN environment variable is not set!"
|
106 |
+
log_info "Please set it using: export HUGGING_FACE_TOKEN=\"your_token_here\""
|
107 |
+
exit 1
|
108 |
+
fi
|
109 |
+
|
110 |
+
# Validate token format
|
111 |
+
if ! validate_token "$HUGGING_FACE_TOKEN"; then
|
112 |
+
log_warning "The HUGGING_FACE_TOKEN format doesn't appear valid (should start with 'hf_')."
|
113 |
+
read -p "Continue anyway? (y/n): " -n 1 -r
|
114 |
+
echo
|
115 |
+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
116 |
+
log_info "Deployment canceled."
|
117 |
+
exit 1
|
118 |
+
fi
|
119 |
+
fi
|
120 |
+
|
121 |
+
# Set default Hugging Face space or use provided value
|
122 |
+
HUGGING_FACE_SPACE="${HUGGING_FACE_SPACE:-TeleologyHI/HIM-self}"
|
123 |
+
log_info "Deploying to Hugging Face space: $HUGGING_FACE_SPACE"
|
124 |
+
|
125 |
+
# Login to Hugging Face
|
126 |
+
log_info "Logging in to Hugging Face..."
|
127 |
+
if ! HUGGINGFACE_HUB_TOKEN="$HUGGING_FACE_TOKEN" huggingface-cli login --token "$HUGGING_FACE_TOKEN" 2>/dev/null; then
|
128 |
+
log_error "Failed to log in to Hugging Face. Please check your token."
|
129 |
+
exit 1
|
130 |
+
fi
|
131 |
+
log_success "Logged in to Hugging Face successfully."
|
132 |
+
|
133 |
+
# Check if we're in a git repository
|
134 |
+
if [ ! -d .git ]; then
|
135 |
+
log_warning "Not in a git repository. Initializing..."
|
136 |
+
git init
|
137 |
+
git config --local user.name "HIM Deployment"
|
138 |
+
git config --local user.email "[email protected]"
|
139 |
+
fi
|
140 |
+
|
141 |
+
# Check if repository is configured properly
|
142 |
+
if ! git remote -v | grep -q "$HUGGING_FACE_SPACE"; then
|
143 |
+
log_info "Setting up git remote for Hugging Face..."
|
144 |
+
git remote add huggingface "https://huggingface.co/spaces/$HUGGING_FACE_SPACE"
|
145 |
+
# Alternatively, use the token in the URL (less secure but sometimes needed)
|
146 |
+
# git remote add huggingface "https://USER:[email protected]/spaces/$HUGGING_FACE_SPACE"
|
147 |
+
fi
|
148 |
+
|
149 |
+
# Make sure dependencies are installed
|
150 |
+
log_info "Checking if requirements.txt exists..."
|
151 |
+
if [ -f requirements.txt ]; then
|
152 |
+
log_info "Installing dependencies from requirements.txt..."
|
153 |
+
if ! pip install -r requirements.txt; then
|
154 |
+
log_warning "Some dependencies could not be installed. This might cause deployment issues."
|
155 |
+
fi
|
156 |
+
else
|
157 |
+
log_warning "requirements.txt not found. Make sure all dependencies are installed."
|
158 |
+
fi
|
159 |
+
|
160 |
+
# Prepare deployment
|
161 |
+
log_section "Preparing Deployment"
|
162 |
+
|
163 |
+
# Check for untracked files
|
164 |
+
UNTRACKED_FILES=$(git ls-files --others --exclude-standard)
|
165 |
+
if [ -n "$UNTRACKED_FILES" ]; then
|
166 |
+
log_warning "Untracked files found:"
|
167 |
+
echo "$UNTRACKED_FILES"
|
168 |
+
read -p "Add these files to git? (y/n): " -n 1 -r
|
169 |
+
echo
|
170 |
+
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
171 |
+
git add .
|
172 |
+
log_success "Files added to git."
|
173 |
+
else
|
174 |
+
log_warning "Continuing without adding untracked files."
|
175 |
+
fi
|
176 |
+
fi
|
177 |
+
|
178 |
+
# Commit changes if needed
|
179 |
+
if ! git diff-index --quiet HEAD --; then
|
180 |
+
log_info "Changes detected, creating a commit..."
|
181 |
+
read -p "Enter commit message (default: 'Update deployment'): " COMMIT_MSG
|
182 |
+
COMMIT_MSG=${COMMIT_MSG:-"Update deployment"}
|
183 |
+
git commit -am "$COMMIT_MSG"
|
184 |
+
log_success "Changes committed."
|
185 |
+
fi
|
186 |
+
|
187 |
+
# Deploy to Hugging Face
|
188 |
+
log_section "Deploying to Hugging Face"
|
189 |
+
log_info "Pushing to Hugging Face Space: $HUGGING_FACE_SPACE"
|
190 |
+
|
191 |
+
if ! git push -u huggingface main; then
|
192 |
+
log_error "Failed to push to Hugging Face."
|
193 |
+
log_info "Try running: git push -u huggingface main --force"
|
194 |
+
exit 1
|
195 |
+
fi
|
196 |
+
|
197 |
+
log_success "Deployment successful!"
|
198 |
+
log_info "Your model is now available at: https://huggingface.co/spaces/$HUGGING_FACE_SPACE"
|
199 |
+
|
200 |
+
# Optional: Generate a client usage example
|
201 |
+
log_section "Client Usage Example"
|
202 |
+
cat << EOF
|
203 |
+
Python client example:
|
204 |
+
|
205 |
+
from gradio_client import Client
|
206 |
+
|
207 |
+
client = Client("$HUGGING_FACE_SPACE")
|
208 |
+
|
209 |
+
result = client.predict(
|
210 |
+
message="Hello!!",
|
211 |
+
system_message="You are a friendly Chatbot.",
|
212 |
+
max_tokens=512,
|
213 |
+
temperature=0.7,
|
214 |
+
top_p=0.95,
|
215 |
+
api_name="/chat"
|
216 |
+
)
|
217 |
+
|
218 |
+
print(result)
|
219 |
+
EOF
|
220 |
+
|
221 |
+
exit 0
|
222 |
+
|