CoolFace
Apppublic

DrKO999/darija-transcription

sourceHugging Faceapache-2.0updated 3mo agoView on Hugging Face
1likes
App README

Darija Transcription & Diarization API

A FastAPI-based service for transcribing and diarizing Moroccan Darija audio using OpenAI Whisper with LoRA fine-tuning and pyannote speaker diarization.

Features

โœจ Speech-to-Text: Transcribes Darija audio using Whisper + LoRA fine-tuned model ๐Ÿ‘ฅ Speaker Diarization: Identifies and labels different speakers ๐Ÿ“ค Multiple Export Formats: JSON, VTT, SRT, TXT ๐Ÿš€ GPU-Accelerated: Optimized for CUDA devices ๐Ÿ”„ Retry Logic: Automatic retries for robust downloads

Deploy to Hugging Face Spaces ๐Ÿค—

Step 1: Prepare Your GitHub Repository

  1. 1.Create a new GitHub repository (or use an existing one)
  2. 2.Clone it locally:
bash
   git clone https://github.com/YOUR_USERNAME/darija-transcription.git
   cd darija-transcription
  1. 1.Add these files to your repo:
  2. 2.app.py โœ“
  3. 3.requirements.txt โœ“
  4. 4.Dockerfile โœ“
  5. 5..gitignore โœ“
  6. 6.README.md (this file)
  1. 1.Commit and push:
bash
   git add .
   git commit -m "Initial commit: Darija transcription API"
   git push origin main

Step 2: Create a Hugging Face Space

  1. 1.Go to huggingface.co/spaces
  2. 2.Click "Create new Space"
  3. 3.Fill in:
  4. 4.Space name: darija-transcription (or your preferred name)
  5. 5.License: Apache 2.0 (or your choice)
  6. 6.Space SDK: Docker
  7. 7.Space hardware: GPU T4 (free tier: ~20 hrs/week)
  8. 8.Private or Public: Choose your preference
  1. 1.Click "Create Space"

Step 3: Connect Your Repository

  1. 1.In your newly created Space, click "Settings" (โš™๏ธ icon)
  2. 2.Under "Linked Repository", paste your GitHub repo URL:
   https://github.com/YOUR_USERNAME/darija-transcription
  1. 1.Click "Link repository"

The Space will automatically build and deploy from your GitHub repo!

Step 4: Add Environment Variables (Important!)

  1. 1.Go to Settings โ†’ "Repository secrets"
  2. 2.Add HF_TOKEN:
  3. 3.Generate a token at huggingface.co/settings/tokens
  4. 4.Make sure it has "repo" permissions
  5. 5.Copy and paste it as HF_TOKEN secret

This allows the pyannote diarization model to download (it requires authentication).

Local Development

Prerequisites

  • โ€”Python 3.10+
  • โ€”CUDA 12.1 (or CPU-only mode)
  • โ€”~20 GB disk space for models

Installation

  1. 1.Clone the repo:
bash
   git clone https://github.com/YOUR_USERNAME/darija-transcription.git
   cd darija-transcription
  1. 1.Create a virtual environment:
bash
   python3 -m venv venv
   source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. 1.Install dependencies:
bash
   pip install -r requirements.txt
  1. 1.Create .env file:
bash
   echo "HF_TOKEN=your_huggingface_token_here" > .env

Get your token from huggingface.co/settings/tokens

  1. 1.Run the app:
bash
   python app.py

The API will be available at http://localhost:8000

API Usage

Health Check

bash
curl http://localhost:8000/health

Transcribe Audio

bash
curl -X POST "http://localhost:8000/transcribe" \
  -H "Content-Type: application/json" \
  -d '{
    "audio_url": "https://example.com/audio.wav",
    "num_speakers": 2,
    "merge_same_speaker": true
  }'

Request Parameters:

ParameterTypeDefaultDescription
audio_urlstringrequiredURL to audio file (HTTP/HTTPS)
languagestring"ar"Language code (ar for Arabic/Darija)
num_speakersint2Expected number of speakers (1-10)
merge_same_speakerbooltrueMerge consecutive segments from same speaker

Response:

json
{
  "utterances": [
    {
      "start": 0.5,
      "end": 2.3,
      "text": "ุงู„ุณู„ุงู… ุนู„ูŠูƒู… ูˆุฑุญู…ุฉ ุงู„ู„ู‡ ูˆุจุฑูƒุงุชู‡",
      "speaker": "SPEAKER_00",
      "confidence": 0.95
    },
    {
      "start": 2.8,
      "end": 5.1,
      "text": "ุนู„ูŠูƒู… ุงู„ุณู„ุงู… ูˆุฑุญู…ุฉ ุงู„ู„ู‡ ูˆุจุฑูƒุงุชู‡",
      "speaker": "SPEAKER_01",
      "confidence": 0.92
    }
  ],
  "total_duration": 5.1,
  "num_speakers": 2,
  "language": "ar",
  "processing_time_seconds": 12.4
}

Export to Different Formats

bash
# Export to VTT (WebVTT subtitles)
curl -X POST "http://localhost:8000/transcribe_with_export?format=vtt" \
  -H "Content-Type: application/json" \
  -d '{
    "audio_url": "https://example.com/audio.wav"
  }' | jq -r '.content'

# Export to SRT (SubRip subtitles)
curl -X POST "http://localhost:8000/transcribe_with_export?format=srt" \
  -H "Content-Type: application/json" \
  -d '{
    "audio_url": "https://example.com/audio.wav"
  }' | jq -r '.content'

# Export to TXT (plain text)
curl -X POST "http://localhost:8000/transcribe_with_export?format=txt" \
  -H "Content-Type: application/json" \
  -d '{
    "audio_url": "https://example.com/audio.wav"
  }' | jq -r '.content'

Interactive Documentation

Once the API is running, visit:

  • โ€”Swagger UI: http://localhost:8000/docs
  • โ€”ReDoc: http://localhost:8000/redoc

You can test endpoints directly from the browser!

Performance Notes

First Deployment

  • โ€”Model loading takes ~3-5 minutes on first startup
  • โ€”Subsequent requests are much faster

Free Tier Limitations (HF Spaces)

  • โ€”GPU available: ~20 hours/week (T4)
  • โ€”Space sleeps after 48 hours of inactivity
  • โ€”Cold starts take longer

Optimize Performance

To reduce model size (trades accuracy for speed):

python
# In app.py, change to:
BASE_MODEL = "openai/whisper-base"  # 150MB instead of 3GB

Troubleshooting

"HF_TOKEN not found"

  • โ€”Make sure you added HF_TOKEN to Space secrets
  • โ€”Token needs "repo" access level at minimum

"CUDA out of memory"

  • โ€”Reduce chunk size in app.py:
python
  chunk_length_s=15,  # Reduce from 30

Models not downloading

  • โ€”Check your HF token is valid: huggingface-cli login
  • โ€”Ensure you have enough disk space

Slow first request

  • โ€”Normal! Models are loading from cache on first startup
  • โ€”Takes 3-5 minutes; subsequent requests are faster

Project Structure

darija-transcription/
โ”œโ”€โ”€ app.py                 # FastAPI application
โ”œโ”€โ”€ requirements.txt       # Python dependencies
โ”œโ”€โ”€ Dockerfile            # Docker configuration for HF Spaces
โ”œโ”€โ”€ .gitignore           # Git ignore rules
โ””โ”€โ”€ README.md            # This file

Models Used

  • โ€”ASR: OpenAI Whisper Large V3 Turbo + LoRA fine-tune for Darija
  • โ€”Diarization: pyannote/speaker-diarization-3.1
  • โ€”Hardware: CUDA GPU (T4 on HF Spaces)

License

Apache 2.0 โ€” Feel free to use, modify, and distribute!

Support

For issues or questions:

  1. 1.Check the troubleshooting section
  2. 2.Review API logs in Space runtime
  3. 3.Open an issue on GitHub

Built with โค๏ธ for Darija speakers