Better quickstart script including gpu management #1751
Eyalm321
started this conversation in
Development
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This code is for developers with multiple GPU setup, it runs 1 audio file per GPU, if GPU has OOM errors it tries to fall back to cpu memory.
`import os
import logging
from pyannote.audio import Pipeline
from pydub import AudioSegment
import torch
from multiprocessing import Semaphore, Queue, Process, Lock
Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
Hugging Face authentication token
auth_token = ""
Directory containing wav files
wav_dir = "/home/admin/voice"
output_dir = "/home/admin/voice/segments"
Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
List of available GPU devices
gpu_devices = [f"cuda:{i}" for i in range(torch.cuda.device_count())]
Check if GPUs are available
if not gpu_devices:
logging.info("No GPU available. Using CPU.")
gpu_devices = ["cpu"]
Initialize a lock for managing GPU availability
gpu_locks = {gpu: Lock() for gpu in gpu_devices}
def process_file(wav_path, gpu_device, semaphore):
with semaphore:
try:
if gpu_device != "cpu":
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_device.split(":")[-1]
device = torch.device(f"cuda:0")
else:
os.environ["CUDA_VISIBLE_DEVICES"] = ""
device = torch.device("cpu")
def worker(queue, semaphore):
while not queue.empty():
try:
wav_path, gpu_device = queue.get_nowait()
except Queue.Empty:
break
with gpu_locks[gpu_device]:
process_file(wav_path, gpu_device, semaphore)
if name == "main":
# List all wav files
wav_files = [os.path.join(wav_dir, f) for f in os.listdir(wav_dir) if f.endswith(".wav")]
`
Runs well for long audio files.
Cheers.
Beta Was this translation helpful? Give feedback.
All reactions