File size: 6,734 Bytes
ff03012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# Script de déploiement pour Hugging Face Spaces
# Build et push le Space vers Hugging Face

set -e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() { echo -e "${BLUE}ℹ️  $1${NC}"; }
print_success() { echo -e "${GREEN}✅ $1${NC}"; }
print_warning() { echo -e "${YELLOW}⚠️  $1${NC}"; }
print_error() { echo -e "${RED}❌ $1${NC}"; }

echo ""
print_info "🚀 Deploying CU1-X to Hugging Face Spaces"
echo "================================================"
echo ""

# Check if we're in a git repo
if [ ! -d ".git" ]; then
    print_error "Not a git repository!"
    print_info "Initializing git repository..."
    git init
    print_success "Git repository initialized"
fi

# Check Git LFS
print_info "Configuring Git LFS..."
if ! command -v git-lfs &> /dev/null; then
    print_error "Git LFS not installed!"
    print_info "Install with: brew install git-lfs (macOS) or sudo apt install git-lfs (Linux)"
    exit 1
fi

git lfs install > /dev/null 2>&1 || true

# Ensure model.pth is tracked
if [ -f "model.pth" ]; then
    if ! git lfs ls-files | grep -q "model.pth"; then
        print_info "Adding model.pth to Git LFS..."
        git lfs track "*.pth"
        git add .gitattributes
        print_success "model.pth configured for Git LFS"
    else
        print_success "model.pth already tracked by Git LFS"
    fi
else
    print_error "model.pth not found!"
    exit 1
fi

# Check HF remote
print_info "Checking Hugging Face remote..."
if git remote | grep -q "origin"; then
    REMOTE_URL=$(git remote get-url origin 2>/dev/null || echo "")
    if echo "$REMOTE_URL" | grep -q "huggingface.co"; then
        print_success "HF Space remote configured: $REMOTE_URL"
        SPACE_URL=$(echo "$REMOTE_URL" | sed -E 's|.*spaces/([^/]+)/([^/]+).*|\1/\2|')
        print_info "Space URL: https://huggingface.co/spaces/$SPACE_URL"
    else
        print_warning "Remote exists but doesn't look like HF Space"
        print_info "Current remote: $REMOTE_URL"
    fi
else
    print_warning "No remote configured"
    print_info "You'll need to add a remote:"
    print_info "  git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME"
    read -p "Do you want to configure it now? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        read -p "Enter your HF username: " HF_USERNAME
        read -p "Enter your Space name: " SPACE_NAME
        git remote add origin "https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
        print_success "Remote configured"
        SPACE_URL="$HF_USERNAME/$SPACE_NAME"
    else
        print_error "Cannot deploy without remote"
        exit 1
    fi
fi

# Check login
print_info "Checking Hugging Face login..."
if command -v hf &> /dev/null; then
    if hf auth whoami &> /dev/null 2>&1; then
        USERNAME=$(hf auth whoami 2>/dev/null | head -n1)
        print_success "Logged in as: $USERNAME"
    else
        print_warning "Not logged in"
        print_info "Logging in..."
        hf login
    fi
elif command -v huggingface-cli &> /dev/null; then
    if huggingface-cli whoami &> /dev/null 2>&1; then
        USERNAME=$(huggingface-cli whoami 2>/dev/null | head -n1)
        print_success "Logged in as: $USERNAME"
    else
        print_warning "Not logged in"
        print_info "Logging in..."
        huggingface-cli login
    fi
else
    print_error "Hugging Face CLI not found!"
    print_info "Install: pip install huggingface-hub"
    exit 1
fi

# Ensure requirements.txt is complete
print_info "Checking requirements.txt..."
if [ -f "requirements-full.txt" ] && [ -f "requirements.txt" ]; then
    FULL_LINES=$(wc -l < requirements-full.txt)
    CURRENT_LINES=$(wc -l < requirements.txt)
    
    if [ $CURRENT_LINES -lt $FULL_LINES ]; then
        print_warning "requirements.txt seems incomplete"
        read -p "Use requirements-full.txt? (y/n) " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            cp requirements-full.txt requirements.txt
            print_success "Updated requirements.txt from requirements-full.txt"
        fi
    fi
fi

# Stage all files
print_info "Staging files..."
git add .
print_success "Files staged"

# Check if there are changes
if [ -z "$(git status --porcelain)" ]; then
    print_warning "No changes to commit"
    print_info "Everything is already up to date"
else
    # Show what will be committed
    print_info "Changes to commit:"
    git status --short
    
    # Commit
    print_info "Creating commit..."
    COMMIT_MSG="Deploy CU1-X to Hugging Face Spaces

- Multi-model AI pipeline (RF-DETR, CLIP, OCR, BLIP)
- Unified API architecture
- Gradio web interface
- Full model weights included via Git LFS
- Ready for production deployment"
    
    git commit -m "$COMMIT_MSG" || {
        print_error "Commit failed"
        exit 1
    }
    print_success "Changes committed"
fi

# Push to Hugging Face
print_info "Pushing to Hugging Face Spaces..."
print_warning "This may take several minutes (model.pth is 510MB)..."
echo ""

BRANCH=$(git branch --show-current 2>/dev/null || echo "main")

if git push -u origin "$BRANCH" 2>&1 | tee /tmp/hf_push.log; then
    print_success "Push completed successfully!"
    echo ""
    echo "================================================"
    print_success "🎉 Deployment Successful!"
    echo "================================================"
    echo ""
    
    if [ -n "$SPACE_URL" ]; then
        print_info "Your Space is deploying at:"
        echo "   https://huggingface.co/spaces/$SPACE_URL"
        echo ""
        print_info "Build progress:"
        echo "   https://huggingface.co/spaces/$SPACE_URL/logs"
        echo ""
        print_info "Once deployed, your app will be at:"
        echo "   https://huggingface.co/spaces/$SPACE_URL"
        echo ""
        print_info "API endpoint:"
        echo "   https://$SPACE_URL.hf.space/api/predict"
        echo ""
    fi
    
    print_warning "First build may take 5-10 minutes"
    print_info "HF Spaces will automatically:"
    print_info "  - Install dependencies from requirements.txt"
    print_info "  - Download models (CLIP, BLIP, EasyOCR)"
    print_info "  - Start app.py"
    print_info "  - Expose Gradio interface and API"
    echo ""
    print_success "All done! 🎉"
else
    print_error "Push failed!"
    echo ""
    print_info "Common issues:"
    print_info "1. Authentication failed: Run 'hf login' or 'huggingface-cli login'"
    print_info "2. Git LFS error: Ensure Git LFS is installed and model.pth is tracked"
    print_info "3. Network error: Check your internet connection"
    echo ""
    print_info "Check the error above for details"
    exit 1
fi