File size: 5,198 Bytes
9699bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<#
.SYNOPSIS
    Deploy MediGuard AI to Hugging Face Spaces
.DESCRIPTION
    This script automates the deployment of MediGuard AI to Hugging Face Spaces.
    It handles copying files, setting up the Dockerfile, and pushing to the Space.
.PARAMETER SpaceName
    Name of your Hugging Face Space (e.g., "mediguard-ai")
.PARAMETER Username
    Your Hugging Face username
.PARAMETER SkipClone
    Skip cloning if you've already cloned the Space
.EXAMPLE
    .\deploy_huggingface.ps1 -Username "your-username" -SpaceName "mediguard-ai"
#>

param(
    [Parameter(Mandatory=$true)]
    [string]$Username,
    
    [Parameter(Mandatory=$false)]
    [string]$SpaceName = "mediguard-ai",
    
    [switch]$SkipClone
)

$ErrorActionPreference = "Stop"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host " MediGuard AI - Hugging Face Deployment" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Configuration
$ProjectRoot = Split-Path -Parent $PSScriptRoot
$DeployDir = Join-Path $ProjectRoot "hf-deploy"
$SpaceUrl = "https://huggingface.co/spaces/$Username/$SpaceName"

Write-Host "Project Root: $ProjectRoot" -ForegroundColor Gray
Write-Host "Deploy Dir: $DeployDir" -ForegroundColor Gray
Write-Host "Space URL: $SpaceUrl" -ForegroundColor Gray
Write-Host ""

# Step 1: Clone or use existing Space
if (-not $SkipClone) {
    Write-Host "[1/6] Cloning Hugging Face Space..." -ForegroundColor Yellow
    
    if (Test-Path $DeployDir) {
        Write-Host "  Removing existing deploy directory..." -ForegroundColor Gray
        Remove-Item -Recurse -Force $DeployDir
    }
    
    git clone "https://huggingface.co/spaces/$Username/$SpaceName" $DeployDir
    
    if ($LASTEXITCODE -ne 0) {
        Write-Host "ERROR: Failed to clone Space. Make sure it exists!" -ForegroundColor Red
        Write-Host "Create it at: https://huggingface.co/new-space" -ForegroundColor Yellow
        exit 1
    }
} else {
    Write-Host "[1/6] Using existing deploy directory..." -ForegroundColor Yellow
}

# Step 2: Copy project files
Write-Host "[2/6] Copying project files..." -ForegroundColor Yellow

# Core directories
$CoreDirs = @("src", "config", "data", "huggingface")
foreach ($dir in $CoreDirs) {
    $source = Join-Path $ProjectRoot $dir
    $dest = Join-Path $DeployDir $dir
    if (Test-Path $source) {
        Write-Host "  Copying $dir..." -ForegroundColor Gray
        Copy-Item -Path $source -Destination $dest -Recurse -Force
    }
}

# Copy specific files
$CoreFiles = @("pyproject.toml", ".dockerignore")
foreach ($file in $CoreFiles) {
    $source = Join-Path $ProjectRoot $file
    if (Test-Path $source) {
        Write-Host "  Copying $file..." -ForegroundColor Gray
        Copy-Item -Path $source -Destination (Join-Path $DeployDir $file) -Force
    }
}

# Step 3: Set up Dockerfile (HF Spaces expects it in root)
Write-Host "[3/6] Setting up Dockerfile..." -ForegroundColor Yellow
$HfDockerfile = Join-Path $DeployDir "huggingface/Dockerfile"
$RootDockerfile = Join-Path $DeployDir "Dockerfile"
Copy-Item -Path $HfDockerfile -Destination $RootDockerfile -Force
Write-Host "  Copied huggingface/Dockerfile to Dockerfile" -ForegroundColor Gray

# Step 4: Set up README with HF metadata
Write-Host "[4/6] Setting up README.md..." -ForegroundColor Yellow
$HfReadme = Join-Path $DeployDir "huggingface/README.md"
$RootReadme = Join-Path $DeployDir "README.md"
Copy-Item -Path $HfReadme -Destination $RootReadme -Force
Write-Host "  Copied huggingface/README.md to README.md" -ForegroundColor Gray

# Step 5: Verify vector store exists
Write-Host "[5/6] Verifying vector store..." -ForegroundColor Yellow
$VectorStore = Join-Path $DeployDir "data/vector_stores/medical_knowledge.faiss"
if (Test-Path $VectorStore) {
    $size = (Get-Item $VectorStore).Length / 1MB
    Write-Host "  Vector store found: $([math]::Round($size, 2)) MB" -ForegroundColor Green
} else {
    Write-Host "  WARNING: Vector store not found!" -ForegroundColor Red
    Write-Host "  Run 'python scripts/setup_embeddings.py' first to create it." -ForegroundColor Yellow
}

# Step 6: Commit and push
Write-Host "[6/6] Committing and pushing to Hugging Face..." -ForegroundColor Yellow

Push-Location $DeployDir

git add .
git commit -m "Deploy MediGuard AI - $(Get-Date -Format 'yyyy-MM-dd HH:mm')"

Write-Host ""
Write-Host "Ready to push! Run the following command:" -ForegroundColor Green
Write-Host ""
Write-Host "  cd $DeployDir" -ForegroundColor Cyan
Write-Host "  git push" -ForegroundColor Cyan
Write-Host ""
Write-Host "After pushing, add your API key as a Secret in Space Settings:" -ForegroundColor Yellow
Write-Host "  Name: GROQ_API_KEY  (or GOOGLE_API_KEY)" -ForegroundColor Gray
Write-Host "  Value: your-api-key" -ForegroundColor Gray
Write-Host ""
Write-Host "Your Space will be live at:" -ForegroundColor Green
Write-Host "  $SpaceUrl" -ForegroundColor Cyan

Pop-Location

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Deployment prepared successfully!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan