zxyse commited on
Commit
6ad6b66
·
verified ·
1 Parent(s): b960f25

Upload 6 files

Browse files
README.md CHANGED
@@ -1,12 +1,40 @@
1
- ---
2
- title: Transcribe
3
- emoji: 🐢
4
- colorFrom: red
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 4.38.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/EliasVincent/whisper-subtitles-webui/blob/master/colab/whisper_subtitles_webui_colab.ipynb)
2
+
3
+ # What
4
+
5
+ A gradio frontend for generating transcribed or translated subtitles for videos using OpenAI Whisper locally.
6
+
7
+ # Why
8
+
9
+ I just wanted a nice frontend where you can just drop a video or url and it will spit out subs. Whisper is amazing but I haven't found that many implementations, especially ones that can be run locally.
10
+
11
+ ![](img/1.png)
12
+
13
+ # Install
14
+
15
+ ```
16
+ python -m venv .
17
+ .\Scripts\activate
18
+
19
+ # if this doesn't work, pip install the following manually: openai-whisper ffmpeg torch gradio
20
+
21
+ pip install -r requirements.txt
22
+
23
+ python server.py
24
+ ```
25
+
26
+ To share, add `--remote=True`.
27
+
28
+ # Features
29
+
30
+ - Input a video or any other media file
31
+ - Input a YouTube URL
32
+ - Transcribe
33
+ - Translate to English
34
+ - Select different models for your hardware
35
+ - CUDA support
36
+ - Output .srt or video file with embedded subtitles
37
+
38
+ # Troubleshooting
39
+
40
+ If the output says `gpu available: False` [you might need to pip install a different version of Torch for your specific hardware](https://pytorch.org/get-started/locally/#start-locally)
colab/whisper_subtitles_webui_colab.ipynb ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ },
15
+ "accelerator": "GPU",
16
+ "gpuClass": "standard"
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "markdown",
21
+ "source": [
22
+ "# Clone and install dependencies"
23
+ ],
24
+ "metadata": {
25
+ "id": "ZiuEHGjLiLq-"
26
+ }
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "source": [
31
+ "! git clone https://github.com/EliasVincent/whisper-subtitles-webui\n",
32
+ "! pip install -U pip\n",
33
+ "! pip install -r whisper-subtitles-webui/requirements.txt\n",
34
+ "! add-apt-repository -y ppa:savoury1/ffmpeg4\n",
35
+ "! apt-get -qq install -y ffmpeg\n"
36
+ ],
37
+ "metadata": {
38
+ "id": "cdhoSxCJiMFA"
39
+ },
40
+ "execution_count": null,
41
+ "outputs": []
42
+ },
43
+ {
44
+ "cell_type": "markdown",
45
+ "source": [
46
+ "# Start server"
47
+ ],
48
+ "metadata": {
49
+ "id": "zj_Xi9baiqQk"
50
+ }
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "source": [
55
+ "! python whisper-subtitles-webui/server.py --remote=True"
56
+ ],
57
+ "metadata": {
58
+ "id": "fOC6bU2nui_r"
59
+ },
60
+ "execution_count": null,
61
+ "outputs": []
62
+ }
63
+ ]
64
+ }
img/1.png ADDED
requirements.txt ADDED
Binary file (96 Bytes). View file
 
server.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import ffmpeg
3
+ import argparse
4
+ import tempfile
5
+ import torch
6
+ import whisper
7
+ from whisper.utils import get_writer
8
+
9
+ import ytdlp_functions
10
+
11
+
12
+ def download_video(url, quick, language, model, task, addSrtToVideo):
13
+ if quick:
14
+ returned_yt_file = ytdlp_functions.download_quick_mp4(url=url, folder=str(tempfile.gettempdir()))
15
+
16
+ return transcribe(
17
+ inputFile=returned_yt_file,
18
+ language=language,
19
+ model=model,
20
+ task=task,
21
+ addSrtToVideo=addSrtToVideo)
22
+
23
+
24
+ def transcribe(inputFile, language, model, task, addSrtToVideo):
25
+ print("gpu available: " + str(torch.cuda.is_available()))
26
+ gpu = torch.cuda.is_available()
27
+ model = whisper.load_model(model)
28
+ # ytdlp_functions will give us a string, gradio filepicker an actual file
29
+ inputFileCleared = inputFile if isinstance(inputFile, str) else inputFile.name
30
+
31
+ whisperOutput = model.transcribe(
32
+ inputFileCleared,
33
+ task=task,
34
+ language=language,
35
+ verbose=True,
36
+ fp16=gpu
37
+ )
38
+
39
+ writer = get_writer("srt", str(tempfile.gettempdir()))
40
+ writer(whisperOutput, inputFileCleared)
41
+ # broken srt filepaths. Use those if tempdir acts weird.
42
+ # srtFile = f"{inputFileCleared}" + ".srt"
43
+ # anotherSrtFile = inputFileCleared.rsplit(".", 2)[0] + ".srt"
44
+ srtFile = inputFileCleared.rsplit(".", 1)[0] + ".srt"
45
+ if addSrtToVideo:
46
+ video_out = inputFileCleared + "_output.mkv"
47
+
48
+ input_ffmpeg = ffmpeg.input(inputFileCleared)
49
+ input_ffmpeg_sub = ffmpeg.input(srtFile)
50
+
51
+ input_video = input_ffmpeg['v']
52
+ input_audio = input_ffmpeg['a']
53
+ input_subtitles = input_ffmpeg_sub['s']
54
+ stream = ffmpeg.output(
55
+ input_video, input_audio, input_subtitles, video_out,
56
+ vcodec='copy', acodec='copy', scodec='srt'
57
+ )
58
+ stream = ffmpeg.overwrite_output(stream)
59
+ ffmpeg.run(stream)
60
+ return video_out
61
+
62
+ return srtFile
63
+
64
+
65
+ with gr.Blocks() as app:
66
+ gr.Markdown("# whisper-subtitles-webui")
67
+ with gr.Tab("Subtitle Video"):
68
+ st_file = gr.File()
69
+ st_lang = gr.Textbox(label="Language", placeholder="source language (en, de, ja, ..)")
70
+ st_model = gr.Dropdown(["tiny", "small", "medium", "large", ], label="Model", value="tiny")
71
+ st_task = gr.Radio(["transcribe", "translate"], label="Task", value="translate")
72
+ st_embed = gr.Checkbox(label="embed subtitles into video file")
73
+ st_file_out = gr.File()
74
+ st_start_button = gr.Button("Run", variant="primary")
75
+ with gr.Tab("YouTube to Subtitle"):
76
+ gr.Markdown(">try to update yt-dlp if downloads don't work")
77
+ yt_url = gr.Textbox(label="YouTube URL", placeholder="YouTube URL")
78
+ yt_quick = gr.Checkbox(label="Quick settings", value=True, interactive=False)
79
+ yt_lang = gr.Textbox(label="Language", placeholder="source language (en, de, ja, ..)")
80
+ yt_model = gr.Dropdown(["tiny", "small", "medium", "large"], label="Model", value="tiny")
81
+ yt_task = gr.Radio(["transcribe", "translate"], label="Task", value="translate")
82
+ yt_embed = gr.Checkbox(label="embed subtitles into video file")
83
+ yt_file_out = gr.File()
84
+ yt_start_button = gr.Button("Download and Run", variant="primary")
85
+
86
+ st_start_button.click(fn=transcribe, inputs=
87
+ [st_file,
88
+ st_lang,
89
+ st_model,
90
+ st_task,
91
+ st_embed
92
+ ], outputs=st_file_out, api_name="video_to_subs")
93
+ yt_start_button.click(fn=download_video, inputs=
94
+ [
95
+ yt_url,
96
+ yt_quick,
97
+ yt_lang,
98
+ yt_model,
99
+ yt_task,
100
+ yt_embed,
101
+ ], outputs=yt_file_out, api_name="yt_to_subs")
102
+ parser = argparse.ArgumentParser(description='Share option')
103
+ parser.add_argument('--remote', type=bool, help='share', default=False)
104
+ args = parser.parse_args()
105
+ app.launch(share=args.remote)
ytdlp_functions.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from yt_dlp import YoutubeDL
2
+ import os
3
+
4
+ def download_quick_mp4(url, folder):
5
+ ytdl_format_options = {
6
+ 'outtmpl': os.path.join(folder, '%(title)s-%(id)s.%(ext)s')
7
+ }
8
+
9
+ with YoutubeDL(ytdl_format_options) as ydl:
10
+ info = ydl.extract_info(url, download=False)
11
+ file = ydl.download(url)
12
+
13
+ return os.path.join(folder, f"{info['title']}-{info['id']}.{info['ext']}")