Spaces:
Runtime error
Runtime error
changes
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- __pycache__/transcribe.cpython-312.pyc +0 -0
- __pycache__/utils.cpython-312.pyc +0 -0
- app.py +47 -28
- transcribe.py +27 -2
- utils.py +18 -2
__pycache__/app.cpython-312.pyc
ADDED
Binary file (4.55 kB). View file
|
|
__pycache__/transcribe.cpython-312.pyc
ADDED
Binary file (1.61 kB). View file
|
|
__pycache__/utils.cpython-312.pyc
ADDED
Binary file (3.8 kB). View file
|
|
app.py
CHANGED
@@ -3,33 +3,52 @@ import utils
|
|
3 |
import transcribe
|
4 |
|
5 |
with gr.Blocks(theme="base") as demo:
|
6 |
-
gr.Markdown("<center><h1> π
|
7 |
-
with gr.
|
8 |
-
with gr.
|
9 |
-
with gr.
|
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 |
demo.launch()
|
|
|
3 |
import transcribe
|
4 |
|
5 |
with gr.Blocks(theme="base") as demo:
|
6 |
+
gr.Markdown("<center><h1> π Transcription Delight </h1></center>")
|
7 |
+
with gr.Tabs(selected="result") as tabs:
|
8 |
+
with gr.Tab("Input"):
|
9 |
+
with gr.Row():
|
10 |
+
with gr.Column():
|
11 |
+
source = gr.Radio(label="Source type", choices=[("Audio", "audio"), ("Video", "video"), ("YouTube URL", "youtube")], value="audio")
|
12 |
+
@gr.render(inputs=source)
|
13 |
+
def show_source(s):
|
14 |
+
if s == "audio":
|
15 |
+
source_component = gr.Audio(type="filepath")
|
16 |
+
elif s == "video":
|
17 |
+
source_component = gr.Video()
|
18 |
+
else:
|
19 |
+
source_component = gr.Textbox(placeholder="https://www.youtube.com/watch?v=44vi31hehw4")
|
20 |
+
preview = gr.HTML(label="Video preview")
|
21 |
+
source_component.change(utils.convert_to_embed_url, source_component, preview)
|
22 |
+
transcribe_btn.click(
|
23 |
+
lambda : gr.Tabs(selected="result"),
|
24 |
+
None,
|
25 |
+
tabs
|
26 |
+
).then(
|
27 |
+
utils.generate_audio,
|
28 |
+
[source, source_component],
|
29 |
+
[download_audio],
|
30 |
+
show_progress="minimal"
|
31 |
+
).then(
|
32 |
+
transcribe.transcribe,
|
33 |
+
[download_audio],
|
34 |
+
[preliminary_transcript],
|
35 |
+
show_progress="hidden"
|
36 |
+
)
|
37 |
+
|
38 |
+
with gr.Column():
|
39 |
+
gr.Dropdown(label="Languages", choices=["(Autodetect)", "English"], value="(Autodetect)")
|
40 |
+
gr.CheckboxGroup(label="Cleanup Transcript with LLM", choices=["Remove typos", "Separate into paragraphs"])
|
41 |
+
gr.Checkbox(label="Diarize Speakers (coming soon)", interactive=False)
|
42 |
+
|
43 |
+
transcribe_btn = gr.Button("Transcribe audio β¨", variant="primary")
|
44 |
+
source.change(utils.transcribe_button, source, transcribe_btn)
|
45 |
+
|
46 |
+
with gr.Tab("Result", id="result"):
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
download_audio = gr.DownloadButton("Downloading Audio File (please wait...)", variant="primary", interactive=False, size="sm")
|
50 |
+
preliminary_transcript = gr.Textbox(info="Preliminary transcript", lines=10, show_copy_button=True, show_label=False, interactive=False)
|
51 |
+
with gr.Column():
|
52 |
+
gr.Markdown("*Final transcript will appear here*")
|
53 |
|
54 |
demo.launch()
|
transcribe.py
CHANGED
@@ -1,2 +1,27 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydub import AudioSegment
|
2 |
+
from pydub.utils import make_chunks
|
3 |
+
import os
|
4 |
+
import whisper
|
5 |
+
|
6 |
+
model = whisper.load_model("base")
|
7 |
+
|
8 |
+
def transcribe(audio_path):
|
9 |
+
transcripts = []
|
10 |
+
for transcript in transcribe_audio_in_chunks(audio_path, chunk_length_ms=30000):
|
11 |
+
transcripts.append(transcript)
|
12 |
+
yield " ".join(transcripts)
|
13 |
+
|
14 |
+
def transcribe_segment(segment, segment_number):
|
15 |
+
temp_filename = f"temp_segment_{segment_number}.wav"
|
16 |
+
segment.export(temp_filename, format="wav")
|
17 |
+
result = model.transcribe(temp_filename)
|
18 |
+
os.remove(temp_filename)
|
19 |
+
return result["text"]
|
20 |
+
|
21 |
+
def transcribe_audio_in_chunks(audio_path, chunk_length_ms):
|
22 |
+
audio = AudioSegment.from_file(audio_path)
|
23 |
+
chunks = make_chunks(audio, chunk_length_ms)
|
24 |
+
for i, chunk in enumerate(chunks):
|
25 |
+
transcription = transcribe_segment(chunk, i)
|
26 |
+
yield transcription
|
27 |
+
|
utils.py
CHANGED
@@ -20,11 +20,11 @@ def download_audio_from_youtube(video_url):
|
|
20 |
yt = YouTube(video_url)
|
21 |
audio_stream = yt.streams.filter(only_audio=True).first()
|
22 |
downloaded_file = audio_stream.download(".")
|
23 |
-
base,
|
24 |
mp3_file = base + '.mp3'
|
25 |
AudioSegment.from_file(downloaded_file).export(mp3_file, format='mp3')
|
26 |
os.remove(downloaded_file)
|
27 |
-
return
|
28 |
except Exception as e:
|
29 |
gr.Error(f"An error occurred: {e}")
|
30 |
|
@@ -41,3 +41,19 @@ def convert_video_to_audio(input_file):
|
|
41 |
except ffmpeg.Error as e:
|
42 |
gr.Error(f"An error occurred: {e}")
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
yt = YouTube(video_url)
|
21 |
audio_stream = yt.streams.filter(only_audio=True).first()
|
22 |
downloaded_file = audio_stream.download(".")
|
23 |
+
base, _ = os.path.splitext(downloaded_file)
|
24 |
mp3_file = base + '.mp3'
|
25 |
AudioSegment.from_file(downloaded_file).export(mp3_file, format='mp3')
|
26 |
os.remove(downloaded_file)
|
27 |
+
return mp3_file
|
28 |
except Exception as e:
|
29 |
gr.Error(f"An error occurred: {e}")
|
30 |
|
|
|
41 |
except ffmpeg.Error as e:
|
42 |
gr.Error(f"An error occurred: {e}")
|
43 |
|
44 |
+
def transcribe_button(source):
|
45 |
+
if source == "audio":
|
46 |
+
return gr.Button("Transcribe audio β¨")
|
47 |
+
else:
|
48 |
+
return gr.Button("Transcribe video β¨")
|
49 |
+
|
50 |
+
def generate_audio(source, source_file):
|
51 |
+
if source == "audio":
|
52 |
+
audio_file = source_file
|
53 |
+
elif source == "video":
|
54 |
+
gr.Info("Converting video to audio...")
|
55 |
+
audio_file = convert_video_to_audio(source_file)
|
56 |
+
else:
|
57 |
+
gr.Info("Downloading audio from YouTube...")
|
58 |
+
audio_file = download_audio_from_youtube(source_file)
|
59 |
+
return gr.DownloadButton("Downloading Audio File", value=audio_file, interactive=True)
|