This project is a Django-based API that provides text translation services. The API accepts a POST request with text input in JSON format, processes the input, and returns the translated text in JSON format.
Follow these steps to set up the project on your local machine.
Make sure you have the following installed on your local machine:
- Python 3.x
- Git
- pip (Python package manager)
First, you need to clone the GitHub repository to your local machine. Run the following command in your terminal:
git clone https://github.com/LucaXYB/sign_languange_translation_api.git
Once the repository is cloned, navigate into the project directory:
cd sign_languange_translation_api
It's a good practice to use a virtual environment to isolate project dependencies. Run the following commands to set up and activate a virtual environment:
For macOS/Linux:
python3 -m venv venv
source venv/bin/activate
For Windows:
python -m venv venv
.\venv\Scripts\activate
Once the virtual environment is activated, install the project dependencies using the requirements.txt file:
pip install -r requirements.txt
This will install all the necessary libraries and packages to run the project.
After the dependencies are installed, you can run the Django development server:
python manage.py runserver
This will start the server at http://127.0.0.1:8000/.
By default, the API endpoint for text translation is available at:
POST http://127.0.0.1:8000/api/translate/
There are two ways to test this API: using Django's built-in test framework or using curl to manually send HTTP requests.
I finished the test code, you can use the following command to run the test suite:
python manage.py test
The test suite includes tests for:
- Validating successful text translation.
- Handling invalid JSON input.
- Handling cases where no text is provided.
- Ensuring only POST requests are accepted.
Expected Output
If all the tests pass, the expected output will look like this:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
....
----------------------------------------------------------------------
Ran 4 tests in 0.012s
OK
You can manually test the API using curl commands from the terminal. Below are some examples:
- Test a successful translation
curl -X POST http://127.0.0.1:8000/api/translate/ \
-H "Content-Type: application/json" \
-d '{"text": "hello"}'
Expected response:
{
"input_text": "hello",
"translated_text": "hello (translated)",
"message": "Translation successful"
}
- Test missing text input
curl -X POST http://127.0.0.1:8000/api/translate/ \
-H "Content-Type: application/json" \
-d '{}'
Expected response:
{
"error": "No text provided"
}
- Test invalid JSON input
curl -X POST http://127.0.0.1:8000/api/translate/ \
-H "Content-Type: application/json" \
-d 'invalid json'
Expected response:
{
"error": "Invalid JSON"
}
- Test invalid method (GET request)
curl -X GET http://127.0.0.1:8000/api/translate/
Expected response:
{
"error": "Invalid method"
}
Here's an overview of the project structure:
sign_language_translation_api/
├── manage.py
├── requirements.txt
├── translation/
│ ├── __init__.py
│ ├── views.py
│ ├── tests.py
│ ├── urls.py
│ ├── migrations/
- manage.py: The entry point to run and manage the Django project.
- requirements.txt: The file containing all project dependencies.
- translation/: Contains the views, tests, and URL configurations for the API.