This project is a Django-based API with machine learning capabilities using Transformers and PyTorch. The application includes authentication, CORS support, and JWT for secure API access.
- Python 3.10 or higher
- Docker and Docker Compose
- Virtual environment (optional, but recommended)
Ensure to set the following environment variables in your .env file (if applicable):
SECRET_KEY: Django secret key for cryptographic signing.DEBUG: Set toTruefor development;Falsefor production.
You can test the API using tools like Postman or directly via curl. Below are examples of how to make requests using curl.
- Django==4.2
- djangorestframework==3.14.0
- transformers==4.31.0
- torch==2.0.1
- django-cors-headers==4.4.0
- djangorestframework-simplejwt==5.3.1
- langdetect==1.0.9
- Clone the repository
git clone https://github.com/your_username/trans_ai_api.git
cd trans_ai_apiTo install the necessary dependencies, you can run:
pip install -r requirements.txt
python manage.py migrate
python manage.py runserverThe api will be accessible at [http://localhost:8000]
docker login
docker-compose build
docker-compose -f docker-compose.yml up-
Translate
- Endpoint:
/api/translate/ - Method:
POST - Description: Detect Language of user input. Detect System Language of user. Translates the user input text into detected language or user choosen target language or default target lang 'eng-Latn'. Requires Access Token in header, Auth type Bearer Token. Requires source_text and target_lang.
- Endpoint:
-
User Registration
- Endpoint:
/api/auth/register/ - Method:
POST - Description: Registers a new user, generate refresh token and access token. Requires user details username and password in the request body.
- Endpoint:
-
Obtain Token
- Endpoint:
/api/auth/obtaintoken/ - Method:
POST - Description: Obtains access token and refresh token to registered user. Requires username and password.
- Endpoint:
-
Token Refresh
- Endpoint:
/api/token/refresh/ - Method:
POST - Description: Refreshes the JWT token using a valid refresh token. Requires refresh token.
- Endpoint:
-
Translating Text: To translate text, send a
POSTrequest to/api/translate/with the necessary payload. -
Mutil Language Audio Detect: To detect audio, send a
POSTrequest and auiod file to/api/translate/with the necessary payload. -
Registering a User: Send a
POSTrequest to/api/auth/register/with the user's information (username, password) to register and get refresh token and access token. -
Obtaining a Token: Authenticate a user by sending a
POSTrequest to/api/auth/obtaintoken/with the username and password. This will return a JWT token. -
Refreshing the Token: Use a valid refresh token to get a new JWT by sending a
POSTrequest to/api/token/refresh/.
{
"username": "testname9",
"password": "Testname@123"
}curl -X POST http://localhost:8000/api/auth/register/ \
-H "Content-Type: application/json" \
-d '{"username": "testname9", "password": "Testname@123"}'output:
{
"user": {
"username": "testname9"
},
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTcyOTY0OTI0MSwiaWF0IjoxNzI5NTYyODQxLCJqdGkiOiJiYjQ4ZmQyOGU4MTE0Yzk3OTVmNjk1MGY4MGFiOGY1YSIsInVzZXJfaWQiOjEzfQ.aeI0TirkIkepYWAdgm02qafZGYDz8hC0hokb08nybdI",
"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzMyMTU0ODQxLCJpYXQiOjE3Mjk1NjI4NDEsImp0aSI6IjkxZmZhOWE1MzZhMDRjYzQ5MjIyMmJmMjY2MDFhOTBkIiwidXNlcl9pZCI6MTN9.K0hQuuSKAMptsGJlCj6Tug5tnknyCF8u6EfGCrVnizI",
"message": "User registered successfully"
}{
"username" : "testname9",
"password" : "Testname@123"
}curl -X POST http://localhost:8000/api/auth/obtaintoken/ \
-H "Content-Type: application/json" \
-d '{"username": "testname9", "password": "Testname@123"}'output:
{
"user": {
"username": "testname9"
},
"refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTcyOTY1MjA0MCwiaWF0IjoxNzI5NTY1NjQwLCJqdGkiOiIwZjcwMDExYTEzMjI0MDUwOGNmZjIwNTE4YmNkY2FkMCIsInVzZXJfaWQiOjEzfQ.eIGQyCHlc3D5LHw_jHa5wH5ZUqA8nkEKRIRiwfUTI5E",
"access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzI5NTY1OTQwLCJpYXQiOjE3Mjk1NjU2NDAsImp0aSI6ImY4NzExNGY1YWUwZjRhZGRhMGMzNTdlYmM3N2UxNGY2IiwidXNlcl9pZCI6MTN9.J8jOOnERIwHABNop42sDVmnGlgcavGDU44nKu_XRVXs",
"message": "Token generated successfully"
}{
"source_text": "El rápido zorro marrón salta sobre el perro perezoso",
"target_lang": "ace_Arab"
}curl -X POST http://localhost:8000/api/translate/ \
-H "Authorization: Bearer <your_token>" \
-H "Content-Type: application/json" \
-d '{"text": "Hello, world!", "target_language": "fr"}'output:
{
"source_text": "El rápido zorro marrón salta sobre el perro perezoso",
"source_lang": "es",
"target_lang": "eng_Latn",
"trans_text": "The fast brown fox jumps over the slothful dog."
}This project is licensed under the MIT License - see the LICENSE file for details.