mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 12:19:06 +00:00
* feat: improve pipeline threading, and transcriber (parakeet and silero vad) * refactor: remove whisperx, implement parakeet * refactor: make audio_chunker more smart and wait for speech, instead of fixed frame * refactor: make audio merge to always downscale the audio to 16k for transcription * refactor: make the audio transcript modal accepting batches * refactor: improve type safety and remove prometheus metrics - Add DiarizationSegment TypedDict for proper diarization typing - Replace List/Optional with modern Python list/| None syntax - Remove all Prometheus metrics from TranscriptDiarizationAssemblerProcessor - Add comprehensive file processing pipeline with parallel execution - Update processor imports and type annotations throughout - Implement optimized file pipeline as default in process.py tool * refactor: convert FileDiarizationProcessor I/O types to BaseModel Update FileDiarizationInput and FileDiarizationOutput to inherit from BaseModel instead of plain classes, following the standard pattern used by other processors in the codebase. * test: add tests for file transcript and diarization with pytest-recording * build: add pytest-recording * feat: add local pyannote for testing * fix: replace PyAV AudioResampler with torchaudio for reliable audio processing - Replace problematic PyAV AudioResampler that was causing ValueError: [Errno 22] Invalid argument - Use torchaudio.functional.resample for robust sample rate conversion - Optimize processing: skip conversion for already 16kHz mono audio - Add direct WAV writing with Python wave module for better performance - Consolidate duplicate downsample checks for cleaner code - Maintain list[av.AudioFrame] input interface - Required for Silero VAD which needs 16kHz mono audio * fix: replace PyAV AudioResampler with torchaudio solution - Resolves ValueError: [Errno 22] Invalid argument in AudioMergeProcessor - Replaces problematic PyAV AudioResampler with torchaudio.functional.resample - Optimizes processing to skip unnecessary conversions when audio is already 16kHz mono - Uses direct WAV writing with Python's wave module for better performance - Fixes test_basic_process to disable diarization (pyannote dependency not installed) - Updates test expectations to match actual processor behavior - Removes unused pydub dependency from pyproject.toml - Adds comprehensive TEST_ANALYSIS.md documenting test suite status * feat: add parameterized test for both diarization modes - Adds @pytest.mark.parametrize to test_basic_process with enable_diarization=[False, True] - Test with diarization=False always passes (tests core AudioMergeProcessor functionality) - Test with diarization=True gracefully skips when pyannote.audio is not installed - Provides comprehensive test coverage for both pipeline configurations * fix: resolve pipeline property naming conflict in AudioDiarizationPyannoteProcessor - Renames 'pipeline' property to 'diarization_pipeline' to avoid conflict with base Processor.pipeline attribute - Fixes AttributeError: 'property 'pipeline' object has no setter' when set_pipeline() is called - Updates property usage in _diarize method to use new name - Now correctly supports pipeline initialization for diarization processing * fix: add local for pyannote * test: add diarization test * fix: resample on audio merge now working * fix: correctly restore timestamp * fix: display exception in a threaded processor if that happen * Update pyproject.toml * ci: remove option * ci: update astral-sh/setup-uv * test: add monadical url for pytest-recording * refactor: remove previous version * build: move faster whisper to local dep * test: fix missing import * refactor: improve main_file_pipeline organization and error handling - Move all imports to the top of the file - Create unified EmptyPipeline class to replace duplicate mock pipeline code - Remove timeout and fallback logic - let processors handle their own retries - Fix error handling to raise any exception from parallel tasks - Add proper type hints and validation for captured results * fix: wrong function * fix: remove task_done * feat: add configurable file processing timeouts for modal processors - Add TRANSCRIPT_FILE_TIMEOUT setting (default: 600s) for file transcription - Add DIARIZATION_FILE_TIMEOUT setting (default: 600s) for file diarization - Replace hardcoded timeout=600 with configurable settings in modal processors - Allows customization of timeout values via environment variables * fix: use logger * fix: worker process meetings now use file pipeline * fix: topic not gathered * refactor: remove prepare(), pipeline now work * refactor: implement many review from Igor * test: add test for test_pipeline_main_file * refactor: remove doc * doc: add doc * ci: update build to use native arm64 builder * fix: merge fixes * refactor: changes from Igor review + add test (not by default) to test gpu modal part * ci: update to our own runner linux-amd64 * ci: try using suggested mode=min * fix: update diarizer for latest modal, and use volume * fix: modal file extension detection * fix: put the diarizer as A100
254 lines
7.7 KiB
Python
254 lines
7.7 KiB
Python
"""
|
|
Reflector GPU backend - diarizer
|
|
===================================
|
|
"""
|
|
|
|
import os
|
|
import uuid
|
|
from typing import Mapping, NewType
|
|
from urllib.parse import urlparse
|
|
|
|
import modal
|
|
|
|
PYANNOTE_MODEL_NAME: str = "pyannote/speaker-diarization-3.1"
|
|
MODEL_DIR = "/root/diarization_models"
|
|
UPLOADS_PATH = "/uploads"
|
|
SUPPORTED_FILE_EXTENSIONS = ["mp3", "mp4", "mpeg", "mpga", "m4a", "wav", "webm"]
|
|
|
|
DiarizerUniqFilename = NewType("DiarizerUniqFilename", str)
|
|
AudioFileExtension = NewType("AudioFileExtension", str)
|
|
|
|
app = modal.App(name="reflector-diarizer")
|
|
|
|
# Volume for temporary file uploads
|
|
upload_volume = modal.Volume.from_name("diarizer-uploads", create_if_missing=True)
|
|
|
|
|
|
def detect_audio_format(url: str, headers: Mapping[str, str]) -> AudioFileExtension:
|
|
parsed_url = urlparse(url)
|
|
url_path = parsed_url.path
|
|
|
|
for ext in SUPPORTED_FILE_EXTENSIONS:
|
|
if url_path.lower().endswith(f".{ext}"):
|
|
return AudioFileExtension(ext)
|
|
|
|
content_type = headers.get("content-type", "").lower()
|
|
if "audio/mpeg" in content_type or "audio/mp3" in content_type:
|
|
return AudioFileExtension("mp3")
|
|
if "audio/wav" in content_type:
|
|
return AudioFileExtension("wav")
|
|
if "audio/mp4" in content_type:
|
|
return AudioFileExtension("mp4")
|
|
|
|
raise ValueError(
|
|
f"Unsupported audio format for URL: {url}. "
|
|
f"Supported extensions: {', '.join(SUPPORTED_FILE_EXTENSIONS)}"
|
|
)
|
|
|
|
|
|
def download_audio_to_volume(
|
|
audio_file_url: str,
|
|
) -> tuple[DiarizerUniqFilename, AudioFileExtension]:
|
|
import requests
|
|
from fastapi import HTTPException
|
|
|
|
print(f"Checking audio file at: {audio_file_url}")
|
|
response = requests.head(audio_file_url, allow_redirects=True)
|
|
if response.status_code == 404:
|
|
raise HTTPException(status_code=404, detail="Audio file not found")
|
|
|
|
print(f"Downloading audio file from: {audio_file_url}")
|
|
response = requests.get(audio_file_url, allow_redirects=True)
|
|
|
|
if response.status_code != 200:
|
|
print(f"Download failed with status {response.status_code}: {response.text}")
|
|
raise HTTPException(
|
|
status_code=response.status_code,
|
|
detail=f"Failed to download audio file: {response.status_code}",
|
|
)
|
|
|
|
audio_suffix = detect_audio_format(audio_file_url, response.headers)
|
|
unique_filename = DiarizerUniqFilename(f"{uuid.uuid4()}.{audio_suffix}")
|
|
file_path = f"{UPLOADS_PATH}/{unique_filename}"
|
|
|
|
print(f"Writing file to: {file_path} (size: {len(response.content)} bytes)")
|
|
with open(file_path, "wb") as f:
|
|
f.write(response.content)
|
|
|
|
upload_volume.commit()
|
|
print(f"File saved as: {unique_filename}")
|
|
return unique_filename, audio_suffix
|
|
|
|
|
|
def migrate_cache_llm():
|
|
"""
|
|
XXX The cache for model files in Transformers v4.22.0 has been updated.
|
|
Migrating your old cache. This is a one-time only operation. You can
|
|
interrupt this and resume the migration later on by calling
|
|
`transformers.utils.move_cache()`.
|
|
"""
|
|
from transformers.utils.hub import move_cache
|
|
|
|
print("Moving LLM cache")
|
|
move_cache(cache_dir=MODEL_DIR, new_cache_dir=MODEL_DIR)
|
|
print("LLM cache moved")
|
|
|
|
|
|
def download_pyannote_audio():
|
|
from pyannote.audio import Pipeline
|
|
|
|
Pipeline.from_pretrained(
|
|
PYANNOTE_MODEL_NAME,
|
|
cache_dir=MODEL_DIR,
|
|
use_auth_token=os.environ["HF_TOKEN"],
|
|
)
|
|
|
|
|
|
diarizer_image = (
|
|
modal.Image.debian_slim(python_version="3.10.8")
|
|
.pip_install(
|
|
"pyannote.audio==3.1.0",
|
|
"requests",
|
|
"onnx",
|
|
"torchaudio",
|
|
"onnxruntime-gpu",
|
|
"torch==2.0.0",
|
|
"transformers==4.34.0",
|
|
"sentencepiece",
|
|
"protobuf",
|
|
"numpy",
|
|
"huggingface_hub",
|
|
"hf-transfer",
|
|
)
|
|
.run_function(
|
|
download_pyannote_audio,
|
|
secrets=[modal.Secret.from_name("hf_token")],
|
|
)
|
|
.run_function(migrate_cache_llm)
|
|
.env(
|
|
{
|
|
"LD_LIBRARY_PATH": (
|
|
"/usr/local/lib/python3.10/site-packages/nvidia/cudnn/lib/:"
|
|
"/opt/conda/lib/python3.10/site-packages/nvidia/cublas/lib/"
|
|
)
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
@app.cls(
|
|
gpu="A100",
|
|
timeout=60 * 30,
|
|
image=diarizer_image,
|
|
volumes={UPLOADS_PATH: upload_volume},
|
|
enable_memory_snapshot=True,
|
|
experimental_options={"enable_gpu_snapshot": True},
|
|
secrets=[
|
|
modal.Secret.from_name("hf_token"),
|
|
],
|
|
)
|
|
@modal.concurrent(max_inputs=1)
|
|
class Diarizer:
|
|
@modal.enter(snap=True)
|
|
def enter(self):
|
|
import torch
|
|
from pyannote.audio import Pipeline
|
|
|
|
self.use_gpu = torch.cuda.is_available()
|
|
self.device = "cuda" if self.use_gpu else "cpu"
|
|
print(f"Using device: {self.device}")
|
|
self.diarization_pipeline = Pipeline.from_pretrained(
|
|
PYANNOTE_MODEL_NAME,
|
|
cache_dir=MODEL_DIR,
|
|
use_auth_token=os.environ["HF_TOKEN"],
|
|
)
|
|
self.diarization_pipeline.to(torch.device(self.device))
|
|
|
|
@modal.method()
|
|
def diarize(self, filename: str, timestamp: float = 0.0):
|
|
import torchaudio
|
|
|
|
upload_volume.reload()
|
|
|
|
file_path = f"{UPLOADS_PATH}/{filename}"
|
|
if not os.path.exists(file_path):
|
|
raise FileNotFoundError(f"File not found: {file_path}")
|
|
|
|
print(f"Diarizing audio from: {file_path}")
|
|
waveform, sample_rate = torchaudio.load(file_path)
|
|
diarization = self.diarization_pipeline(
|
|
{"waveform": waveform, "sample_rate": sample_rate}
|
|
)
|
|
|
|
words = []
|
|
for diarization_segment, _, speaker in diarization.itertracks(yield_label=True):
|
|
words.append(
|
|
{
|
|
"start": round(timestamp + diarization_segment.start, 3),
|
|
"end": round(timestamp + diarization_segment.end, 3),
|
|
"speaker": int(speaker[-2:]),
|
|
}
|
|
)
|
|
print("Diarization complete")
|
|
return {"diarization": words}
|
|
|
|
|
|
# -------------------------------------------------------------------
|
|
# Web API
|
|
# -------------------------------------------------------------------
|
|
|
|
|
|
@app.function(
|
|
timeout=60 * 10,
|
|
scaledown_window=60 * 3,
|
|
secrets=[
|
|
modal.Secret.from_name("reflector-gpu"),
|
|
],
|
|
volumes={UPLOADS_PATH: upload_volume},
|
|
image=diarizer_image,
|
|
)
|
|
@modal.concurrent(max_inputs=40)
|
|
@modal.asgi_app()
|
|
def web():
|
|
from fastapi import Depends, FastAPI, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from pydantic import BaseModel
|
|
|
|
diarizerstub = Diarizer()
|
|
|
|
app = FastAPI()
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
|
def apikey_auth(apikey: str = Depends(oauth2_scheme)):
|
|
if apikey != os.environ["REFLECTOR_GPU_APIKEY"]:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid API key",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
class DiarizationResponse(BaseModel):
|
|
result: dict
|
|
|
|
@app.post("/diarize", dependencies=[Depends(apikey_auth)])
|
|
def diarize(audio_file_url: str, timestamp: float = 0.0) -> DiarizationResponse:
|
|
unique_filename, audio_suffix = download_audio_to_volume(audio_file_url)
|
|
|
|
try:
|
|
func = diarizerstub.diarize.spawn(
|
|
filename=unique_filename, timestamp=timestamp
|
|
)
|
|
result = func.get()
|
|
return result
|
|
finally:
|
|
try:
|
|
file_path = f"{UPLOADS_PATH}/{unique_filename}"
|
|
print(f"Deleting file: {file_path}")
|
|
os.remove(file_path)
|
|
upload_volume.commit()
|
|
except Exception as e:
|
|
print(f"Error cleaning up {unique_filename}: {e}")
|
|
|
|
return app
|