Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,87 @@
|
|
| 1 |
-
import
|
| 2 |
import zipfile
|
|
|
|
| 3 |
import shutil
|
| 4 |
-
import torch
|
| 5 |
-
import torchaudio
|
| 6 |
from pyannote.audio import Pipeline
|
| 7 |
-
import
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
pipeline = Pipeline.from_pretrained(
|
| 12 |
"pyannote/speaker-diarization-3.1",
|
| 13 |
-
use_auth_token=
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
with zipfile.ZipFile(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Function to classify and group files by speaker
|
| 29 |
-
def classify_and_group_speakers(zip_file):
|
| 30 |
-
# Step 1: Create temporary directories
|
| 31 |
-
extract_dir = 'extract_temp'
|
| 32 |
-
speaker1_dir = 'speaker1_temp'
|
| 33 |
-
speaker2_dir = 'speaker2_temp'
|
| 34 |
-
os.makedirs(extract_dir, exist_ok=True)
|
| 35 |
os.makedirs(speaker1_dir, exist_ok=True)
|
| 36 |
os.makedirs(speaker2_dir, exist_ok=True)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
speaker1_zip
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
shutil.rmtree(
|
| 78 |
-
|
| 79 |
-
|
| 80 |
return speaker1_zip, speaker2_zip
|
| 81 |
|
| 82 |
-
# Gradio
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import zipfile
|
| 3 |
+
import os
|
| 4 |
import shutil
|
|
|
|
|
|
|
| 5 |
from pyannote.audio import Pipeline
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
# Set up the directory for processing
|
| 9 |
+
TEMP_DIR = "temp_audio"
|
| 10 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# Initialize the pyannote.audio pipeline
|
| 13 |
pipeline = Pipeline.from_pretrained(
|
| 14 |
"pyannote/speaker-diarization-3.1",
|
| 15 |
+
use_auth_token=os.getenv("HF_TOKEN")
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Move pipeline to GPU if available
|
| 19 |
+
if torch.cuda.is_available():
|
| 20 |
+
pipeline.to(torch.device("cuda"))
|
| 21 |
+
|
| 22 |
+
def process_audio_zip(file_info):
|
| 23 |
+
# Unzip the uploaded file
|
| 24 |
+
with zipfile.ZipFile(file_info, 'r') as zip_ref:
|
| 25 |
+
zip_ref.extractall(TEMP_DIR)
|
| 26 |
+
|
| 27 |
+
speaker1_dir = os.path.join(TEMP_DIR, "speaker1")
|
| 28 |
+
speaker2_dir = os.path.join(TEMP_DIR, "speaker2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
os.makedirs(speaker1_dir, exist_ok=True)
|
| 30 |
os.makedirs(speaker2_dir, exist_ok=True)
|
| 31 |
|
| 32 |
+
# Process each audio file in the temporary directory
|
| 33 |
+
for filename in os.listdir(TEMP_DIR):
|
| 34 |
+
if filename.endswith(".wav"):
|
| 35 |
+
file_path = os.path.join(TEMP_DIR, filename)
|
| 36 |
+
|
| 37 |
+
# Run the diarization pipeline
|
| 38 |
+
diarization = pipeline(file_path)
|
| 39 |
+
|
| 40 |
+
# Determine if the audio is mostly from speaker1 or speaker2
|
| 41 |
+
total_duration = {1: 0.0, 2: 0.0}
|
| 42 |
+
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 43 |
+
total_duration[speaker] += turn.duration
|
| 44 |
+
|
| 45 |
+
# Move file to the corresponding speaker directory
|
| 46 |
+
dominant_speaker = 1 if total_duration[1] >= total_duration[2] else 2
|
| 47 |
+
if dominant_speaker == 1:
|
| 48 |
+
shutil.move(file_path, os.path.join(speaker1_dir, filename))
|
| 49 |
+
else:
|
| 50 |
+
shutil.move(file_path, os.path.join(speaker2_dir, filename))
|
| 51 |
+
|
| 52 |
+
# Zip the results
|
| 53 |
+
speaker1_zip = "speaker1.zip"
|
| 54 |
+
speaker2_zip = "speaker2.zip"
|
| 55 |
+
|
| 56 |
+
def zipdir(path, ziph):
|
| 57 |
+
# Zip the directories
|
| 58 |
+
for root, dirs, files in os.walk(path):
|
| 59 |
+
for file in files:
|
| 60 |
+
ziph.write(os.path.join(root, file),
|
| 61 |
+
os.path.relpath(os.path.join(root, file),
|
| 62 |
+
os.path.join(path, '..')))
|
| 63 |
+
|
| 64 |
+
with zipfile.ZipFile(speaker1_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 65 |
+
zipdir(speaker1_dir, zipf)
|
| 66 |
+
|
| 67 |
+
with zipfile.ZipFile(speaker2_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 68 |
+
zipdir(speaker2_dir, zipf)
|
| 69 |
+
|
| 70 |
+
# Clean up the temporary directory
|
| 71 |
+
shutil.rmtree(TEMP_DIR)
|
| 72 |
+
|
|
|
|
| 73 |
return speaker1_zip, speaker2_zip
|
| 74 |
|
| 75 |
+
# Gradio interface
|
| 76 |
+
iface = gr.Interface(
|
| 77 |
+
fn=process_audio_zip,
|
| 78 |
+
inputs=gr.inputs.File(type="file"),
|
| 79 |
+
outputs=[
|
| 80 |
+
gr.File(label="Speaker 1 Audio"),
|
| 81 |
+
gr.File(label="Speaker 2 Audio")
|
| 82 |
+
],
|
| 83 |
+
title="Speaker Diarization",
|
| 84 |
+
description="Upload a ZIP file containing audio files, and this will return two ZIP files containing diarized audio for each speaker."
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
iface.launch()
|