This project implements an accent detection system that takes a public URL (of a video or audio file) as input, extracts the audio, and then uses a pre-trained deep learning model to identify the speaker's accent. The system provides the detected accent and a confidence score as a JSON output. It also features a simple web interface built with FastAPI and Jinja2 for users to interact with the system. You can view the app here.
demo.mp4
Please note this project is meant to be a POC of the capabilities of accent detection and as such it is not recommended to deploy this in a production setting.
We have adopted a multi-stage approach to achieve accent detection from a public URL:
- URL Handling: The user provides a public URL through a web form.
- Media Download: The system downloads the media (audio or video) from the provided URL using yt-dlp, which is capable of handling various website formats, including YouTube.
- Decision: We chose
yt-dlpover the standardhttp.clientbecauseyt-dlpis specifically designed for downloading media from online platforms and robustly handles various formats and streaming protocols. Whilehttp.clientcould fetch generic files, it wouldn't reliably extract audio streams from video platforms.
- Decision: We chose
- Audio Extraction/Conversion: If the downloaded media is a video, we use
ffmpegto extract the audio track and convert it to a standard format (16kHz, mono, PCM s16le) suitable for the accent detection model. If the input URL directly points to an audio file, this step might be skipped or just involve format standardization if needed.- Decision:
ffmpegis a powerful and widely used command-line tool for audio and video processing, making it a reliable choice for format conversion. The specific audio parameters (16kHz, mono) are chosen to align with the requirements of the pre-trained accent detection model we are using.
- Decision:
- Accent Prediction: The processed audio is then fed into a pre-trained SpeechBrain ECAPA-TDNN model (
Jzuluaga/accent-id-commonaccent_ecapa) for accent classification. This model is trained to recognize 16 different English accents.- Decision: We selected the accent-id-commonaccent_ecapa model from SpeechBrain due to its proven performance in accent identification for a common set of English accents and the ease of integration provided by the SpeechBrain library.
- Result Output: The system returns a JSON response containing the detected accent label and the confidence score (as a percentage).
- Size Limitation: To manage processing time, the system checks the size of the processed audio file. If it exceeds a predefined limit (10MB) or the video is longer than 5 minutes, an error is returned to the user.
- Decision: Implementing a size limit is a straightforward way to prevent the system from being overwhelmed by very large audio files, which would lead to long processing times and potential resource exhaustion. 10MB was chosen as a reasonable initial limit, but this can be adjusted.
graph LR
A[User Input: Public URL] --> B(Download Media);
B --> C{Is Video?};
C -- Yes --> D(Extract);
D --> E(Load SpeechBrain Model);
E --> F(Predict Accent);
F --> I[Output: JSON Accent,nConfidence];
To run this application locally:
-
Ensure you have Python 3.12+ installed.
-
Create a virtualenv and activate it. If you have uv you can run this command
uv venv --python=3.12
-
Install the required dependencies:
pip install .[dev,test]
or if you are using uv
uv sync
-
Run the FastAPI application:
uv run fastapi dev
-
Open your web browser and go to
http://127.0.0.1:8000/.
You can also directly use the API endpoint:
POST /api/v1/accent/detect_accent
- Request Body (as form data):
url: The public URL of the video or audio file.
- Response (JSON):
or an error message in case of failure.
{ "accent": "Detected Accent Label", "confidence": "Confidence Percentage%" }
Pros:
- Handles Various Sources: Using
yt-dlpallows the system to process audio from a wide range of online platforms. - Leverages Pre-trained Model: Utilizing a pre-trained SpeechBrain model simplifies development and provides a good baseline performance for the supported accents.
- Clear Output: The JSON output format is easy to parse for other applications.
- Web Interface: FastAPI and Jinja2 provide a simple way for users to interact with the system through a web browser.
- Mitigation of Long Processing: The file size limit helps prevent excessive processing times for very large files.
- Asynchronous Operations: Using FastAPI's
asyncandawaitmakes the backend more efficient by handling I/O-bound tasks (like downloading and file operations) concurrently.
Cons:
- Limited Accent Coverage: The pre-trained model only supports 16 specific English accents. It will not accurately identify accents outside of this set.
- Dependency on External Tools: The system relies on
yt-dlpandffmpeg, which need to be installed and available on the system. - Potential for Long Processing (within the size limit): While the size limit helps, processing very large (but under the limit) audio files can still take time.
- Accuracy Limitations: The accuracy of the accent detection is limited by the performance of the pre-trained model on the given audio as well as background noise.
Going forward if we were to improve this accent detection system we would do the following:
- Add techniques to improve the audio quality: We can make use of open source libraries or llm models like openai whisper to detect and differentiate speakers in audio which would drastically improve results.
- Explore More Accent Models: Investigate other open-source models that might offer broader accent coverage or better performance for specific accents. This could involve experimenting with models from other libraries or fine-tuning existing models on more diverse datasets.
- Implement Chunking for Large Files: For files within the size limit but still large, we could implement the chunking strategy discussed earlier. This would involve processing the audio in segments and providing more immediate feedback or a more robust overall prediction by analyzing multiple chunks.
- Real-time Processing: For live audio streams (if the input source allows), we could explore real-time accent detection using WebSockets.
- User Feedback and Correction: Allow users to provide feedback on the detected accent, which could be used to improve the system over time (e.g., by creating a small fine-tuning dataset).
- More Sophisticated Error Handling: Provide more informative error messages to the user when issues occur (e.g., during download or conversion).
- Configuration: Externalize configuration parameters (like the maximum file size, S3 , etc.) to make the system more adaptable.
- Create custom functionality: This deployment uses many third party libraries to achieve the end result but it would be better to write our own custom functionality to improve performance and tailor to our use case.
- Housekeeping and Maintenance: We can implement all the best practices of software development i.e testing, CI/CD, etc