Delete app.py
Browse files
app.py
DELETED
|
@@ -1,146 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Pyannote Speaker Diarization API
|
| 3 |
-
Deployed as HuggingFace Space with Gradio API
|
| 4 |
-
|
| 5 |
-
This provides a simple API endpoint for speaker diarization.
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import os
|
| 10 |
-
from pyannote.audio import Pipeline
|
| 11 |
-
import torch
|
| 12 |
-
|
| 13 |
-
# Get HuggingFace token from environment
|
| 14 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 15 |
-
|
| 16 |
-
if not HF_TOKEN:
|
| 17 |
-
raise ValueError("HF_TOKEN environment variable must be set")
|
| 18 |
-
|
| 19 |
-
# Load the diarization pipeline
|
| 20 |
-
print("Loading Pyannote diarization pipeline...")
|
| 21 |
-
pipeline = Pipeline.from_pretrained(
|
| 22 |
-
"pyannote/speaker-diarization-3.1",
|
| 23 |
-
use_auth_token=HF_TOKEN
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# Move to GPU if available
|
| 27 |
-
if torch.cuda.is_available():
|
| 28 |
-
pipeline.to(torch.device("cuda"))
|
| 29 |
-
print("✅ Pipeline loaded on GPU")
|
| 30 |
-
else:
|
| 31 |
-
print("⚠️ GPU not available, using CPU")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def diarize_audio(audio_file, min_speakers=None, max_speakers=None):
|
| 35 |
-
"""
|
| 36 |
-
Perform speaker diarization on an audio file.
|
| 37 |
-
|
| 38 |
-
Args:
|
| 39 |
-
audio_file: Path to audio file or tuple (sample_rate, audio_data)
|
| 40 |
-
min_speakers: Minimum number of speakers (optional)
|
| 41 |
-
max_speakers: Maximum number of speakers (optional)
|
| 42 |
-
|
| 43 |
-
Returns:
|
| 44 |
-
List of dicts with speaker segments
|
| 45 |
-
"""
|
| 46 |
-
try:
|
| 47 |
-
# Prepare diarization parameters
|
| 48 |
-
diarization_kwargs = {}
|
| 49 |
-
if min_speakers is not None and min_speakers > 0:
|
| 50 |
-
diarization_kwargs["min_speakers"] = int(min_speakers)
|
| 51 |
-
if max_speakers is not None and max_speakers > 0:
|
| 52 |
-
diarization_kwargs["max_speakers"] = int(max_speakers)
|
| 53 |
-
|
| 54 |
-
# Run diarization
|
| 55 |
-
diarization = pipeline(audio_file, **diarization_kwargs)
|
| 56 |
-
|
| 57 |
-
# Convert to list of segments
|
| 58 |
-
segments = []
|
| 59 |
-
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 60 |
-
segments.append({
|
| 61 |
-
"start": float(turn.start),
|
| 62 |
-
"end": float(turn.end),
|
| 63 |
-
"speaker": speaker,
|
| 64 |
-
"confidence": 0.95, # Pyannote doesn't provide per-segment confidence
|
| 65 |
-
})
|
| 66 |
-
|
| 67 |
-
# Create summary
|
| 68 |
-
unique_speakers = len(set(s["speaker"] for s in segments))
|
| 69 |
-
summary = f"Found {unique_speakers} speakers in {len(segments)} segments"
|
| 70 |
-
|
| 71 |
-
# Format output for display
|
| 72 |
-
output_text = f"{summary}\n\n"
|
| 73 |
-
output_text += "Segments:\n"
|
| 74 |
-
for seg in segments[:10]: # Show first 10
|
| 75 |
-
output_text += f" {seg['start']:.2f}s - {seg['end']:.2f}s: {seg['speaker']}\n"
|
| 76 |
-
|
| 77 |
-
if len(segments) > 10:
|
| 78 |
-
output_text += f" ... and {len(segments) - 10} more segments\n"
|
| 79 |
-
|
| 80 |
-
return output_text, segments
|
| 81 |
-
|
| 82 |
-
except Exception as e:
|
| 83 |
-
error_msg = f"Error during diarization: {str(e)}"
|
| 84 |
-
print(error_msg)
|
| 85 |
-
return error_msg, []
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# Create Gradio interface
|
| 89 |
-
with gr.Blocks(title="ORCA360 Speaker Diarization") as demo:
|
| 90 |
-
gr.Markdown("# 🎙️ ORCA360 Speaker Diarization")
|
| 91 |
-
gr.Markdown("Upload an audio file to identify different speakers")
|
| 92 |
-
|
| 93 |
-
with gr.Row():
|
| 94 |
-
with gr.Column():
|
| 95 |
-
audio_input = gr.Audio(
|
| 96 |
-
label="Upload Audio File",
|
| 97 |
-
type="filepath",
|
| 98 |
-
sources=["upload"]
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
with gr.Row():
|
| 102 |
-
min_speakers = gr.Number(
|
| 103 |
-
label="Min Speakers (optional)",
|
| 104 |
-
value=None,
|
| 105 |
-
precision=0
|
| 106 |
-
)
|
| 107 |
-
max_speakers = gr.Number(
|
| 108 |
-
label="Max Speakers (optional)",
|
| 109 |
-
value=None,
|
| 110 |
-
precision=0
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
submit_btn = gr.Button("Diarize", variant="primary")
|
| 114 |
-
|
| 115 |
-
with gr.Column():
|
| 116 |
-
output_text = gr.Textbox(
|
| 117 |
-
label="Results",
|
| 118 |
-
lines=15,
|
| 119 |
-
max_lines=20
|
| 120 |
-
)
|
| 121 |
-
output_json = gr.JSON(label="Segments (JSON)")
|
| 122 |
-
|
| 123 |
-
# Handle button click
|
| 124 |
-
submit_btn.click(
|
| 125 |
-
fn=diarize_audio,
|
| 126 |
-
inputs=[audio_input, min_speakers, max_speakers],
|
| 127 |
-
outputs=[output_text, output_json]
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
# Examples
|
| 131 |
-
gr.Markdown("### Example Usage")
|
| 132 |
-
gr.Markdown("""
|
| 133 |
-
1. Upload an audio file (WAV, MP3, M4A, etc.)
|
| 134 |
-
2. Optionally specify min/max number of speakers
|
| 135 |
-
3. Click "Diarize" to process
|
| 136 |
-
4. View results showing speaker segments with timestamps
|
| 137 |
-
""")
|
| 138 |
-
|
| 139 |
-
# Launch with API enabled
|
| 140 |
-
if __name__ == "__main__":
|
| 141 |
-
demo.launch(
|
| 142 |
-
server_name="0.0.0.0",
|
| 143 |
-
server_port=7860,
|
| 144 |
-
share=False
|
| 145 |
-
)
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
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