File size: 15,324 Bytes
0bb0d8e
 
 
87f3409
d990dd0
da9138c
7ac4ac2
da9138c
 
64259e4
acd8816
 
 
 
0bb0d8e
a3dba94
 
 
 
 
 
 
 
 
acd8816
16bc3e4
a8b126b
 
 
 
 
26eb097
a8b126b
 
16bc3e4
a8b126b
 
 
16bc3e4
acd8816
16bc3e4
d274746
da9138c
57968e0
a96aeb1
d274746
da9138c
d274746
 
 
 
 
 
 
 
 
 
 
 
 
57968e0
d274746
 
acd8816
 
a96aeb1
 
57968e0
a96aeb1
 
 
 
 
 
 
 
 
 
 
57968e0
a96aeb1
 
 
57968e0
a96aeb1
 
 
 
 
 
 
57968e0
a96aeb1
 
d274746
57968e0
d274746
 
 
 
 
 
 
 
 
 
 
57968e0
d274746
 
a96aeb1
57968e0
a96aeb1
 
 
64259e4
a96aeb1
 
 
 
 
 
 
 
 
 
64259e4
57968e0
a96aeb1
 
 
57968e0
a96aeb1
 
 
57968e0
a96aeb1
 
64259e4
57968e0
d274746
 
 
57968e0
d274746
 
 
57968e0
d274746
 
da9138c
d274746
 
 
 
 
57968e0
d274746
 
 
 
acd8816
 
 
d274746
57968e0
64259e4
d274746
 
57968e0
d274746
 
64259e4
57968e0
acd8816
64259e4
acd8816
 
57968e0
64259e4
 
acd8816
 
 
 
57968e0
acd8816
 
d680d0f
 
57968e0
d680d0f
57968e0
d680d0f
57968e0
 
 
26eb097
d680d0f
a96aeb1
26eb097
 
 
d811f00
 
 
d680d0f
 
 
 
 
 
 
 
a8b126b
 
 
 
 
d680d0f
a8b126b
 
 
 
 
 
 
 
 
 
 
d680d0f
a96aeb1
 
d274746
26eb097
d811f00
 
 
 
d274746
 
a96aeb1
 
 
64259e4
 
 
26eb097
d811f00
 
64259e4
1c4f98a
 
 
a8b126b
1c4f98a
a8b126b
1c4f98a
a96aeb1
64259e4
acd8816
 
d274746
 
 
 
 
64259e4
26eb097
d274746
 
 
 
1c4f98a
 
 
 
 
d274746
acd8816
26eb097
a96aeb1
d274746
26eb097
a96aeb1
 
26eb097
d274746
a96aeb1
 
 
 
 
 
 
64259e4
 
 
 
 
ae4be5c
d5bbd76
 
 
 
 
 
834c15f
 
 
 
 
 
 
 
 
 
d5bbd76
 
 
 
834c15f
 
 
 
 
d5bbd76
 
 
 
a3dba94
d5bbd76
 
 
 
 
 
 
 
ed10fe0
 
 
 
 
 
 
 
 
ae4be5c
d5bbd76
a3dba94
dbb0e1e
 
a3dba94
d5bbd76
a3dba94
d5bbd76
 
 
 
 
 
a3dba94
d5bbd76
a3dba94
d5bbd76
 
 
1c4f98a
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import gradio as gr
import os
import time
import sys
import io
import tempfile
import subprocess
import requests
from urllib.parse import urlparse
from pydub import AudioSegment
import logging
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
import yt_dlp

class LogCapture(io.StringIO):
    def __init__(self, callback):
        super().__init__()
        self.callback = callback

    def write(self, s):
        super().write(s)
        self.callback(s)
        
logging.basicConfig(level=logging.INFO)

# Clone and install faster-whisper from GitHub
try:
    subprocess.run(["git", "clone", "https://github.com/SYSTRAN/faster-whisper.git"], check=True)
    subprocess.run(["pip", "install", "-e", "./faster-whisper"], check=True)
except subprocess.CalledProcessError as e:
    logging.error(f"Error during faster-whisper installation: {e}")
    sys.exit(1)

sys.path.append("./faster-whisper")

from faster_whisper import WhisperModel
from faster_whisper.transcribe import BatchedInferencePipeline

device = "cuda:0" if torch.cuda.is_available() else "cpu"

def download_audio(url, method_choice):
    parsed_url = urlparse(url)
    logging.info(f"Downloading audio from URL: {url} using method: {method_choice}")
    if parsed_url.netloc in ['www.youtube.com', 'youtu.be', 'youtube.com']:
        return download_youtube_audio(url, method_choice)
    else:
        return download_direct_audio(url, method_choice)

def download_youtube_audio(url, method_choice):
    methods = {
        'yt-dlp': youtube_dl_method,
        'pytube': pytube_method,
        'youtube-dl': youtube_dl_classic_method,
        'yt-dlp-alt': youtube_dl_alternative_method,
        'ffmpeg': ffmpeg_method,
        'aria2': aria2_method
    }
    method = methods.get(method_choice, youtube_dl_method)
    try:
        logging.info(f"Attempting to download YouTube audio using {method_choice}")
        return method(url)
    except Exception as e:
        logging.error(f"Error downloading using {method_choice}: {str(e)}")
        return None

def youtube_dl_method(url):
    logging.info("Using yt-dlp method")
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'outtmpl': '%(id)s.%(ext)s',
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=True)
        logging.info(f"Downloaded YouTube audio: {info['id']}.mp3")
        return f"{info['id']}.mp3"

def pytube_method(url):
    logging.info("Using pytube method")
    from pytube import YouTube
    yt = YouTube(url)
    audio_stream = yt.streams.filter(only_audio=True).first()
    out_file = audio_stream.download()
    base, ext = os.path.splitext(out_file)
    new_file = base + '.mp3'
    os.rename(out_file, new_file)
    logging.info(f"Downloaded and converted audio to: {new_file}")
    return new_file

def youtube_dl_classic_method(url):
    logging.info("Using youtube-dl classic method")
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'outtmpl': '%(id)s.%(ext)s',
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=True)
        logging.info(f"Downloaded YouTube audio: {info['id']}.mp3")
        return f"{info['id']}.mp3"

def youtube_dl_alternative_method(url):
    logging.info("Using yt-dlp alternative method")
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio', 
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'outtmpl': '%(id)s.%(ext)s',
        'no_warnings': True,
        'quiet': True,
        'no_check_certificate': True,
        'prefer_insecure': True,
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=True) 
        logging.info(f"Downloaded YouTube audio: {info['id']}.mp3")
        return f"{info['id']}.mp3"

def ffmpeg_method(url):
    logging.info("Using ffmpeg method")
    output_file = tempfile.mktemp(suffix='.mp3')
    command = ['ffmpeg', '-i', url, '-vn', '-acodec', 'libmp3lame', '-q:a', '2', output_file]
    subprocess.run(command, check=True, capture_output=True)
    logging.info(f"Downloaded and converted audio to: {output_file}")
    return output_file

def aria2_method(url):  
    logging.info("Using aria2 method")
    output_file = tempfile.mktemp(suffix='.mp3')
    command = ['aria2c', '--split=4', '--max-connection-per-server=4', '--out', output_file, url]
    subprocess.run(command, check=True, capture_output=True)
    logging.info(f"Downloaded audio to: {output_file}")
    return output_file

def download_direct_audio(url, method_choice):
    logging.info(f"Downloading direct audio from: {url} using method: {method_choice}")
    if method_choice == 'wget':
        return wget_method(url)
    else:
        try:
            response = requests.get(url)
            if response.status_code == 200:
                with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
                    temp_file.write(response.content)
                    logging.info(f"Downloaded direct audio to: {temp_file.name}")
                    return temp_file.name
            else:
                raise Exception(f"Failed to download audio from {url}")
        except Exception as e:
            logging.error(f"Error downloading direct audio: {str(e)}")
            return None

def wget_method(url):
    logging.info("Using wget method")
    output_file = tempfile.mktemp(suffix='.mp3')  
    command = ['wget', '-O', output_file, url]
    subprocess.run(command, check=True, capture_output=True)
    logging.info(f"Downloaded audio to: {output_file}")
    return output_file

def trim_audio(audio_path, start_time, end_time):
    logging.info(f"Trimming audio from {start_time} to {end_time}")
    audio = AudioSegment.from_file(audio_path)
    trimmed_audio = audio[start_time*1000:end_time*1000] if end_time else audio[start_time*1000:]
    trimmed_audio_path = tempfile.mktemp(suffix='.wav')
    trimmed_audio.export(trimmed_audio_path, format="wav")
    logging.info(f"Trimmed audio saved to: {trimmed_audio_path}")
    return trimmed_audio_path

def save_transcription(transcription):
    file_path = tempfile.mktemp(suffix='.txt')
    with open(file_path, 'w') as f:
        f.write(transcription)
    logging.info(f"Transcription saved to: {file_path}")
    return file_path

def get_model_options(pipeline_type):
    if pipeline_type == "faster-batched":
        return ["cstr/whisper-large-v3-turbo-int8_float32", "deepdml/faster-whisper-large-v3-turbo-ct2", "Systran/faster-whisper-large-v3", "GalaktischeGurke/primeline-whisper-large-v3-german-ct2"]
    elif pipeline_type == "faster-sequenced":
        return ["cstr/whisper-large-v3-turbo-int8_float32", "deepdml/faster-whisper-large-v3-turbo-ct2", "Systran/faster-whisper-large-v3", "GalaktischeGurke/primeline-whisper-large-v3-german-ct2"]
    elif pipeline_type == "transformers":
        return ["openai/whisper-large-v3", "openai/whisper-large-v3-turbo", "primeline/whisper-large-v3-german"]
    else:
        return []

def transcribe_audio(input_source, pipeline_type, model_id, dtype, batch_size, download_method, start_time=None, end_time=None, verbose=False):
    try:
        logging.info(f"Transcription parameters: pipeline_type={pipeline_type}, model_id={model_id}, dtype={dtype}, batch_size={batch_size}, download_method={download_method}")
        verbose_messages = f"Starting transcription with parameters:\nPipeline Type: {pipeline_type}\nModel ID: {model_id}\nData Type: {dtype}\nBatch Size: {batch_size}\nDownload Method: {download_method}\n"

        if verbose:
            yield verbose_messages, "", None

        if pipeline_type == "faster-batched":
            model = WhisperModel(model_id, device="auto", compute_type=dtype)
            pipeline = BatchedInferencePipeline(model=model)
        elif pipeline_type == "faster-sequenced":
            model = WhisperModel(model_id)
            pipeline = model.transcribe
        elif pipeline_type == "transformers":
            torch_dtype = torch.float16 if dtype == "float16" else torch.float32
            model = AutoModelForSpeechSeq2Seq.from_pretrained(
                model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
            )
            model.to(device)
            processor = AutoProcessor.from_pretrained(model_id)
            pipeline = pipeline(
                "automatic-speech-recognition",
                model=model,
                tokenizer=processor.tokenizer,
                feature_extractor=processor.feature_extractor,
                chunk_length_s=30,
                batch_size=batch_size,
                return_timestamps=True,
                torch_dtype=torch_dtype,
                device=device,
            )
        else:
            raise ValueError("Invalid pipeline type")

        if isinstance(input_source, str) and (input_source.startswith('http://') or input_source.startswith('https://')):
            audio_path = download_audio(input_source, download_method)
            verbose_messages += f"Audio file downloaded: {audio_path}\n"
            if verbose:
                yield verbose_messages, "", None

            if not audio_path or audio_path.startswith("Error"):
                yield f"Error: {audio_path}", "", None
                return
        else:
            audio_path = input_source

        if start_time is not None or end_time is not None:
            trimmed_audio_path = trim_audio(audio_path, start_time or 0, end_time)
            audio_path = trimmed_audio_path
            verbose_messages += f"Audio trimmed from {start_time} to {end_time}\n"
            if verbose:
                yield verbose_messages, "", None

        start_time_perf = time.time()
        if pipeline_type in ["faster-batched", "faster-sequenced"]:
            segments, info = pipeline(audio_path, batch_size=batch_size)
        else:
            result = pipeline(audio_path)
            segments = result["chunks"]
        end_time_perf = time.time()

        transcription_time = end_time_perf - start_time_perf
        audio_file_size = os.path.getsize(audio_path) / (1024 * 1024)

        metrics_output = (
            f"Transcription time: {transcription_time:.2f} seconds\n"
            f"Audio file size: {audio_file_size:.2f} MB\n"
        )

        if verbose:
            yield verbose_messages + metrics_output, "", None

        transcription = ""

        for segment in segments:
            transcription_segment = (
                f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}\n"
                if pipeline_type in ["faster-batched", "faster-sequenced"] else
                f"[{segment['timestamp'][0]:.2f}s -> {segment['timestamp'][1]:.2f}s] {segment['text']}\n"
            )
            transcription += transcription_segment
            if verbose:
                yield verbose_messages + metrics_output, transcription, None

        transcription_file = save_transcription(transcription)
        yield verbose_messages + metrics_output, transcription, transcription_file

    except Exception as e:
        logging.error(f"An error occurred during transcription: {str(e)}")
        yield f"An error occurred: {str(e)}", "", None

    finally:
        if isinstance(input_source, str) and (input_source.startswith('http://') or input_source.startswith('https://')):
            try:
                os.remove(audio_path)
            except:
                pass
        if start_time is not None or end_time is not None:
            try:
                os.remove(trimmed_audio_path)
            except:
                pass

with gr.Blocks() as iface:
    gr.Markdown("# Multi-Pipeline Transcription")
    gr.Markdown("Transcribe audio using multiple pipelines and models.")
    
    with gr.Row():
        input_source = gr.Textbox(label="Audio Source (Upload, URL, or YouTube URL)")
        pipeline_type = gr.Dropdown(
            choices=["faster-batched", "faster-sequenced", "transformers"],
            label="Pipeline Type",
            value="faster-batched"
        )
        model_id = gr.Dropdown(
            label="Model",
            choices=get_model_options("faster-batched"),
            value=get_model_options("faster-batched")[0]
        )
    
    with gr.Row():
        dtype = gr.Dropdown(choices=["int8", "float16", "float32"], label="Data Type", value="int8")
        batch_size = gr.Slider(minimum=1, maximum=32, step=1, value=16, label="Batch Size")
        download_method = gr.Dropdown(
            choices=["yt-dlp", "pytube", "youtube-dl", "yt-dlp-alt", "ffmpeg", "aria2", "wget"],
            label="Download Method",
            value="yt-dlp"
        )
    
    with gr.Row():
        start_time = gr.Number(label="Start Time (seconds)", value=0)
        end_time = gr.Number(label="End Time (seconds)", value=0)
        verbose = gr.Checkbox(label="Verbose Output", value=True)  # Set to True by default
    
    transcribe_button = gr.Button("Transcribe")
    
    with gr.Row():
        metrics_output = gr.Textbox(label="Transcription Metrics and Verbose Messages", lines=10)
        transcription_output = gr.Textbox(label="Transcription", lines=10)
        transcription_file = gr.File(label="Download Transcription")
    
    def update_model_dropdown(pipeline_type):
        model_choices = get_model_options(pipeline_type)
        logging.info(f"Model choices for {pipeline_type}: {model_choices}")
        
        if model_choices:
            return gr.Dropdown.update(choices=model_choices, value=model_choices[0], visible=True)
        else:
            return gr.Dropdown.update(choices=["No models available"], value=None, visible=False)
    
    pipeline_type.change(update_model_dropdown, inputs=pipeline_type, outputs=model_id)
    
    def transcribe_with_progress(*args):
        for result in transcribe_audio(*args):
            yield result
    
    transcribe_button.click(
        transcribe_with_progress,
        inputs=[input_source, pipeline_type, model_id, dtype, batch_size, download_method, start_time, end_time, verbose],
        outputs=[metrics_output, transcription_output, transcription_file]
    )
    
    gr.Examples(
        examples=[
            ["https://www.youtube.com/watch?v=daQ_hqA6HDo", "faster-batched", "cstr/whisper-large-v3-turbo-int8_float32", "int8", 16, "yt-dlp", 0, None, True],
            ["https://mcdn.podbean.com/mf/web/dir5wty678b6g4vg/HoP_453_-_The_Price_is_Right_-_Law_and_Economics_in_the_Second_Scholastic5yxzh.mp3", "faster-sequenced", "deepdml/faster-whisper-large-v3-turbo-ct2", "float16", 1, "ffmpeg", 0, 300, True],
            ["path/to/local/audio.mp3", "transformers", "openai/whisper-large-v3", "float16", 16, "yt-dlp", 60, 180, True]
        ],
        inputs=[input_source, pipeline_type, model_id, dtype, batch_size, download_method, start_time, end_time, verbose],
    )

iface.launch()