Heidel Medina. commited on
Commit
3d86161
·
0 Parent(s):

Version 1 of ssui-app

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. main.py +300 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .gradio/
main.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import mimetypes
3
+ import os
4
+ os.environ['KMP_DUPLICATE_LIB_OK']='True'
5
+ import argparse
6
+ import stable_whisper
7
+ from stable_whisper.text_output import result_to_any, sec2srt
8
+ import tempfile
9
+ import re
10
+ import textwrap
11
+
12
+ def process_media(model_size, source_lang, upload, model_type):
13
+ if upload is None:
14
+ return None, None, None, None, "No file uploaded."
15
+
16
+ temp_path = upload.name
17
+
18
+ if model_type == "faster whisper":
19
+ model = stable_whisper.load_faster_whisper(model_size, device="cuda")
20
+ else:
21
+ model = stable_whisper.load_model(model_size, device="cuda")
22
+
23
+ try:
24
+ result = model.transcribe(temp_path, language=source_lang, vad=False, regroup=False)
25
+ except Exception as e:
26
+ return None, None, None, None, f"Transcription failed: {e}"
27
+
28
+ for i, segment in enumerate(result):
29
+ if i+1 == len(result):
30
+ break
31
+ next_start = result[i+1].start
32
+ if next_start - segment.end <= 0.100:
33
+ segment.end = next_start
34
+
35
+ srt_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt", mode="w", encoding="utf-8")
36
+ result.to_srt_vtt(srt_file.name, word_level=False)
37
+ srt_file.close()
38
+ srt_file_path = srt_file.name
39
+
40
+ # Transcript as plain text
41
+ transcript_txt = result.to_txt()
42
+
43
+ mime, _ = mimetypes.guess_type(temp_path)
44
+ audio_out = temp_path if mime and mime.startswith("audio") else None
45
+ video_out = temp_path if mime and mime.startswith("video") else None
46
+
47
+ return audio_out, video_out, transcript_txt, srt_file_path, None
48
+
49
+ WHISPER_LANGUAGES = [
50
+ ("Afrikaans", "af"),
51
+ ("Albanian", "sq"),
52
+ ("Amharic", "am"),
53
+ ("Arabic", "ar"),
54
+ ("Armenian", "hy"),
55
+ ("Assamese", "as"),
56
+ ("Azerbaijani", "az"),
57
+ ("Bashkir", "ba"),
58
+ ("Basque", "eu"),
59
+ ("Belarusian", "be"),
60
+ ("Bengali", "bn"),
61
+ ("Bosnian", "bs"),
62
+ ("Breton", "br"),
63
+ ("Bulgarian", "bg"),
64
+ ("Burmese", "my"),
65
+ ("Catalan", "ca"),
66
+ ("Chinese", "zh"),
67
+ ("Croatian", "hr"),
68
+ ("Czech", "cs"),
69
+ ("Danish", "da"),
70
+ ("Dutch", "nl"),
71
+ ("English", "en"),
72
+ ("Estonian", "et"),
73
+ ("Faroese", "fo"),
74
+ ("Finnish", "fi"),
75
+ ("French", "fr"),
76
+ ("Galician", "gl"),
77
+ ("Georgian", "ka"),
78
+ ("German", "de"),
79
+ ("Greek", "el"),
80
+ ("Gujarati", "gu"),
81
+ ("Haitian Creole", "ht"),
82
+ ("Hausa", "ha"),
83
+ ("Hebrew", "he"),
84
+ ("Hindi", "hi"),
85
+ ("Hungarian", "hu"),
86
+ ("Icelandic", "is"),
87
+ ("Indonesian", "id"),
88
+ ("Italian", "it"),
89
+ ("Japanese", "ja"),
90
+ ("Javanese", "jv"),
91
+ ("Kannada", "kn"),
92
+ ("Kazakh", "kk"),
93
+ ("Khmer", "km"),
94
+ ("Korean", "ko"),
95
+ ("Lao", "lo"),
96
+ ("Latin", "la"),
97
+ ("Latvian", "lv"),
98
+ ("Lingala", "ln"),
99
+ ("Lithuanian", "lt"),
100
+ ("Luxembourgish", "lb"),
101
+ ("Macedonian", "mk"),
102
+ ("Malagasy", "mg"),
103
+ ("Malay", "ms"),
104
+ ("Malayalam", "ml"),
105
+ ("Maltese", "mt"),
106
+ ("Maori", "mi"),
107
+ ("Marathi", "mr"),
108
+ ("Mongolian", "mn"),
109
+ ("Nepali", "ne"),
110
+ ("Norwegian", "no"),
111
+ ("Nyanja", "ny"),
112
+ ("Occitan", "oc"),
113
+ ("Pashto", "ps"),
114
+ ("Persian", "fa"),
115
+ ("Polish", "pl"),
116
+ ("Portuguese", "pt"),
117
+ ("Punjabi", "pa"),
118
+ ("Romanian", "ro"),
119
+ ("Russian", "ru"),
120
+ ("Sanskrit", "sa"),
121
+ ("Serbian", "sr"),
122
+ ("Shona", "sn"),
123
+ ("Sindhi", "sd"),
124
+ ("Sinhala", "si"),
125
+ ("Slovak", "sk"),
126
+ ("Slovenian", "sl"),
127
+ ("Somali", "so"),
128
+ ("Spanish", "es"),
129
+ ("Sundanese", "su"),
130
+ ("Swahili", "sw"),
131
+ ("Swedish", "sv"),
132
+ ("Tagalog", "tl"),
133
+ ("Tajik", "tg"),
134
+ ("Tamil", "ta"),
135
+ ("Tatar", "tt"),
136
+ ("Telugu", "te"),
137
+ ("Thai", "th"),
138
+ ("Turkish", "tr"),
139
+ ("Turkmen", "tk"),
140
+ ("Ukrainian", "uk"),
141
+ ("Urdu", "ur"),
142
+ ("Uzbek", "uz"),
143
+ ("Vietnamese", "vi"),
144
+ ("Welsh", "cy"),
145
+ ("Yiddish", "yi"),
146
+ ("Yoruba", "yo"),
147
+ ]
148
+
149
+ with gr.Blocks() as interface:
150
+ gr.HTML(
151
+ """
152
+ <style>.html-container.svelte-phx28p.padding { padding: 0 !important; }</style>
153
+ <div class='custom-container'>
154
+ <h1 style='text-align: left;'>Speech Solutions</h1>
155
+ """
156
+ )
157
+ gr.Markdown(
158
+ """
159
+ This is a simple Gradio UI app that combines AI-powered speech and language processing technologies. This app supports the following features:
160
+
161
+ - Speech-to-text (WhisperAI)
162
+ - Language translation (GPT-4) (In progress)
163
+
164
+ <b>NOTE: This app is currently in the process of applying other AI-solutions for other use cases.</b>
165
+ """
166
+ )
167
+
168
+ with gr.Tabs():
169
+ with gr.TabItem("Speech to Text"):
170
+ gr.HTML("<h2 style='text-align: left;'>OpenAI/Whisper + stable-ts</h1>")
171
+ gr.Markdown(
172
+ """
173
+ Open Ai's <b>Whisper</b> is a versatile speech recognition model trained on diverse audio for tasks like multilingual transcription, translation, and language ID. With the help of <b>stable-ts</b>, it provides accurate word-level timestamps in chronological order without extra processing.
174
+ """
175
+ )
176
+ #General Settings
177
+ with gr.Row():
178
+ #Media Input
179
+ with gr.Column(scale=1):
180
+ file_input = gr.File(label="Upload Audio or Video", file_types=["audio", "video"])
181
+ #Settings
182
+ with gr.Column(scale=1):
183
+ with gr.Group():
184
+ source_lang = gr.Dropdown(
185
+ choices=WHISPER_LANGUAGES,
186
+ label="Source Language",
187
+ value="en", # default to English
188
+ interactive=True
189
+ )
190
+ model_type = gr.Dropdown(
191
+ choices=["faster whisper", "whisper"],
192
+ label="Model Type",
193
+ value="faster whisper",
194
+ interactive=True
195
+ )
196
+ model_size = gr.Dropdown(
197
+ choices=[
198
+ ("Large v3 Turbo", "large-v3-turbo"),
199
+ ("Large v3", "large-v3"),
200
+ ("Large v2", "large-v2"),
201
+ ("Large", "large"),
202
+ ("Medium", "medium"),
203
+ ("Small", "small"),
204
+ ("Base", "base"),
205
+ ("Tiny", "tiny")
206
+ ],
207
+ label="Model Size",
208
+ value="large-v2",
209
+ interactive=True
210
+ )
211
+ #Advanced Settings
212
+ with gr.Accordion("Advanced Settings", open=False):
213
+ gr.Markdown(
214
+ """
215
+ These settings allow you to customize the segmentation of the audio or video file. Adjust these parameters to control how the segments are created based on characters, words, and lines.
216
+
217
+ <b><i>Note: The values currently set are the default values. You can adjust them to your needs, but be aware that changing these values may affect the segmentation of the audio or video file.</i></b>
218
+ """
219
+ )
220
+ with gr.Row():
221
+ with gr.Column():
222
+ max_chars = gr.Number(
223
+ label="Max Chars",
224
+ info="Maximum characters allowed in segment",
225
+ value=86,
226
+ precision=0,
227
+ interactive=True
228
+ )
229
+ max_words = gr.Number(
230
+ label="Max Words",
231
+ info="Maximum words allowed in segment",
232
+ value=30,
233
+ precision=0,
234
+ interactive=True
235
+ )
236
+ max_lines_per_segment = gr.Number(
237
+ label="Max Lines Per Segment",
238
+ info="Max lines allowed per subtitle segment",
239
+ value=3,
240
+ precision=0,
241
+ interactive=True
242
+ )
243
+ with gr.Column():
244
+ extend_in = gr.Number(
245
+ label="Extend In",
246
+ info="Extend the start of all segments by this value (in seconds)",
247
+ value=0,
248
+ precision=2,
249
+
250
+ )
251
+ extend_out = gr.Number(
252
+ label="Extend Out",
253
+ info="Extend the end of all segments by this value (in seconds)",
254
+ value=0.5,
255
+ precision=2,
256
+ interactive=True
257
+ )
258
+ collapse_gaps = gr.Number(
259
+ label="Collapse Gaps",
260
+ info="Collapse gaps between segments under a certain duration",
261
+ value=0.3,
262
+ precision=2,
263
+ interactive=True
264
+ )
265
+
266
+ with gr.Column():
267
+ line_penalty = gr.Number(
268
+ label="Longest Line Character",
269
+ info="Penalty for each additional line (used to decide when to split segment into several lines)",
270
+ value=22.01,
271
+ precision=2,
272
+ interactive=True
273
+ )
274
+ longest_line_char_penalty = gr.Number(
275
+ label="Longest Line Character",
276
+ info="Penalty for each character of the longest segment line (used to decide when to split segment into several lines)",
277
+ value=1,
278
+ precision=2,
279
+ interactive=True
280
+ )
281
+ submit_btn = gr.Button("PROCESS", elem_id="orange-process-btn")
282
+ with gr.Row():
283
+ with gr.Column():
284
+ transcript_output = gr.Textbox(label="Transcript", lines=8, interactive=False)
285
+ srt_output = gr.File(label="Download SRT", interactive=False)
286
+
287
+ with gr.Column():
288
+ video_output = gr.Video(label="Video Output")
289
+ audio_output = gr.Audio(label="Audio Output")
290
+
291
+ submit_btn.click(
292
+ fn=process_media,
293
+ inputs=[model_size, source_lang, file_input, model_type],
294
+ outputs=[audio_output, video_output, transcript_output, srt_output]
295
+ )
296
+
297
+ with gr.TabItem("..."):
298
+ pass
299
+
300
+ interface.launch(share=True)