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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Translator-API
|
| 3 |
+
emoji: π»
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
+
pinned: true
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# π Translator API
|
| 11 |
+
|
| 12 |
+
> **Alternative gratuite Γ Google Translate** β API REST sans clΓ©, auto-hΓ©bergΓ©e, avec dashboard admin temps rΓ©el.
|
| 13 |
+
|
| 14 |
+
[](https://nodejs.org/)
|
| 15 |
+
[](https://docker.com/)
|
| 16 |
+
[](LICENSE)
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## β¨ FonctionnalitΓ©s
|
| 21 |
+
|
| 22 |
+
| FonctionnalitΓ© | Description |
|
| 23 |
+
|---|---|
|
| 24 |
+
| π **Gratuit & Sans clΓ© API** | Pas d'inscription, pas de quota mensuel |
|
| 25 |
+
| β‘ **Rate limiting** | 30 requΓͺtes/minute par IP |
|
| 26 |
+
| π **100+ langues** | Support complet ISO 639-1 + dΓ©tection auto |
|
| 27 |
+
| π **Dashboard Admin** | MΓ©triques CPU/RAM temps rΓ©el, logs, graphiques 120s |
|
| 28 |
+
| βοΈ **Architecture distribuΓ©e** | Central (API + 1 core) + Workers (1 core) |
|
| 29 |
+
| π **Auth admin sΓ©curisΓ©e** | Code d'accΓ¨s via variables d'environnement |
|
| 30 |
+
| π³ **Docker ready** | DΓ©ploiement 1-click sur HuggingFace Spaces |
|
| 31 |
+
| π **Wiki intΓ©grΓ©** | Exemples JS, Python, Java, cURL |
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
## π DΓ©marrage rapide
|
| 36 |
+
|
| 37 |
+
### Option 1 : Docker Compose (RecommandΓ©)
|
| 38 |
+
|
| 39 |
+
```bash
|
| 40 |
+
# 1. Cloner le repo
|
| 41 |
+
git clone https://github.com/NathMen12/Translator-API.git
|
| 42 |
+
cd Translator-API
|
| 43 |
+
|
| 44 |
+
# 2. Configurer les secrets
|
| 45 |
+
cp .env.example .env
|
| 46 |
+
# Γditer .env avec votre ADMIN_ACCESS_CODE
|
| 47 |
+
|
| 48 |
+
# 3. Lancer
|
| 49 |
+
docker-compose up -d
|
| 50 |
+
|
| 51 |
+
# 4. AccΓ©der Γ l'API
|
| 52 |
+
curl -X POST http://localhost:7820/translate \
|
| 53 |
+
-H "Content-Type: application/json" \
|
| 54 |
+
-d '{"text": "Bonjour le monde", "source": "fr", "target": "en"}'
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### Option 2 : DΓ©veloppement local
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
# Installer les dΓ©pendances
|
| 61 |
+
npm install
|
| 62 |
+
|
| 63 |
+
# Lancer le central (terminal 1)
|
| 64 |
+
npm run dev:central
|
| 65 |
+
|
| 66 |
+
# Lancer le worker (terminal 2)
|
| 67 |
+
npm run dev:worker
|
| 68 |
+
|
| 69 |
+
# Test
|
| 70 |
+
curl -X POST http://localhost:7820/translate \
|
| 71 |
+
-H "Content-Type: application/json" \
|
| 72 |
+
-d '{"text": "Hello", "target": "fr"}'
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
---
|
| 76 |
+
|
| 77 |
+
## π DΓ©ploiement sur HuggingFace Spaces
|
| 78 |
+
|
| 79 |
+
1. **Fork ce repo** sur votre GitHub
|
| 80 |
+
2. **CrΓ©ez un Space** sur [huggingface.co/new-space](https://huggingface.co/new-space)
|
| 81 |
+
- SDK: **Docker**
|
| 82 |
+
- Hardware: **CPU Basic (2 vCPU, 16 GB RAM)** β
|
| 83 |
+
- Visibility: Public ou Private
|
| 84 |
+
3. **Ajoutez les Secrets** dans Settings β Repository secrets :
|
| 85 |
+
- `ADMIN_ACCESS_CODE` = votre mot de passe admin fort
|
| 86 |
+
- `TAILSCALE_API_KEY` = (optionnel) pour dΓ©couverte workers
|
| 87 |
+
4. **Push** β Le Space build et dΓ©ploie automatiquement !
|
| 88 |
+
|
| 89 |
+
> β οΈ Le port **7820** est exposΓ©. HuggingFace Spaces mappe automatiquement sur le port 7860 en externe.
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
## π Utilisation de l'API
|
| 94 |
+
|
| 95 |
+
### Endpoint principal
|
| 96 |
+
|
| 97 |
+
```
|
| 98 |
+
POST /translate
|
| 99 |
+
Content-Type: application/json
|
| 100 |
+
|
| 101 |
+
{
|
| 102 |
+
"text": "Bonjour le monde",
|
| 103 |
+
"source": "fr", // optionnel, dΓ©faut: "auto"
|
| 104 |
+
"target": "en" // optionnel, dΓ©faut: "fr"
|
| 105 |
+
}
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### RΓ©ponse
|
| 109 |
+
|
| 110 |
+
```json
|
| 111 |
+
{
|
| 112 |
+
"translatedText": "Hello world",
|
| 113 |
+
"source": "fr",
|
| 114 |
+
"target": "en",
|
| 115 |
+
"duration": 245
|
| 116 |
+
}
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
### Codes d'erreur
|
| 120 |
+
|
| 121 |
+
| Code | Signification |
|
| 122 |
+
|------|--------------|
|
| 123 |
+
| 200 | Succès |
|
| 124 |
+
| 400 | RequΓͺte invalide (texte manquant, trop long >5000 chars) |
|
| 125 |
+
| 429 | Rate limit dΓ©passΓ© (30 req/min/IP) |
|
| 126 |
+
| 500 | Erreur serveur |
|
| 127 |
+
|
| 128 |
+
---
|
| 129 |
+
|
| 130 |
+
## π» Exemples d'intΓ©gration
|
| 131 |
+
|
| 132 |
+
### JavaScript / TypeScript
|
| 133 |
+
|
| 134 |
+
```javascript
|
| 135 |
+
// Fetch API (navigateur / Node 18+)
|
| 136 |
+
async function translate(text, source = 'auto', target = 'fr') {
|
| 137 |
+
const res = await fetch('https://VOTRE_SPACE.hf.space/translate', {
|
| 138 |
+
method: 'POST',
|
| 139 |
+
headers: { 'Content-Type': 'application/json' },
|
| 140 |
+
body: JSON.stringify({ text, source, target })
|
| 141 |
+
});
|
| 142 |
+
|
| 143 |
+
if (!res.ok) throw new Error((await res.json()).error);
|
| 144 |
+
return res.json();
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
// Utilisation
|
| 148 |
+
translate('Bonjour', 'fr', 'es').then(r => console.log(r.translatedText)); // "Hola"
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
### Python
|
| 152 |
+
|
| 153 |
+
```python
|
| 154 |
+
import requests
|
| 155 |
+
|
| 156 |
+
def translate(text, source='auto', target='fr', base_url='https://VOTRE_SPACE.hf.space'):
|
| 157 |
+
resp = requests.post(f'{base_url}/translate',
|
| 158 |
+
json={'text': text, 'source': source, 'target': target}, timeout=30)
|
| 159 |
+
resp.raise_for_status()
|
| 160 |
+
return resp.json()
|
| 161 |
+
|
| 162 |
+
# Usage
|
| 163 |
+
print(translate('Hello world', 'en', 'fr')['translatedText']) # "Bonjour le monde"
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
### Java (HttpClient 11+)
|
| 167 |
+
|
| 168 |
+
```java
|
| 169 |
+
var client = HttpClient.newHttpClient();
|
| 170 |
+
var body = "{\"text\":\"Bonjour\",\"source\":\"fr\",\"target\":\"en\"}";
|
| 171 |
+
var request = HttpRequest.newBuilder()
|
| 172 |
+
.uri(URI.create("https://VOTRE_SPACE.hf.space/translate"))
|
| 173 |
+
.header("Content-Type", "application/json")
|
| 174 |
+
.POST(HttpRequest.BodyPublishers.ofString(body))
|
| 175 |
+
.build();
|
| 176 |
+
|
| 177 |
+
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
| 178 |
+
// Parse JSON pour obtenir translatedText
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
### cURL
|
| 182 |
+
|
| 183 |
+
```bash
|
| 184 |
+
curl -X POST https://VOTRE_SPACE.hf.space/translate \
|
| 185 |
+
-H "Content-Type: application/json" \
|
| 186 |
+
-d '{"text": "Bonjour", "source": "fr", "target": "en"}'
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
> π **Plus d'exemples** : Visitez `/wiki` sur votre instance dΓ©ployΓ©e !
|
| 190 |
+
|
| 191 |
+
---
|
| 192 |
+
|
| 193 |
+
## π Panel Admin
|
| 194 |
+
|
| 195 |
+
AccΓ©dez Γ `https://VOTRE_SPACE.hf.space/admin` et entrez votre `ADMIN_ACCESS_CODE`.
|
| 196 |
+
|
| 197 |
+
### Onglets disponibles :
|
| 198 |
+
|
| 199 |
+
| Onglet | Contenu |
|
| 200 |
+
|--------|---------|
|
| 201 |
+
| π₯οΈ **Machines** | CPU/RAM temps rΓ©el (central + workers), jobs actifs |
|
| 202 |
+
| π **RequΓͺtes (120s)** | Graphique requΓͺtes/minute, stats, taux actuel |
|
| 203 |
+
| π **Logs** | DerniΓ¨res traductions : IP, durΓ©e, langues, entrΓ©e/sortie |
|
| 204 |
+
|
| 205 |
+
---
|
| 206 |
+
|
| 207 |
+
## βοΈ Configuration
|
| 208 |
+
|
| 209 |
+
### Central (`central/settings.json`)
|
| 210 |
+
|
| 211 |
+
```json
|
| 212 |
+
{
|
| 213 |
+
"port": 7820,
|
| 214 |
+
"rateLimit": { "maxRequestsPerMinutePerIP": 30, "windowMs": 60000 },
|
| 215 |
+
"scanLocalWorkers": true,
|
| 216 |
+
"metricsWindowSeconds": 120,
|
| 217 |
+
"maxLocalJobs": 4
|
| 218 |
+
}
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
### Worker (`worker/settings.json`)
|
| 222 |
+
|
| 223 |
+
```json
|
| 224 |
+
{
|
| 225 |
+
"maxConcurrentJobs": 2,
|
| 226 |
+
"centralHost": "localhost",
|
| 227 |
+
"centralPort": 7820
|
| 228 |
+
}
|
| 229 |
+
```
|
| 230 |
+
|
| 231 |
+
### Variables d'environnement (Secrets)
|
| 232 |
+
|
| 233 |
+
| Variable | Requis | Description |
|
| 234 |
+
|----------|--------|-------------|
|
| 235 |
+
| `ADMIN_ACCESS_CODE` | β
| Mot de passe admin (fort !) |
|
| 236 |
+
| `TAILSCALE_API_KEY` | β | ClΓ© API Tailscale pour auto-dΓ©couverte workers |
|
| 237 |
+
| `PORT` | β | Port d'Γ©coute (dΓ©faut: 7820) |
|
| 238 |
+
|
| 239 |
+
---
|
| 240 |
+
|
| 241 |
+
## ποΈ Architecture
|
| 242 |
+
|
| 243 |
+
```
|
| 244 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 245 |
+
β HUGGINGFACE SPACE β
|
| 246 |
+
β βββββββββββββββββββββββ ββββββββββββββββββββββ β
|
| 247 |
+
β β CENTRAL β β WORKER β β
|
| 248 |
+
β β (1.5 CPU / 12GB) ββββββ (0.5 CPU / 4GB) β β
|
| 249 |
+
β β β’ Express API β WS β β’ Translation β β
|
| 250 |
+
β β β’ Rate Limiting β β β’ Job Queue β β
|
| 251 |
+
β β β’ Job Dispatch β β β’ Metrics Push β β
|
| 252 |
+
β β β’ Admin Dashboard β β β β
|
| 253 |
+
β β β’ Metrics Storage β β β β
|
| 254 |
+
β βββββββββββββββββββββββ ββββββββββββββββββββββ β
|
| 255 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 256 |
+
```
|
| 257 |
+
|
| 258 |
+
- **Central** : Gère l'API HTTP, rate limiting, dispatch vers workers, dashboard
|
| 259 |
+
- **Worker** : Se connecte en WebSocket, exΓ©cute les traductions, pousse mΓ©triques
|
| 260 |
+
- **Communication** : WebSocket natif (ws) pour faible latence
|
| 261 |
+
|
| 262 |
+
---
|
| 263 |
+
|
| 264 |
+
## π¦ Structure du projet
|
| 265 |
+
|
| 266 |
+
```
|
| 267 |
+
Translator-API/
|
| 268 |
+
βββ central/ # Machine centrale
|
| 269 |
+
β βββ src/index.js # Serveur Express + WS + Admin
|
| 270 |
+
β βββ settings.json # Config centrale
|
| 271 |
+
β βββ public/ # Frontend statique
|
| 272 |
+
β β βββ index.html # Page d'accueil + test
|
| 273 |
+
β β βββ admin.html # Dashboard admin (Chart.js)
|
| 274 |
+
β β βββ wiki.html # Documentation intΓ©grΓ©e
|
| 275 |
+
β βββ secrets/ # .env (ignorΓ© par git)
|
| 276 |
+
βββ worker/ # Worker de traduction
|
| 277 |
+
β βββ src/index.js # Client WS + file d'attente
|
| 278 |
+
β βββ settings.json # Config worker
|
| 279 |
+
βββ shared/ # Code partagΓ© (futur)
|
| 280 |
+
βββ wiki/ # Docs markdown (source)
|
| 281 |
+
βββ Dockerfile # Multi-stage build
|
| 282 |
+
βββ docker-compose.yml # Orchestration locale
|
| 283 |
+
βββ .env.example # Template secrets
|
| 284 |
+
βββ package.json # DΓ©pendances root
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
---
|
| 288 |
+
|
| 289 |
+
## π οΈ DΓ©veloppement
|
| 290 |
+
|
| 291 |
+
```bash
|
| 292 |
+
# Installer tout
|
| 293 |
+
npm run install:all
|
| 294 |
+
|
| 295 |
+
# Central en mode watch
|
| 296 |
+
npm run dev:central
|
| 297 |
+
|
| 298 |
+
# Worker en mode watch
|
| 299 |
+
npm run dev:worker
|
| 300 |
+
|
| 301 |
+
# Tests
|
| 302 |
+
npm test
|
| 303 |
+
```
|
| 304 |
+
|
| 305 |
+
### Logs
|
| 306 |
+
|
| 307 |
+
- Central : `central/logs/central.log`
|
| 308 |
+
- Worker : `worker/logs/worker.log`
|
| 309 |
+
|
| 310 |
+
---
|
| 311 |
+
|
| 312 |
+
## π SΓ©curitΓ©
|
| 313 |
+
|
| 314 |
+
- **Pas de clΓ© API publique** β Protection par rate limiting IP
|
| 315 |
+
- **Admin protΓ©gΓ©** β Code fort dans variable d'environnement (pas dans le code)
|
| 316 |
+
- **Secrets HF Spaces** β StockΓ©s chiffrΓ©s, injectΓ©s au runtime
|
| 317 |
+
- **Non-root Docker** β User `nodejs` (UID 1001)
|
| 318 |
+
- **Helmet/CORS** β Configurables selon besoins
|
| 319 |
+
|
| 320 |
+
---
|
| 321 |
+
|
| 322 |
+
## π Licence
|
| 323 |
+
|
| 324 |
+
MIT License β Voir [LICENSE](LICENSE)
|
| 325 |
+
|
| 326 |
+
---
|
| 327 |
+
|
| 328 |
+
## π€ Contribution
|
| 329 |
+
|
| 330 |
+
1. Fork le projet
|
| 331 |
+
2. CrΓ©ez une branche (`git checkout -b feature/amazing`)
|
| 332 |
+
3. Committez (`git commit -m 'Add amazing feature'`)
|
| 333 |
+
4. Push (`git push origin feature/amazing`)
|
| 334 |
+
5. Ouvrez une Pull Request
|
| 335 |
+
|
| 336 |
+
---
|
| 337 |
+
|
| 338 |
+
## π Remerciements
|
| 339 |
+
|
| 340 |
+
- [@vitalets/google-translate-api](https://github.com/vitalets/google-translate-api) β Moteur de traduction
|
| 341 |
+
- [Chart.js](https://www.chartjs.org/) β Graphiques admin
|
| 342 |
+
- [HuggingFace Spaces](https://huggingface.co/spaces) β HΓ©bergement gratuit
|
| 343 |
+
|
| 344 |
+
---
|
| 345 |
+
|
| 346 |
+
<div align="center">
|
| 347 |
+
<strong>Fait avec β€οΈ par <a href="https://github.com/NathMen12">NathMen12</a></strong>
|
| 348 |
+
<br>
|
| 349 |
+
<a href="https://github.com/NathMen12/Translator-API">β Star sur GitHub</a> β’
|
| 350 |
+
<a href="https://github.com/NathMen12/Translator-API/issues">π Signaler un bug</a> β’
|
| 351 |
+
<a href="https://github.com/NathMen12/Translator-API/discussions">π¬ Discussions</a>
|
| 352 |
+
</div>
|