File size: 6,315 Bytes
790877f
 
 
 
 
 
 
 
 
 
 
 
bd47d2d
790877f
bd47d2d
790877f
 
bd47d2d
790877f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd47d2d
 
 
 
 
 
 
 
 
790877f
 
 
 
bd47d2d
 
 
 
 
 
790877f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash

# ===============================================================
# Hugging Face Deployment Script
# ===============================================================
# This script deploys your model to Hugging Face.
#
# SECURITY NOTICE:
# - NEVER hardcode tokens or secrets in this script
# - Use environment variables for sensitive information
# - Store secrets in a secure location, not in your code
#
# Required environment variables from .env file in project root:
# - HUGGING_FACE_TOKEN: Your Hugging Face API token
# - HUGGING_FACE_SPACE: Your Hugging Face space name
#
# How to use:
# 1. Make sure .env file exists in project root with proper variables
#
# 2. Run the script:
#    ./scripts/deploy_to_hf.sh
# ===============================================================

# Color definitions for better readability
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
PURPLE="\033[0;35m"
CYAN="\033[0;36m"
NC="\033[0m" # No Color

# Function for logging messages with color
log_info() {
  echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
  echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
  echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
  echo -e "${RED}[ERROR]${NC} $1"
}

log_section() {
  echo -e "\n${PURPLE}=== $1 ===${NC}"
}

# Function to check if a command exists
command_exists() {
  command -v "$1" >/dev/null 2>&1
}

# Function to validate token format (basic check)
validate_token() {
  local token="$1"
  # Check if token starts with "hf_" prefix (typical for Hugging Face tokens)
  if [[ ! "$token" =~ ^hf_[a-zA-Z0-9]+$ ]]; then
    return 1
  fi
  return 0
}

# Exit on any error
set -e

# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

# Change to project root directory
cd "$PROJECT_ROOT" || {
  log_error "Failed to change to project root directory: $PROJECT_ROOT"
  exit 1
}

log_section "Starting Hugging Face Deployment"

# Check for required dependencies
log_info "Checking dependencies..."

MISSING_DEPS=0
for cmd in git huggingface-cli python pip; do
  if ! command_exists "$cmd"; then
    log_error "Required command not found: $cmd"
    MISSING_DEPS=1
  fi
done

if [ $MISSING_DEPS -eq 1 ]; then
  log_error "Please install missing dependencies and try again."
  exit 1
fi

# Load environment variables from .env file
log_info "Loading environment variables from .env file..."
if [ -f "$PROJECT_ROOT/.env" ]; then
  source "$PROJECT_ROOT/.env"
else
  log_error ".env file not found in project root directory!"
  exit 1
fi

# Check for required environment variables
log_info "Checking environment variables..."

if [ -z "$HUGGING_FACE_TOKEN" ]; then
  log_error "HUGGING_FACE_TOKEN environment variable is not set in .env file!"
  exit 1
fi

if [ -z "$HUGGING_FACE_SPACE" ]; then
  log_error "HUGGING_FACE_SPACE environment variable is not set in .env file!"
  exit 1
fi

# Validate token format
if ! validate_token "$HUGGING_FACE_TOKEN"; then
  log_warning "The HUGGING_FACE_TOKEN format doesn't appear valid (should start with 'hf_')."
  read -p "Continue anyway? (y/n): " -n 1 -r
  echo
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    log_info "Deployment canceled."
    exit 1
  fi
fi

log_info "Deploying to Hugging Face space: $HUGGING_FACE_SPACE"

# Login to Hugging Face
log_info "Logging in to Hugging Face..."
if ! HUGGINGFACE_HUB_TOKEN="$HUGGING_FACE_TOKEN" huggingface-cli login --token "$HUGGING_FACE_TOKEN" 2>/dev/null; then
  log_error "Failed to log in to Hugging Face. Please check your token."
  exit 1
fi
log_success "Logged in to Hugging Face successfully."

# Check if we're in a git repository
if [ ! -d .git ]; then
  log_warning "Not in a git repository. Initializing..."
  git init
  git config --local user.name "HIM Deployment"
  git config --local user.email "[email protected]"
fi

# Check if repository is configured properly
if ! git remote -v | grep -q "$HUGGING_FACE_SPACE"; then
  log_info "Setting up git remote for Hugging Face..."
  git remote add huggingface "https://huggingface.co/spaces/$HUGGING_FACE_SPACE"
  # Alternatively, use the token in the URL (less secure but sometimes needed)
  # git remote add huggingface "https://USER:[email protected]/spaces/$HUGGING_FACE_SPACE"
fi

# Make sure dependencies are installed
log_info "Checking if requirements.txt exists..."
if [ -f requirements.txt ]; then
  log_info "Installing dependencies from requirements.txt..."
  if ! pip install -r requirements.txt; then
    log_warning "Some dependencies could not be installed. This might cause deployment issues."
  fi
else
  log_warning "requirements.txt not found. Make sure all dependencies are installed."
fi

# Prepare deployment
log_section "Preparing Deployment"

# Check for untracked files
UNTRACKED_FILES=$(git ls-files --others --exclude-standard)
if [ -n "$UNTRACKED_FILES" ]; then
  log_warning "Untracked files found:"
  echo "$UNTRACKED_FILES"
  read -p "Add these files to git? (y/n): " -n 1 -r
  echo
  if [[ $REPLY =~ ^[Yy]$ ]]; then
    git add .
    log_success "Files added to git."
  else
    log_warning "Continuing without adding untracked files."
  fi
fi

# Commit changes if needed
if ! git diff-index --quiet HEAD --; then
  log_info "Changes detected, creating a commit..."
  read -p "Enter commit message (default: 'Update deployment'): " COMMIT_MSG
  COMMIT_MSG=${COMMIT_MSG:-"Update deployment"}
  git commit -am "$COMMIT_MSG"
  log_success "Changes committed."
fi

# Deploy to Hugging Face
log_section "Deploying to Hugging Face"
log_info "Pushing to Hugging Face Space: $HUGGING_FACE_SPACE"

if ! git push -u huggingface main; then
  log_error "Failed to push to Hugging Face."
  log_info "Try running: git push -u huggingface main --force"
  exit 1
fi

log_success "Deployment successful!"
log_info "Your model is now available at: https://huggingface.co/spaces/$HUGGING_FACE_SPACE"

# Optional: Generate a client usage example
log_section "Client Usage Example"
cat << EOF
Python client example:

from gradio_client import Client

client = Client("$HUGGING_FACE_SPACE")

result = client.predict(
    message="Hello!!",
    system_message="You are a friendly Chatbot.",
    max_tokens=512,
    temperature=0.7,
    top_p=0.95,
    api_name="/chat"
)

print(result)
EOF

exit 0