This is a Next.js application that leverages HuggingFace models for real-time language translation. It provides a simple and intuitive user interface to translate text between several languages.
- Real-time text translation using HuggingFace models.
- Supports English to Hindi, Spanish, German, and French translations.
- User-friendly interface with language selection and input/output fields.
- Loading indicator for translation requests.
This project is built with the following key technologies:
- Next.js : A React framework for building production-ready applications. It provides server-side rendering (SSR) and API routes, which are crucial for this application.
- React : A JavaScript library for building user interfaces. Used for the frontend components of the translator.
- Tailwind CSS : A utility-first CSS framework for rapidly styling the application with pre-defined classes.
- Axios : A promise-based HTTP client for the browser and Node.js. Used for making API requests from the frontend to the backend.
- @huggingface/inference : A JavaScript library to interact with HuggingFace's Inference API. This is used on the backend to perform the actual language translation.
The application follows a client-server architecture:
- Frontend (Next.js/React): The user interacts with the
Example.jsx
component, which allows them to input text and select a target language. - API Request (Axios): When the user clicks the "Translate" button, the frontend sends a POST request to the
/api/huggingface
endpoint using Axios. - Backend (Next.js API Route): The request is handled by the
pages/api/huggingface/index.js
API route. - HuggingFace Inference: The API route uses the
@huggingface/inference
library to send the input text and selected language model to the HuggingFace Inference API. - Translation Response: HuggingFace processes the request and returns the translated text.
- Response to Frontend: The API route sends the translated text back to the frontend.
- Display Translation: The frontend updates the UI to display the translated text.
To get this project up and running on your local machine, follow these steps:
- Node.js (version 14 or higher)
- npm or yarn
- A HuggingFace API token. You can get one by signing up on the HuggingFace website.
-
Clone the repository:
git clone https://github.com/mohitkothari-dev/Hf_Language_Translator.git cd Hf_Language_Translator
-
Install the dependencies:
npm install # or yarn install
-
Create a
.env
file in the root directory of the project and add your HuggingFace API token:HF_ACCESS_TOKEN=YOUR_HF_ACCESS_TOKEN
Replace
YOUR_HF_ACCESS_TOKEN
with your actual token.
To start the development server:
npm run dev
# or
yarn dev
Open http://localhost:3000 in your browser to see the application.
- Select your desired translation pair from the dropdown menu (e.g., English to Hindi).
- Enter the text you wish to translate into the "Input Text" field.
- Click the "Translate" button.
- The translated text will appear below the button.
- Leverages Powerful AI Models: By using HuggingFace's pre-trained models, the application benefits from state-of-the-art translation capabilities without needing to train custom models.
- Scalable Backend: Next.js API routes provide a serverless function environment, making the backend easily scalable.
- Fast Development: Next.js and Tailwind CSS enable rapid development and a streamlined styling process.
- Modular and Maintainable: The codebase is organized into clear components and API routes, making it easy to understand and maintain.
- Extensible: Easily add more language pairs by updating the
languageModels
object in the API route and adding options to the frontend dropdown.