--- title: Language Detector emoji: 📉 colorFrom: green colorTo: gray sdk: gradio sdk_version: 5.16.2 app_file: app.py pinned: false license: apache-2.0 short_description: language_detector --- # Language Detection with Gradio This repository contains a simple language detection application built with [Gradio](https://gradio.app/) and [Transformers](https://huggingface.co/transformers/). The application leverages a pre-trained language detection model to identify the language of a given text input. The user interface is created using Gradio, making it easy to run and share as a web app. ## Features - **Language Detection:** Enter text in any language and the model will output the detected language. - **Interactive UI:** A Gradio interface provides an easy-to-use web interface for testing and demos. - **Examples:** Predefined examples in multiple languages (English, French, Spanish, Arabic) are provided for quick testing. ## Code Overview The main script performs the following tasks: 1. **Importing Libraries:** - Imports `gradio` for building the web interface. - Imports `pipeline` from `transformers` to load the pre-trained language detection model. 2. **Defining the Language Detection Function:** - `detect_language(text)`: This function takes a text string as input, processes it through the language detection model, and returns the detected language label. - **Note:** Ensure that the variable `language_detector` is properly initialized with a language detection pipeline (e.g., using `pipeline("text-classification", model="your-model-name")`). This snippet assumes that `language_detector` is already defined elsewhere or should be added before using the function. 3. **Setting Up Examples:** - A list of example inputs in English, French, Spanish, and Arabic to demonstrate the functionality. 4. **Creating the Gradio Interface:** - An instance of `gr.Interface` is created with the function `detect_language`, input and output components, title, description, and examples. - The interface is then launched with `iface.launch()`. ## Installation 1. **Clone the Repository:** ```bash git clone https://github.com/yourusername/language-detection-app.git cd language-detection-app ``` 2. **Set Up a Virtual Environment (Optional but Recommended):** ```bash python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate ``` 3. **Install the Required Dependencies:** ```bash pip install gradio transformers ``` 4. **Initialize the Language Detector:** Before running the code, ensure that the `language_detector` pipeline is initialized. For example, you might add the following code at the top of your script: ```python from transformers import pipeline language_detector = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection") ``` Replace `"papluca/xlm-roberta-base-language-detection"` with the model of your choice if needed. ## Usage 1. **Run the Application:** ```bash python your_script_name.py ``` 2. **Access the Gradio Interface:** Once the script is running, a local URL (e.g., http://127.0.0.1:7860) will be displayed in your terminal. Open this URL in your web browser to interact with the language detection application. 3. **Test the Application:** - Type or paste text into the input textbox. - Click the "Submit" button to see the detected language. - You can also use the provided examples to test the functionality. ## Customization - **Model Choice:** You can swap out the language detection model by changing the model parameter in the `pipeline` initialization. - **Interface Customization:** Modify the Gradio interface parameters (e.g., title, description, input/output types) to better suit your needs. - **Deployment:** The Gradio app can be easily shared or deployed using services like [Hugging Face Spaces](https://huggingface.co/spaces). ## Contributing Contributions are welcome! Feel free to open issues or submit pull requests to enhance the functionality of this project. ## License This project is licensed under the [MIT License](LICENSE). --- Enjoy building and sharing your language detection app!