A web-based application that translates text between multiple languages (English, Chinese, Spanish, Hindi, and Myanmar) with an option for reverse translation. The app utilizes OpenAI's GPT-4 model via the LangChain framework to handle the translations and provides real-time feedback in a clean, user-friendly interface. The application includes rate limiting to prevent excessive requests and informs users about their remaining requests.
- Supports translation from English to:
- Chinese
- Spanish
- Hindi
- Myanmar
- Reverse translation option (translate back to English)
- Rate Limiting:
- Limits each IP to 5 requests per 5 minutes
- Displays remaining requests to the user
- Informs users when the limit is exceeded and how long to wait
- Clean, responsive user interface with loading indicators
- In-memory caching of translations to avoid redundant requests
- Secure and efficient backend using Node.js, Express, and Helmet for security
- Detailed logging of requests and errors using Winston and Morgan
AS A developer
I WANT an AI-powered multi-language translation application
SO THAT I can translate text from English to different languages and back efficiently using an AI-powered model.
GIVEN an AI-powered translation application
WHEN a user selects a target language and inputs text
THEN the application provides the translation from English to the target language in real-time
WHEN the reverse translation option is selected
THEN the application translates the input text back to English
WHEN the user exceeds the allowed number of translation requests
THEN the application informs the user and indicates how long to wait before making a new request
- Technologies Used
- Installation
- Configuration
- Usage
- Logging
- File Structure
- API Endpoint
- Rate Limiting
- Error Handling
- Future Enhancements
- Contributing
- License
- Acknowledgments
- Frontend:
- HTML5, CSS3 (Responsive design)
- Vanilla JavaScript (DOM Manipulation, Fetch API)
- Backend:
- Node.js with Express for routing and server-side logic
- OpenAI API via LangChain for GPT-4 powered translations
- Helmet for securing HTTP headers
- Express Rate Limit for rate limiting middleware
- Other Tools:
- dotenv for environment variables
- Morgan and Winston for logging
- nodemon for development (optional)
To set up the AI Multi-Language Translator locally, follow these steps:
- Node.js: Ensure you have Node.js installed (version >=14.x).
- npm: Node package manager comes with Node.js; ensure it's up to date.
- OpenAI API Key: You need a valid OpenAI API key to use the translation features.
-
Clone the Repository:
git clone https://github.com/SKhai77/ai-multi-language-translator.git
-
Navigate to the Project Directory:
cd ai-multi-language-translator
-
Install Dependencies:
Install the necessary Node.js dependencies:
npm install
Create a .env
file in the project root directory and add your environment variables:
OPENAI_API_KEY=your_openai_api_key
Replace your_openai_api_key
with your actual OpenAI API key.
OPENAI_MODEL=gpt-4o
OPENAI_TEMPERATURE=0.25
In config/config.js
, you can adjust the rate limiting settings:
rateLimit: {
windowMs: 5 * 60 * 1000,
max: 5,
}
-
Start the Server:
To start the Express server and serve the application:
npm run start
or for development:
npm run dev
-
Access the Application:
Once the server is running, open your web browser and navigate to:
http://localhost:3000
-
Perform Translations:
- Enter the text you want to translate in English.
- Select a target language from the dropdown menu.
- Optionally, check the Reverse Translation box to translate the result back into English.
- Click the Translate button.
- View the translation result and your remaining requests.
-
The application uses Winston for logging, and logs are written to an
app.log
file. -
Whenever the application is running, the
app.log
file will be created in the root directory (if it doesn't already exist) and will store logs such as request details, errors, and other important events.You can find the log file at:
/project-root/app.log
-
The logs include information about incoming requests, successful translations, errors, and more. This can be useful for debugging or monitoring purposes.
/project-root
├── /.vscode # VSCode-specific settings
│ └── settings.json # Spell-check configuration
├── /config # Logger and other configurations
│ ├── config.js # Configuration file
│ └── logger.js # Winston logger configuration
├── /public # Contains all public-facing files
│ ├── index.html # Main HTML file
│ ├── styles.css # CSS file for styling
│ └── script.js # JavaScript for front-end logic
├── /src # Contains server-side code
│ └── server.js # Main server file
├── .env # Environment variables
├── .gitignore # Git ignore file
├── app.log # Log file created by Winston
├── license # MIT License information
├── package-lock.json # Lockfile for installed dependencies
├── package.json # npm package file
└── README.md # Project documentation
Handles translation requests.
{
"question": "Your text to translate",
"targetLanguage": "Target language",
"reverse": true or false
}
{
"result": "Translated text"
}
{
"error": "Error message"
}
-
Each IP address is limited to 5 requests per 5 minutes.
-
The application displays the remaining number of requests after each translation.
-
If the rate limit is exceeded, users receive a message indicating how long they need to wait before making a new request.
-
Headers related to rate limiting:
RateLimit-Limit
: Maximum number of requests allowed.RateLimit-Remaining
: Number of requests left in the current window.RateLimit-Reset
: Time when the current window resets in UTC epoch seconds.Retry-After
: Number of seconds to wait before making a new request.
The frontend will display the remaining requests and, when the limit is exceeded, the time when users can retry.
- Checks if the input text is provided before sending a request.
- Displays informative error messages for missing inputs.
- Validates request parameters.
- Returns appropriate HTTP status codes and error messages.
- Handles network errors gracefully.
- Provides user-friendly messages for unexpected errors.
- Additional Language Support: Expand the available languages for translation.
- User Authentication: Add login/logout functionality for personalized translation history.
- Speech-to-Text and Text-to-Speech: Implement speech translation capabilities.
- Translation History: Allow users to save and view past translations.
- Caching Improvements: Enhance the caching strategy to store more translations efficiently.
Contributions are welcome! If you'd like to contribute to this project, please fork the repository and use a feature branch. Pull requests are warmly welcomed.
- Fork the repository
- Create your feature branch (
git checkout -b feature/YourFeature
) - Commit your changes (
git commit -m 'Add SomeFeature'
) - Push to the branch (
git push origin feature/YourFeature
) - Open a Pull Request
This project is licensed under the MIT License.
- OpenAI for the GPT-4 model.
- LangChain for the language model framework.
- Express.js for the web framework.
- Express Rate Limit for rate limiting middleware.
- Winston and Morgan for logging.