Alex0505 commited on
Commit
cf4e767
·
verified ·
1 Parent(s): bdab6d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import HfApi
2
+ available_pipelines = [p.modelId for p in HfApi().list_models(filter="pyannote-audio-pipeline")]
3
+ list(filter(lambda p: p.startswith("pyannote/"), available_pipelines))
4
+
5
+ from pyannote.audio import Pipeline
6
+ pipeline_diar = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1", use_auth_token=True)
7
+
8
+ import torch
9
+ import torchaudio
10
+ import base64
11
+ from io import BytesIO
12
+
13
+ from pyannote.core import Annotation
14
+
15
+ from pydub import AudioSegment
16
+
17
+ from transformers import pipeline
18
+ transcriber = pipeline(model="openai/whisper-large-v2", return_timestamps=False)
19
+
20
+ import gradio as gr
21
+
22
+ pipeline_diar.to(torch.device("cuda"))
23
+
24
+ def transcribe_recording(base64_encoded_audio):
25
+ """
26
+ Transcribes audio data given as base64 encoded byte string into text and yields
27
+ the transcribed text incrementally as it becomes available.
28
+
29
+ Parameters:
30
+ - base64_audio (str): Base64 encoded byte string of audio data.
31
+
32
+ Yields:
33
+ - str: Transcribed text incrementally as it becomes available.
34
+ """
35
+ # Decode the base64 encoded string
36
+ base64_decoded_audio = base64.b64decode(base64_encoded_audio)
37
+
38
+ with BytesIO(base64_decoded_audio) as audio_buffer:
39
+ audio_buffer.seek(0)
40
+ # Load audio with pydub for easy slicing
41
+ audio = AudioSegment.from_file(audio_buffer)
42
+ audio.export("out.mp3", format="mp3")
43
+
44
+ # Run diarization pipeline
45
+ dia = pipeline_diar("out.mp3")
46
+ assert isinstance(dia, Annotation)
47
+ # Prepare a list to store data
48
+ data = []
49
+
50
+ for i, (speech_turn, track, speaker) in enumerate(dia.itertracks(yield_label=True)):
51
+ # Extract start and end times
52
+ start_time, end_time = speech_turn.start, speech_turn.end
53
+
54
+ # Extract and transcribe the audio segment
55
+ segment_audio = audio[int(start_time * 1000):int(end_time * 1000)]
56
+ segment_audio.export("out.mp3", format="mp3")
57
+ text = transcriber("out.mp3")['text']
58
+
59
+ # Append the data to the list
60
+ data.append({
61
+ "Start Time": start_time,
62
+ "End Time": end_time,
63
+ "Speaker": speaker,
64
+ "Transcription": text
65
+ })
66
+
67
+ return data
68
+
69
+ tanscriber = gr.Interface(
70
+ fn=transcribe_recording,
71
+ inputs="encoded byte string",
72
+ outputs="depostion transcript",
73
+ title="whisper transcriber",
74
+ ).launch()