File size: 9,052 Bytes
3b2eb38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# 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 "==================================="