This is a Flask API that translates text from French to English using the Hugging Face translation model. The API supports caching using Redis to improve response times.
- Python (version 3.6 or higher)
- Redis Server
-
Clone the repository:
git clone https://github.com/yourusername/flask-huggingface-translation.git cd flask-huggingface-translation
-
Install the required Python packages:
pip install -r requirements.txt
- Linux / Mac:
redis-server
- Set up a Hugging Face account and obtain an API token.
- Start the Redis server on your machine.
Create a .env
file in the project root with the following content:
HUGGING_FACE_API_TOKEN=your_hugging_face_api_token
REDIS_URL=redis://localhost:6379/0
Replace your_hugging_face_api_token
with your actual Hugging Face API token.
-
Run the Flask app:
python app.py
The app will be accessible at
http://127.0.0.1:5000/
. -
Make a POST request to
/translate
with the text to be translated:Example using
curl
:curl -X POST -H "Content-Type: application/json" -d '{"text": "Bonjour, comment allez-vous?"}' http://127.0.0.1:5000/translate
Example using ThunderClient in Visual Studio Code:
POST http://127.0.0.1:5000/translate Content-Type: application/json { "text": "Le chat noir a couru à travers le jardin et a sauté sur le muret en pierre" }
Output:
{ "translation": "\"The black cat has run through the garden and has jumped on the stone wall." }
-
The API will respond with the translated text.
The API supports caching using Redis to improve response times. Translations are cached for 1 hour.
Feel free to contribute to the project by opening issues or pull requests.