Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pyannote.audio import Pipeline
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Set page title
|
| 7 |
+
st.set_page_config(page_title="Speaker Diarization App")
|
| 8 |
+
|
| 9 |
+
st.title("Speaker Diarization App")
|
| 10 |
+
|
| 11 |
+
# File uploader
|
| 12 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=['wav', 'mp3'])
|
| 13 |
+
|
| 14 |
+
# Hugging Face access token input
|
| 15 |
+
hf_token = st.text_input("Enter your Hugging Face access token:", type="password")
|
| 16 |
+
|
| 17 |
+
if uploaded_file is not None and hf_token:
|
| 18 |
+
# Save uploaded file temporarily
|
| 19 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
|
| 20 |
+
tmp_file.write(uploaded_file.getvalue())
|
| 21 |
+
tmp_path = tmp_file.name
|
| 22 |
+
|
| 23 |
+
# Instantiate the pipeline
|
| 24 |
+
@st.cache_resource
|
| 25 |
+
def load_pipeline(token):
|
| 26 |
+
return Pipeline.from_pretrained(
|
| 27 |
+
"pyannote/speaker-diarization-3.1",
|
| 28 |
+
use_auth_token=token
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
pipeline = load_pipeline(hf_token)
|
| 33 |
+
|
| 34 |
+
# Run the pipeline on the audio file
|
| 35 |
+
with st.spinner('Processing audio...'):
|
| 36 |
+
diarization = pipeline(tmp_path)
|
| 37 |
+
|
| 38 |
+
# Generate RTTM content
|
| 39 |
+
rttm_content = ""
|
| 40 |
+
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 41 |
+
rttm_line = f"SPEAKER {os.path.basename(tmp_path)} 1 {turn.start:.3f} {turn.duration:.3f} <NA> <NA> {speaker} <NA> <NA>\n"
|
| 42 |
+
rttm_content += rttm_line
|
| 43 |
+
|
| 44 |
+
# Display RTTM content
|
| 45 |
+
st.subheader("Diarization Results (RTTM format)")
|
| 46 |
+
st.text_area("RTTM Output", rttm_content, height=300)
|
| 47 |
+
|
| 48 |
+
# Provide download button for RTTM file
|
| 49 |
+
st.download_button(
|
| 50 |
+
label="Download RTTM file",
|
| 51 |
+
data=rttm_content,
|
| 52 |
+
file_name="diarization.rttm",
|
| 53 |
+
mime="text/plain"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
st.error(f"An error occurred: {str(e)}")
|
| 58 |
+
|
| 59 |
+
# Clean up the temporary file
|
| 60 |
+
os.unlink(tmp_path)
|
| 61 |
+
|
| 62 |
+
else:
|
| 63 |
+
st.info("Please upload an audio file and enter your Hugging Face access token to start.")
|