Hervé BREDIN
commited on
Commit
·
0c6d72a
1
Parent(s):
9207240
feat: preparing for inference endpoint
Browse files- handler.py +58 -0
- requirements.txt +1 -0
handler.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MIT License
|
| 2 |
+
#
|
| 3 |
+
# Copyright (c) 2023 CNRS
|
| 4 |
+
#
|
| 5 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
# of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
# in the Software without restriction, including without limitation the rights
|
| 8 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
# copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
# furnished to do so, subject to the following conditions:
|
| 11 |
+
#
|
| 12 |
+
# The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
# copies or substantial portions of the Software.
|
| 14 |
+
#
|
| 15 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
# SOFTWARE.
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
from pyannote.audio import Pipeline, Audio
|
| 25 |
+
import torch
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class EndpointHandler:
|
| 29 |
+
def __init__(self, path=""):
|
| 30 |
+
# initialize pretrained pipeline
|
| 31 |
+
self._pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1")
|
| 32 |
+
|
| 33 |
+
# send pipeline to GPU if available
|
| 34 |
+
if torch.cuda.is_available():
|
| 35 |
+
self._pipeline.to(torch.device("cuda"))
|
| 36 |
+
|
| 37 |
+
# initialize audio reader
|
| 38 |
+
self._io = Audio()
|
| 39 |
+
|
| 40 |
+
def __call__(self, data):
|
| 41 |
+
inputs = data.pop("inputs", data)
|
| 42 |
+
waveform, sample_rate = self._io(inputs)
|
| 43 |
+
|
| 44 |
+
parameters = data.pop("parameters", dict())
|
| 45 |
+
diarization = self.pipeline(
|
| 46 |
+
{"waveform": waveform, "sample_rate": sample_rate}, **parameters
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
processed_diarization = [
|
| 50 |
+
{
|
| 51 |
+
"speaker": speaker,
|
| 52 |
+
"start": f"{turn.start:.3f}",
|
| 53 |
+
"end": f"{turn.end:.3f}",
|
| 54 |
+
}
|
| 55 |
+
for turn, _, speaker in diarization.itertracks(yield_label=True)
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
return {"diarization": processed_diarization}
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pyannote-audio==3.1.1
|