File size: 4,487 Bytes
dd87944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Configure les clés LLM manquantes dans emo/backend/.env puis sync HF Space.
# Usage : powershell -ExecutionPolicy Bypass -File scripts\setup-llm-keys.ps1

$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$EnvFile = Join-Path $Root "emo\backend\.env"
$Example = Join-Path $Root "emo\backend\.env.example"

Write-Host ""
Write-Host "=== Emo Online — Ajout des clés LLM ===" -ForegroundColor Cyan
Write-Host ""

if (-not (Test-Path $EnvFile)) {
    if (Test-Path $Example) {
        Copy-Item $Example $EnvFile
        Write-Host "Créé $EnvFile depuis .env.example" -ForegroundColor Yellow
    } else {
        New-Item -ItemType File -Path $EnvFile | Out-Null
    }
}

function Get-EnvValue($key) {
    if (-not (Test-Path $EnvFile)) { return "" }
    foreach ($line in Get-Content $EnvFile) {
        if ($line -match "^\s*$key=(.*)$") { return $Matches[1].Trim().Trim('"') }
    }
    return ""
}

function Set-EnvValue($key, $value) {
    $lines = @()
    $found = $false
    if (Test-Path $EnvFile) { $lines = Get-Content $EnvFile }
    $newLines = @()
    foreach ($line in $lines) {
        if ($line -match "^\s*$key=") {
            if ($value) { $newLines += "$key=$value" }
            $found = $true
        } else {
            $newLines += $line
        }
    }
    if (-not $found -and $value) { $newLines += "$key=$value" }
    $newLines | Set-Content -Path $EnvFile -Encoding UTF8
}

$prompts = @(
    @{
        Key = "OPENROUTER_API_KEY"
        Label = "OpenRouter"
        Url = "https://openrouter.ai/keys"
        Hint = "Gratuit : Llama 3.3, Gemma 2, Qwen 2.5 (suffixe :free)"
    },
    @{
        Key = "DEEPSEEK_API_KEY"
        Label = "DeepSeek"
        Url = "https://platform.deepseek.com/api_keys"
        Hint = "DeepSeek Chat + Reasoner (R1)"
    },
    @{
        Key = "GROQ_API_KEY"
        Label = "Groq"
        Url = "https://console.groq.com/keys"
        Hint = "Llama / Gemma / Mixtral — tier gratuit"
    },
    @{
        Key = "HF_TOKEN"
        Label = "Hugging Face (HF_TOKEN)"
        Url = "https://huggingface.co/settings/tokens"
        Hint = "Token Read — modèles Llama + Kimi via router HF"
    },
    @{
        Key = "GEMINI_API_KEY"
        Label = "Google Gemini"
        Url = "https://aistudio.google.com/apikey"
        Hint = "Gemini 2.0 Flash (quota free)"
    },
    @{
        Key = "OPENAI_API_KEY"
        Label = "OpenAI"
        Url = "https://platform.openai.com/api-keys"
        Hint = "GPT-4o mini — crédits requis"
    },
    @{
        Key = "ANTHROPIC_API_KEY"
        Label = "Anthropic"
        Url = "https://console.anthropic.com/settings/keys"
        Hint = "Claude Haiku / Sonnet — crédits requis"
    }
)

foreach ($p in $prompts) {
    $current = Get-EnvValue $p.Key
    if ($current) {
        Write-Host "[OK] $($p.Label) déjà dans .env" -ForegroundColor Green
        continue
    }
    Write-Host ""
    Write-Host "--- $($p.Label) ---" -ForegroundColor White
    Write-Host $p.Hint
    Write-Host "Obtenir : $($p.Url)" -ForegroundColor DarkGray
    $open = Read-Host "Ouvrir le lien dans le navigateur ? (O/n)"
    if ($open -ne "n" -and $open -ne "N") {
        Start-Process $p.Url
    }
    $val = Read-Host "Colle la clé (Entrée vide = ignorer)"
    if ($val.Trim()) {
        Set-EnvValue $p.Key $val.Trim()
        Write-Host "  -> enregistré dans .env" -ForegroundColor Green
    }
}

# URLs prod
Set-EnvValue "EMO_PUBLIC_BACKEND_URL" "https://xroxx-emo-online-api.hf.space"
Set-EnvValue "EMO_FRONTEND_URL" "https://xeroxytb.com"
Set-EnvValue "CORS_ORIGINS" "https://xeroxytb.com,https://www.xeroxytb.com,https://xeroxytb.github.io"

Write-Host ""
$sync = Read-Host "Synchroniser vers Hugging Face Space maintenant ? (O/n)"
if ($sync -ne "n" -and $sync -ne "N") {
    $hf = Get-EnvValue "HF_TOKEN"
    if (-not $hf) {
        Write-Host "HF_TOKEN requis pour sync HF (Settings > Access Tokens sur huggingface.co)" -ForegroundColor Yellow
        $hf = Read-Host "Colle ton HF_TOKEN (Write)"
        if ($hf.Trim()) { Set-EnvValue "HF_TOKEN" $hf.Trim() }
    }
    if (Get-EnvValue "HF_TOKEN") {
        & python (Join-Path $Root "scripts\sync-hf-secrets.py")
    }
}

Write-Host ""
Write-Host "Vérification :" -ForegroundColor Cyan
& python (Join-Path $Root "scripts\check-llm-keys.py")
Write-Host ""
Write-Host "Redémarre le Space HF après sync (Settings > Restart) si besoin." -ForegroundColor DarkGray