-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp.py
More file actions
177 lines (159 loc) · 7.13 KB
/
app.py
File metadata and controls
177 lines (159 loc) · 7.13 KB
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
import gradio as gr
from logic import *
from environment import OUTPUT_PATH, DOWNLOAD_PATH, RECORDING_PATH, CACHE_FILE, DEFAULT_MODEL_LANGUAGE, DEFAULT_MODEL, DEFAULT_LANGUAGE
# create output folder
OUTPUT_PATH.mkdir(parents=True, exist_ok=True)
# create download folder
DOWNLOAD_PATH.mkdir(parents=True, exist_ok=True)
# create recording folder
RECORDING_PATH.mkdir(parents=True, exist_ok=True)
# update or create cache file
update_cachefile(CACHE_FILE)
with gr.Blocks(css=get_css(), title="Transcription", theme="default") as page:
# Title
gr.Markdown(
"""## Generate a transcript for YouTube, a video/audio file or your microphone.""")
# Whisper Model Options
with gr.Row() as r1:
# model language
with gr.Column(scale=1) as c1_1:
model_language = gr.Radio(
choices=["multilingual", "english-only"],
label="Choose a model language",
value=DEFAULT_MODEL_LANGUAGE
)
# model size
with gr.Column(scale=2) as c1_2:
model = gr.Radio(
choices=["tiny", "base", "small", "medium", "large"],
label="Choose a model",
value=DEFAULT_MODEL
)
# audio or translation language
with gr.Column(scale=1) as c1_3:
language = gr.Dropdown(
label="Select a language (original or english for translation)",
choices=list(LANGUAGES.keys()),
value=DEFAULT_LANGUAGE,
type="value",
interactive=True
)
# Input / Output
with gr.Row() as r2:
# Input
with gr.Column(scale=2, min_width=500) as c2_1:
# YouTube Tab
with gr.Tab("YouTube", id=0):
text_input0 = gr.Textbox(label="URL")
yt_options = gr.CheckboxGroup(["Save Audio", "Use Cache"],
value=get_yt_default_options(),
show_label=False)
with gr.Row() as yt_preview:
yt_thumbnail = gr.Image(
label="Thumbnail", interactive=False)
yt_preview_data = gr.Markdown(interactive=False)
yt_audio = gr.Audio(visible=False, type="filepath")
with gr.Row():
reset_button0 = gr.Button("Clear", elem_id="clear-button0")
text_button0 = gr.Button(
"Submit", elem_id="submit-button0")
# Video Tab
with gr.Tab("Video", id=1):
video_file_input1 = gr.Video(
label="Video", source="upload", type="filepath")
with gr.Row():
reset_button1 = gr.Button("Clear", elem_id="clear-button1")
text_button1 = gr.Button(
"Submit", elem_id="submit-button1")
# Audio Tab
with gr.Tab("Audio", id=2):
audio_file_input2 = gr.Audio(
label="Audio", source="upload", type="filepath")
with gr.Row():
reset_button2 = gr.Button("Clear", elem_id="clear-button2")
text_button2 = gr.Button(
"Submit", elem_id="submit-button2")
# Microphone Recording Tab
with gr.Tab("Microphone", id=3):
audio_file_input3 = gr.Audio(
label="Audio Recording", source="microphone", type="filepath")
save_recording_button3 = gr.Button("Save Recording")
save_recording_out3 = gr.Markdown()
with gr.Row():
reset_button3 = gr.Button("Clear", elem_id="clear-button3")
text_button3 = gr.Button(
"Submit", elem_id="submit-button3")
# Output
with gr.Column(scale=3, min_width=500, elem_id="output") as c2_2:
with gr.Tab("Text", elem_id="output0"):
text_output = gr.Textbox(show_label=False)
with gr.Tab("Table", elem_id="output1"):
text_markdown_output = gr.Markdown(
label="Table",
value="| Start | End | Text |\n|----|----|----|\n|0|0||")
with gr.Tab("Table (interactive)", elem_id="output2"):
text_table_output = gr.DataFrame(
headers=["Start", "End", "Text"],
datatype=["number", "number", "str"],
value=None)
with gr.Tab("JSON", elem_id="output3"):
text_json_output3 = gr.JSON(label="JSON")
with gr.Tab("JSON (raw)", elem_id="output4"):
text_json_output4 = gr.JSON(label="JSON")
with gr.Tab("Downloads", elem_id="output5"):
files_output5 = gr.Files(value=None)
# Whisper Model Options
model_language.change(
fn=change_model_options,
inputs=[model_language, model],
outputs=[model])
model_language.change(
fn=change_language_options,
inputs=[model_language],
outputs=[language])
# YouTube Tab
text_input0.change(fn=set_yt_preview, inputs=[text_input0], outputs=[
yt_thumbnail, yt_preview_data])
text_button0.click(
fn=youtube_to_subtitles,
inputs=[text_input0, model_language, model, language, yt_options],
outputs=[text_output, text_markdown_output, text_table_output, text_json_output3, text_json_output4, files_output5, yt_audio])
reset_button0.click(
fn=reset_value, inputs=[], outputs=[text_input0])
reset_button0.click(
fn=reset_value, inputs=[], outputs=[yt_thumbnail])
reset_button0.click(
fn=reset_value, inputs=[], outputs=[yt_preview_data])
reset_button0.click(
fn=reset_value_not_visible, inputs=[], outputs=[yt_audio])
# Video Tab
text_button1.click(
fn=video_to_subtitles,
inputs=[video_file_input1, model_language, model, language],
outputs=[text_output, text_markdown_output, text_table_output, text_json_output3, text_json_output4, files_output5])
reset_button1.click(
fn=reset_value, inputs=[], outputs=[video_file_input1])
# Audio Tab
text_button2.click(
fn=mp3_to_subtitles,
inputs=[audio_file_input2, model_language, model, language],
outputs=[text_output, text_markdown_output, text_table_output, text_json_output3, text_json_output4, files_output5])
reset_button2.click(
fn=reset_value, inputs=[], outputs=[audio_file_input2])
# Recording Tab
save_recording_button3.click(
fn=save_recording,
inputs=[audio_file_input3],
outputs=[save_recording_out3]
)
text_button3.click(
fn=mp3_to_subtitles,
inputs=[audio_file_input3, model_language,
model, language],
outputs=[text_output, text_markdown_output, text_table_output, text_json_output3, text_json_output4, files_output5])
reset_button3.click(
fn=reset_value, inputs=[], outputs=[audio_file_input3])
reset_button3.click(
fn=reset_value, inputs=[], outputs=[save_recording_out3]
)
page.launch()