Spaces:
Sleeping
A newer version of the Gradio SDK is available:
5.22.0
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 and 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:
Importing Libraries:
- Imports
gradio
for building the web interface. - Imports
pipeline
fromtransformers
to load the pre-trained language detection model.
- Imports
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., usingpipeline("text-classification", model="your-model-name")
). This snippet assumes thatlanguage_detector
is already defined elsewhere or should be added before using the function.
Setting Up Examples:
- A list of example inputs in English, French, Spanish, and Arabic to demonstrate the functionality.
Creating the Gradio Interface:
- An instance of
gr.Interface
is created with the functiondetect_language
, input and output components, title, description, and examples. - The interface is then launched with
iface.launch()
.
- An instance of
Installation
Clone the Repository:
git clone https://github.com/yourusername/language-detection-app.git cd language-detection-app
Set Up a Virtual Environment (Optional but Recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
Install the Required Dependencies:
pip install gradio transformers
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: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
Run the Application:
python your_script_name.py
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.
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.
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.
Enjoy building and sharing your language detection app!