File size: 5,640 Bytes
c174364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffbe5dc
c174364
 
 
 
 
ebc74bd
 
 
b171431
c174364
 
b171431
c174364
ebc74bd
 
c174364
 
 
 
 
 
 
 
 
 
ebc74bd
 
b171431
ebc74bd
57604a5
c174364
57604a5
ebc74bd
57604a5
 
 
 
 
 
 
 
 
 
ebc74bd
 
c174364
1c1581e
b41dea0
c174364
ebc74bd
 
 
57604a5
 
ebc74bd
c174364
57604a5
 
 
 
 
 
 
 
 
 
 
 
ebc74bd
57604a5
ebc74bd
 
 
 
 
 
b171431
 
 
 
 
c174364
b171431
 
 
317657b
 
ebc74bd
b171431
 
 
 
 
 
 
 
 
 
ebc74bd
57604a5
c174364
57604a5
c174364
 
 
 
 
 
 
 
 
 
 
 
 
57604a5
 
 
c174364
 
 
 
 
57604a5
c174364
 
 
ebc74bd
 
 
 
 
57604a5
ebc74bd
 
56a16bc
57604a5
 
 
 
ebc74bd
57604a5
 
 
ebc74bd
57604a5
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# MIT License
#
# Copyright (c) 2022- CNRS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import torch
import io
import base64
import numpy as np
import scipy.io.wavfile
from typing import Text
import streamlit as st
from pyannote.audio import Pipeline
from pyannote.audio import Audio
from pyannote.audio.pipelines.utils.hook import TimingHook
from pyannote.core import Segment


import streamlit.components.v1 as components


def to_base64(waveform: np.ndarray, sample_rate: int = 16000) -> Text:
    """Convert waveform to base64 data"""
    waveform /= np.max(np.abs(waveform)) + 1e-8
    with io.BytesIO() as content:
        scipy.io.wavfile.write(content, sample_rate, waveform)
        content.seek(0)
        b64 = base64.b64encode(content.read()).decode()
        b64 = f"data:audio/x-wav;base64,{b64}"
    return b64


PYANNOTE_LOGO = "https://avatars.githubusercontent.com/u/7559051?s=400&v=4"
EXCERPT = 120

st.set_page_config(page_title="pyannote pretrained pipelines", page_icon=PYANNOTE_LOGO)

col1, col2 = st.columns([0.2, 0.8], gap="small")

with col1:
    st.image(PYANNOTE_LOGO)

with col2:
    st.markdown(
        """
# pretrained pipelines
Make the most of [pyannote](https://github.com/pyannote) thanks to our [consulting services](https://herve.niderb.fr/consulting.html)
"""
    )


PIPELINES = [
    "pyannote/speaker-diarization-3.1",
    # "pyannote/speaker-diarization-3.0",
]

audio = Audio(sample_rate=16000, mono=True)

selected_pipeline = st.selectbox("Select a pretrained pipeline", PIPELINES, index=0)


with st.spinner("Loading pipeline..."):
    try:
        use_auth_token = st.secrets["PYANNOTE_TOKEN"]
    except FileNotFoundError:
        use_auth_token = None
    except KeyError:
        use_auth_token = None

    pipeline = Pipeline.from_pretrained(
        selected_pipeline, use_auth_token=use_auth_token
    )
    if torch.cuda.is_available():
        pipeline.to(torch.device("cuda"))

uploaded_file = st.file_uploader("Upload an audio file")
if uploaded_file is not None:
    try:
        duration = audio.get_duration(uploaded_file)
    except RuntimeError as e:
        st.error(e)
        st.stop()

    spinner_message = (
        f"Processing {duration:.0f}s file... "
        if duration < EXCERPT
        else f"Processing first {EXCERPT:.0f}s of file..."
    )

    duration = min(duration, EXCERPT)
    waveform, sample_rate = audio.crop(uploaded_file, Segment(0, duration))
    uri = "".join(uploaded_file.name.split())
    file = {"waveform": waveform, "sample_rate": sample_rate, "uri": uri}

    with st.spinner(spinner_message):
        with TimingHook() as hook:
            output = pipeline(file, hook=hook)

    processing_time = file["timing"]["total"]
    faster_than_real_time = duration / processing_time
    st.success(
        f"Processed {duration:.0f}s of audio in {processing_time:.1f}s ({faster_than_real_time:.1f}x faster than real-time)",
        icon="✅",
    )

    with open("assets/template.html") as html, open("assets/style.css") as css:
        html_template = html.read()
        st.markdown("<style>{}</style>".format(css.read()), unsafe_allow_html=True)

    colors = [
        "#ffd70033",
        "#00ffff33",
        "#ff00ff33",
        "#00ff0033",
        "#9932cc33",
        "#00bfff33",
        "#ff7f5033",
        "#66cdaa33",
    ]
    num_colors = len(colors)

    label2color = {
        label: colors[k % num_colors] for k, label in enumerate(sorted(output.labels()))
    }

    BASE64 = to_base64(waveform.numpy().T)

    REGIONS = ""
    for segment, _, label in output.itertracks(yield_label=True):
        REGIONS += f"regions.addRegion({{start: {segment.start:g}, end: {segment.end:g}, color: '{label2color[label]}', resize : false, drag : false}});"

    html = html_template.replace("BASE64", BASE64).replace("REGIONS", REGIONS)
    components.html(html, height=250, scrolling=True)

    with io.StringIO() as fp:
        output.write_rttm(fp)
        content = fp.getvalue()
        b64 = base64.b64encode(content.encode()).decode()
        href = f'<a download="{output.uri}.rttm" href="data:file/text;base64,{b64}">Download</a> result in RTTM file format or run it locally:'
        st.markdown(href, unsafe_allow_html=True)

    code = f"""
# load pretrained pipeline
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained("{selected_pipeline}", 
                                    use_auth_token=HUGGINGFACE_TOKEN)

# (optional) send pipeline to GPU
import torch
pipeline.to(torch.device("cuda"))

# process audio file
output = pipeline("audio.wav")"""
    st.code(code, language="python")