File size: 4,240 Bytes
c1ccaee
 
 
 
 
 
 
 
 
 
 
 
 
494ed90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
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!