Skip to content

mohitkothari-dev/Hf_Language_Translator

Repository files navigation

HuggingFace Language Translator

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.

Table of Contents

Features

  • 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.

Technologies Used

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.

Workflow

The application follows a client-server architecture:

  1. Frontend (Next.js/React): The user interacts with the Example.jsx component, which allows them to input text and select a target language.
  2. API Request (Axios): When the user clicks the "Translate" button, the frontend sends a POST request to the /api/huggingface endpoint using Axios.
  3. Backend (Next.js API Route): The request is handled by the pages/api/huggingface/index.js API route.
  4. HuggingFace Inference: The API route uses the @huggingface/inference library to send the input text and selected language model to the HuggingFace Inference API.
  5. Translation Response: HuggingFace processes the request and returns the translated text.
  6. Response to Frontend: The API route sends the translated text back to the frontend.
  7. Display Translation: The frontend updates the UI to display the translated text.

Getting Started

To get this project up and running on your local machine, follow these steps:

Prerequisites

  • Node.js (version 14 or higher)
  • npm or yarn
  • A HuggingFace API token. You can get one by signing up on the HuggingFace website.

Installation

  1. Clone the repository:

    git clone https://github.com/mohitkothari-dev/Hf_Language_Translator.git
    cd Hf_Language_Translator
  2. Install the dependencies:

    npm install
    # or
    yarn install
  3. 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.

Running the Application

To start the development server:

npm run dev
# or
yarn dev

Open http://localhost:3000 in your browser to see the application.

Usage

  1. Select your desired translation pair from the dropdown menu (e.g., English to Hindi).
  2. Enter the text you wish to translate into the "Input Text" field.
  3. Click the "Translate" button.
  4. The translated text will appear below the button.

Advantages

  • 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.

About

A language translation app built on top of OS HF model.

Topics

Resources

Stars

Watchers

Forks