Khanfar / deploy-fix.ps1
mhammad's picture
Upload folder using huggingface_hub
3b2eb38 verified
# Windows PowerShell Script for Hugging Face Spaces Deployment Fix
# Save this file as deploy-fix.ps1 in your project root directory and run it in PowerShell
# Set text colors for better readability
function Write-ColorOutput($ForegroundColor) {
$fc = $host.UI.RawUI.ForegroundColor
$host.UI.RawUI.ForegroundColor = $ForegroundColor
if ($args) {
Write-Output $args
}
else {
$input | Write-Output
}
$host.UI.RawUI.ForegroundColor = $fc
}
# Function to print colored messages
function PrintBlue($message) {
Write-ColorOutput Blue $message
}
function PrintGreen($message) {
Write-ColorOutput Green $message
}
function PrintRed($message) {
Write-ColorOutput Red $message
}
# Path to your project
$projectPath = "D:\ARAFAT_GUI-V2.9\files\trained-bot\trained-bot"
# Navigate to the project directory
PrintBlue "Navigating to project directory: $projectPath"
Set-Location -Path $projectPath
PrintBlue "=== Hugging Face Spaces Deployment Fix ==="
# Step 1: Check if huggingface_hub is installed
PrintBlue "`nChecking for huggingface_hub..."
$pipList = pip list
if ($pipList -match "huggingface-hub") {
PrintGreen "huggingface_hub is already installed."
}
else {
PrintBlue "Installing huggingface_hub..."
pip install -U huggingface_hub
}
# Step 2: Ensure huggingface-cli is available
PrintBlue "`nMaking sure huggingface-cli is available..."
try {
$null = Get-Command huggingface-cli -ErrorAction Stop
PrintGreen "huggingface-cli is available."
}
catch {
PrintRed "huggingface-cli is not available. Reinstalling huggingface_hub..."
pip install -U --force-reinstall huggingface_hub
}
# Step 3: Login to Hugging Face
PrintBlue "`nLogging in to Hugging Face..."
PrintRed "IMPORTANT: Do not share your token in public chats or forums"
$HF_TOKEN = Read-Host "Enter your Hugging Face token" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($HF_TOKEN)
$HFToken = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
# Set the token as environment variable
$env:HUGGINGFACE_TOKEN = $HFToken
# Login using the token
PrintBlue "`nAttempting login..."
$HFToken | huggingface-cli login
# Step 4: Verify Spaces directory structure
PrintBlue "`nChecking project structure..."
# Check README.md
if (Test-Path "README.md") {
PrintGreen "README.md exists. Checking content..."
$readmeContent = Get-Content "README.md" -Raw
if ($readmeContent -match "app_file:" -and $readmeContent -match "sdk: gradio") {
PrintGreen "README.md appears to have the right content."
}
else {
PrintRed "README.md may be missing required metadata."
PrintBlue "Adding proper metadata to README.md..."
# Backup the original README
Copy-Item "README.md" "README.md.bak"
# Get app file name
$APP_FILE = "app.py"
if (Test-Path "app.py") {
$APP_FILE = "app.py"
}
else {
$INPUT_APP_FILE = Read-Host "Enter your main app file name (default: app.py)"
if (-not [string]::IsNullOrEmpty($INPUT_APP_FILE)) {
$APP_FILE = $INPUT_APP_FILE
}
}
# Create or update README with proper metadata
$TITLE = "Arafat Vehicle Management"
$INPUT_TITLE = Read-Host "Enter your app title (default: Arafat Vehicle Management)"
if (-not [string]::IsNullOrEmpty($INPUT_TITLE)) {
$TITLE = $INPUT_TITLE
}
$newReadmeContent = "---`ntitle: $TITLE`napp_file: $APP_FILE`nsdk: gradio`nsdk_version: 4.44.1`n---`n$readmeContent"
Set-Content -Path "README.md" -Value $newReadmeContent
PrintGreen "Updated README.md with proper metadata."
}
}
else {
PrintRed "README.md doesn't exist. Creating it..."
# Create a basic README.md with proper metadata
$APP_FILE = "app.py"
if (Test-Path "app.py") {
$APP_FILE = "app.py"
}
else {
$INPUT_APP_FILE = Read-Host "Enter your main app file name (default: app.py)"
if (-not [string]::IsNullOrEmpty($INPUT_APP_FILE)) {
$APP_FILE = $INPUT_APP_FILE
}
}
$TITLE = "Arafat Vehicle Management"
$INPUT_TITLE = Read-Host "Enter your app title (default: Arafat Vehicle Management)"
if (-not [string]::IsNullOrEmpty($INPUT_TITLE)) {
$TITLE = $INPUT_TITLE
}
$newReadmeContent = "---`ntitle: $TITLE`napp_file: $APP_FILE`nsdk: gradio`nsdk_version: 4.44.1`n---`n`n# $TITLE`n`nA vehicle management system built with Gradio."
Set-Content -Path "README.md" -Value $newReadmeContent
PrintGreen "Created README.md with proper metadata."
}
# Step 5: Check requirements.txt
PrintBlue "`nChecking requirements.txt..."
if (Test-Path "requirements.txt") {
PrintGreen "requirements.txt exists."
# Check for gradio in requirements
$requirementsContent = Get-Content "requirements.txt" -Raw
if ($requirementsContent -match "gradio") {
PrintGreen "Gradio is listed in requirements."
}
else {
PrintRed "Gradio may be missing in requirements.txt."
PrintBlue "Adding gradio to requirements.txt..."
Add-Content -Path "requirements.txt" -Value "gradio==4.44.1"
PrintGreen "Added gradio to requirements.txt"
}
}
else {
PrintRed "requirements.txt doesn't exist. Creating it..."
# Create a basic requirements.txt
$requirementsContent = "gradio==4.44.1`ngTTS==2.4.0`npygame==2.5.2"
Set-Content -Path "requirements.txt" -Value $requirementsContent
PrintGreen "Created requirements.txt with basic dependencies."
}
# Step 6: Check for the docs directory and data file
PrintBlue "`nChecking for docs directory and data file..."
if (Test-Path "docs") {
PrintGreen "docs directory exists."
if (Test-Path "docs\your_data.csv") {
PrintGreen "Data file exists."
}
else {
PrintRed "Data file 'docs\your_data.csv' not found."
PrintBlue "Please ensure your data file is in the correct location and properly named."
}
}
else {
PrintRed "docs directory doesn't exist. Creating it..."
New-Item -Path "docs" -ItemType Directory
PrintGreen "Created docs directory."
PrintBlue "Please place your data file as 'docs\your_data.csv'."
}
# Step 7: Create or update .gitignore
PrintBlue "`nChecking .gitignore..."
if (Test-Path ".gitignore") {
PrintGreen ".gitignore exists."
}
else {
PrintBlue "Creating .gitignore..."
$gitignoreContent = "__pycache__/`n*.py[cod]`n*`$py.class`n*.so`n.Python`nenv/`nbuild/`ndevelop-eggs/`ndist/`ndownloads/`neggs/`n.eggs/`nlib/`nlib64/`nparts/`nsdist/`nvar/`n*.egg-info/`n.installed.cfg`n*.egg`n.env`n.venv`nenv/`nvenv/`nENV/`nenv.bak/`nvenv.bak/`nstatic/*.mp3`nflagged/`n*.log"
Set-Content -Path ".gitignore" -Value $gitignoreContent
PrintGreen "Created .gitignore file."
}
# Step 8: Prepare for deployment
PrintBlue "`nPreparing for deployment..."
$SPACE_NAME = Read-Host "Enter your Hugging Face Space name (e.g., yourusername/app-name)"
if ([string]::IsNullOrEmpty($SPACE_NAME)) {
PrintRed "No Space name provided. Cannot continue with deployment."
exit 1
}
# Step 9: Attempt deployment
PrintBlue "`nAttempting to deploy to $SPACE_NAME..."
PrintBlue "This may take a few minutes..."
# Check if it's a Git repository
if (Test-Path ".git") {
PrintGreen "Git repository detected."
# Add all files
git add .
# Commit changes
git commit -m "Fix deployment configuration"
# Check if huggingface remote exists
$remotes = git remote
if ($remotes -notcontains "huggingface") {
git remote add huggingface "https://huggingface.co/spaces/$SPACE_NAME"
}
# Push to Hugging Face
PrintBlue "Pushing to Hugging Face..."
git push -f huggingface main
}
else {
PrintBlue "Not a Git repository. Using Hugging Face Hub API to deploy..."
# Use Hugging Face Hub API for deployment
$pythonScript = @"
from huggingface_hub import HfApi
import os
api = HfApi()
api.create_repo(
repo_id='$SPACE_NAME',
repo_type='space',
space_sdk='gradio',
private=False
)
api.upload_folder(
folder_path='.',
repo_id='$SPACE_NAME',
repo_type='space',
ignore_patterns=['.git', '.gitignore', '*.pyc', '__pycache__', '*.log']
)
print('Deployment completed!')
"@
# Save the Python script to a temporary file
$tempFile = [System.IO.Path]::GetTempFileName() + ".py"
Set-Content -Path $tempFile -Value $pythonScript
# Execute the Python script
python $tempFile
# Clean up the temporary file
Remove-Item $tempFile
}
PrintBlue "`nDeployment process completed!"
PrintBlue "Visit your Space at: https://huggingface.co/spaces/$SPACE_NAME"
PrintBlue "Note: It may take a few minutes for your Space to build and become available."
# Final message
PrintGreen "`n==================================="
PrintGreen "Deployment fix script completed!"
PrintGreen "==================================="