Spaces:
Sleeping
Sleeping
| title: Translator-API | |
| emoji: π» | |
| colorFrom: purple | |
| colorTo: gray | |
| sdk: docker | |
| pinned: true | |
| # π Translator API | |
| > **Alternative gratuite Γ Google Translate** β API REST sans clΓ©, auto-hΓ©bergΓ©e, avec dashboard admin temps rΓ©el. | |
| [](https://nodejs.org/) | |
| [](https://docker.com/) | |
| [](LICENSE) | |
| --- | |
| ## β¨ FonctionnalitΓ©s | |
| | FonctionnalitΓ© | Description | | |
| |---|---| | |
| | π **Gratuit & Sans clΓ© API** | Pas d'inscription, pas de quota mensuel | | |
| | β‘ **Rate limiting** | 30 requΓͺtes/minute par IP | | |
| | π **100+ langues** | Support complet ISO 639-1 + dΓ©tection auto | | |
| | π **Dashboard Admin** | MΓ©triques CPU/RAM temps rΓ©el, logs, graphiques 120s | | |
| | βοΈ **Architecture distribuΓ©e** | Central (API + 1 core) + Workers (1 core) | | |
| | π **Auth admin sΓ©curisΓ©e** | Code d'accΓ¨s via variables d'environnement | | |
| | π³ **Docker ready** | DΓ©ploiement 1-click sur HuggingFace Spaces | | |
| | π **Wiki intΓ©grΓ©** | Exemples JS, Python, Java, cURL | | |
| --- | |
| ## π DΓ©marrage rapide | |
| ### Option 1 : Docker Compose (RecommandΓ©) | |
| ```bash | |
| # 1. Cloner le repo | |
| git clone https://github.com/NathMen12/Translator-API.git | |
| cd Translator-API | |
| # 2. Configurer les secrets | |
| cp .env.example .env | |
| # Γditer .env avec votre ADMIN_ACCESS_CODE | |
| # 3. Lancer | |
| docker-compose up -d | |
| # 4. AccΓ©der Γ l'API | |
| curl -X POST http://localhost:7820/translate \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Bonjour le monde", "source": "fr", "target": "en"}' | |
| ``` | |
| ### Option 2 : DΓ©veloppement local | |
| ```bash | |
| # Installer les dΓ©pendances | |
| npm install | |
| # Lancer le central (terminal 1) | |
| npm run dev:central | |
| # Lancer le worker (terminal 2) | |
| npm run dev:worker | |
| # Test | |
| curl -X POST http://localhost:7820/translate \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Hello", "target": "fr"}' | |
| ``` | |
| --- | |
| ## π DΓ©ploiement sur HuggingFace Spaces | |
| 1. **Fork ce repo** sur votre GitHub | |
| 2. **CrΓ©ez un Space** sur [huggingface.co/new-space](https://huggingface.co/new-space) | |
| - SDK: **Docker** | |
| - Hardware: **CPU Basic (2 vCPU, 16 GB RAM)** β | |
| - Visibility: Public ou Private | |
| 3. **Ajoutez les Secrets** dans Settings β Repository secrets : | |
| - `ADMIN_ACCESS_CODE` = votre mot de passe admin fort | |
| - `TAILSCALE_API_KEY` = (optionnel) pour dΓ©couverte workers | |
| 4. **Push** β Le Space build et dΓ©ploie automatiquement ! | |
| > β οΈ Le port **7820** est exposΓ©. HuggingFace Spaces mappe automatiquement sur le port 7860 en externe. | |
| --- | |
| ## π Utilisation de l'API | |
| ### Endpoint principal | |
| ``` | |
| POST /translate | |
| Content-Type: application/json | |
| { | |
| "text": "Bonjour le monde", | |
| "source": "fr", // optionnel, dΓ©faut: "auto" | |
| "target": "en" // optionnel, dΓ©faut: "fr" | |
| } | |
| ``` | |
| ### RΓ©ponse | |
| ```json | |
| { | |
| "translatedText": "Hello world", | |
| "source": "fr", | |
| "target": "en", | |
| "duration": 245 | |
| } | |
| ``` | |
| ### Codes d'erreur | |
| | Code | Signification | | |
| |------|--------------| | |
| | 200 | Succès | | |
| | 400 | RequΓͺte invalide (texte manquant, trop long >5000 chars) | | |
| | 429 | Rate limit dΓ©passΓ© (30 req/min/IP) | | |
| | 500 | Erreur serveur | | |
| --- | |
| ## π» Exemples d'intΓ©gration | |
| ### JavaScript / TypeScript | |
| ```javascript | |
| // Fetch API (navigateur / Node 18+) | |
| async function translate(text, source = 'auto', target = 'fr') { | |
| const res = await fetch('https://VOTRE_SPACE.hf.space/translate', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ text, source, target }) | |
| }); | |
| if (!res.ok) throw new Error((await res.json()).error); | |
| return res.json(); | |
| } | |
| // Utilisation | |
| translate('Bonjour', 'fr', 'es').then(r => console.log(r.translatedText)); // "Hola" | |
| ``` | |
| ### Python | |
| ```python | |
| import requests | |
| def translate(text, source='auto', target='fr', base_url='https://VOTRE_SPACE.hf.space'): | |
| resp = requests.post(f'{base_url}/translate', | |
| json={'text': text, 'source': source, 'target': target}, timeout=30) | |
| resp.raise_for_status() | |
| return resp.json() | |
| # Usage | |
| print(translate('Hello world', 'en', 'fr')['translatedText']) # "Bonjour le monde" | |
| ``` | |
| ### Java (HttpClient 11+) | |
| ```java | |
| var client = HttpClient.newHttpClient(); | |
| var body = "{\"text\":\"Bonjour\",\"source\":\"fr\",\"target\":\"en\"}"; | |
| var request = HttpRequest.newBuilder() | |
| .uri(URI.create("https://VOTRE_SPACE.hf.space/translate")) | |
| .header("Content-Type", "application/json") | |
| .POST(HttpRequest.BodyPublishers.ofString(body)) | |
| .build(); | |
| var response = client.send(request, HttpResponse.BodyHandlers.ofString()); | |
| // Parse JSON pour obtenir translatedText | |
| ``` | |
| ### cURL | |
| ```bash | |
| curl -X POST https://VOTRE_SPACE.hf.space/translate \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"text": "Bonjour", "source": "fr", "target": "en"}' | |
| ``` | |
| > π **Plus d'exemples** : Visitez `/wiki` sur votre instance dΓ©ployΓ©e ! | |
| --- | |
| ## π Panel Admin | |
| AccΓ©dez Γ `https://VOTRE_SPACE.hf.space/admin` et entrez votre `ADMIN_ACCESS_CODE`. | |
| ### Onglets disponibles : | |
| | Onglet | Contenu | | |
| |--------|---------| | |
| | π₯οΈ **Machines** | CPU/RAM temps rΓ©el (central + workers), jobs actifs | | |
| | π **RequΓͺtes (120s)** | Graphique requΓͺtes/minute, stats, taux actuel | | |
| | π **Logs** | DerniΓ¨res traductions : IP, durΓ©e, langues, entrΓ©e/sortie | | |
| --- | |
| ## βοΈ Configuration | |
| ### Central (`central/settings.json`) | |
| ```json | |
| { | |
| "port": 7820, | |
| "rateLimit": { "maxRequestsPerMinutePerIP": 30, "windowMs": 60000 }, | |
| "scanLocalWorkers": true, | |
| "metricsWindowSeconds": 120, | |
| "maxLocalJobs": 4 | |
| } | |
| ``` | |
| ### Worker (`worker/settings.json`) | |
| ```json | |
| { | |
| "maxConcurrentJobs": 2, | |
| "centralHost": "localhost", | |
| "centralPort": 7820 | |
| } | |
| ``` | |
| ### Variables d'environnement (Secrets) | |
| | Variable | Requis | Description | | |
| |----------|--------|-------------| | |
| | `ADMIN_ACCESS_CODE` | β | Mot de passe admin (fort !) | | |
| | `TAILSCALE_API_KEY` | β | ClΓ© API Tailscale pour auto-dΓ©couverte workers | | |
| | `PORT` | β | Port d'Γ©coute (dΓ©faut: 7820) | | |
| --- | |
| ## ποΈ Architecture | |
| ``` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β HUGGINGFACE SPACE β | |
| β βββββββββββββββββββββββ ββββββββββββββββββββββ β | |
| β β CENTRAL β β WORKER β β | |
| β β (1.5 CPU / 12GB) ββββββ (0.5 CPU / 4GB) β β | |
| β β β’ Express API β WS β β’ Translation β β | |
| β β β’ Rate Limiting β β β’ Job Queue β β | |
| β β β’ Job Dispatch β β β’ Metrics Push β β | |
| β β β’ Admin Dashboard β β β β | |
| β β β’ Metrics Storage β β β β | |
| β βββββββββββββββββββββββ ββββββββββββββββββββββ β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| ``` | |
| - **Central** : Gère l'API HTTP, rate limiting, dispatch vers workers, dashboard | |
| - **Worker** : Se connecte en WebSocket, exΓ©cute les traductions, pousse mΓ©triques | |
| - **Communication** : WebSocket natif (ws) pour faible latence | |
| --- | |
| ## π¦ Structure du projet | |
| ``` | |
| Translator-API/ | |
| βββ central/ # Machine centrale | |
| β βββ src/index.js # Serveur Express + WS + Admin | |
| β βββ settings.json # Config centrale | |
| β βββ public/ # Frontend statique | |
| β β βββ index.html # Page d'accueil + test | |
| β β βββ admin.html # Dashboard admin (Chart.js) | |
| β β βββ wiki.html # Documentation intΓ©grΓ©e | |
| β βββ secrets/ # .env (ignorΓ© par git) | |
| βββ worker/ # Worker de traduction | |
| β βββ src/index.js # Client WS + file d'attente | |
| β βββ settings.json # Config worker | |
| βββ shared/ # Code partagΓ© (futur) | |
| βββ wiki/ # Docs markdown (source) | |
| βββ Dockerfile # Multi-stage build | |
| βββ docker-compose.yml # Orchestration locale | |
| βββ .env.example # Template secrets | |
| βββ package.json # DΓ©pendances root | |
| ``` | |
| --- | |
| ## π οΈ DΓ©veloppement | |
| ```bash | |
| # Installer tout | |
| npm run install:all | |
| # Central en mode watch | |
| npm run dev:central | |
| # Worker en mode watch | |
| npm run dev:worker | |
| # Tests | |
| npm test | |
| ``` | |
| ### Logs | |
| - Central : `central/logs/central.log` | |
| - Worker : `worker/logs/worker.log` | |
| --- | |
| ## π SΓ©curitΓ© | |
| - **Pas de clΓ© API publique** β Protection par rate limiting IP | |
| - **Admin protΓ©gΓ©** β Code fort dans variable d'environnement (pas dans le code) | |
| - **Secrets HF Spaces** β StockΓ©s chiffrΓ©s, injectΓ©s au runtime | |
| - **Non-root Docker** β User `nodejs` (UID 1001) | |
| - **Helmet/CORS** β Configurables selon besoins | |
| --- | |
| ## π Licence | |
| MIT License β Voir [LICENSE](LICENSE) | |
| --- | |
| ## π€ Contribution | |
| 1. Fork le projet | |
| 2. CrΓ©ez une branche (`git checkout -b feature/amazing`) | |
| 3. Committez (`git commit -m 'Add amazing feature'`) | |
| 4. Push (`git push origin feature/amazing`) | |
| 5. Ouvrez une Pull Request | |
| --- | |
| ## π Remerciements | |
| - [@vitalets/google-translate-api](https://github.com/vitalets/google-translate-api) β Moteur de traduction | |
| - [Chart.js](https://www.chartjs.org/) β Graphiques admin | |
| - [HuggingFace Spaces](https://huggingface.co/spaces) β HΓ©bergement gratuit | |
| --- | |
| <div align="center"> | |
| <strong>Fait avec β€οΈ par <a href="https://github.com/NathMen12">NathMen12</a></strong> | |
| <br> | |
| <a href="https://github.com/NathMen12/Translator-API">β Star sur GitHub</a> β’ | |
| <a href="https://github.com/NathMen12/Translator-API/issues">π Signaler un bug</a> β’ | |
| <a href="https://github.com/NathMen12/Translator-API/discussions">π¬ Discussions</a> | |
| </div> |