Spaces:
Runtime error
Runtime error
SRT Edit
Browse files
app.py
CHANGED
@@ -1,40 +1,44 @@
|
|
1 |
-
import whisper
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
# Load the Whisper model
|
5 |
model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def transcribe_video(video_file):
|
8 |
# Transcribe the video to generate subtitles
|
9 |
-
result = model.transcribe(video_file
|
10 |
-
|
11 |
-
#
|
12 |
srt_file = "generated_subtitles.srt"
|
13 |
-
|
14 |
-
def write_srt(transcription, output_file):
|
15 |
-
with open(output_file, "w") as f:
|
16 |
-
for i, segment in enumerate(transcription['segments']):
|
17 |
-
start = segment['start']
|
18 |
-
end = segment['end']
|
19 |
-
text = segment['text']
|
20 |
-
# Format timestamps for SRT
|
21 |
-
start_time = whisper.utils.format_timestamp(start)
|
22 |
-
end_time = whisper.utils.format_timestamp(end)
|
23 |
-
f.write(f"{i + 1}\n")
|
24 |
-
f.write(f"{start_time} --> {end_time}\n")
|
25 |
-
f.write(f"{text.strip()}\n\n")
|
26 |
-
|
27 |
# Write the transcription as subtitles
|
28 |
write_srt(result, srt_file)
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
# Create a Gradio interface
|
33 |
iface = gr.Interface(
|
34 |
fn=transcribe_video,
|
35 |
-
inputs=gr.File(label="Upload Video"),
|
36 |
-
outputs=gr.File(label="Download Subtitles")
|
|
|
|
|
37 |
)
|
38 |
|
39 |
-
|
40 |
-
iface.launch(
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
import os
|
4 |
|
5 |
# Load the Whisper model
|
6 |
model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
|
7 |
|
8 |
+
def write_srt(transcription, output_file):
|
9 |
+
with open(output_file, "w") as f:
|
10 |
+
for i, segment in enumerate(transcription['segments']):
|
11 |
+
start = segment['start']
|
12 |
+
end = segment['end']
|
13 |
+
text = segment['text']
|
14 |
+
# Format timestamps for SRT
|
15 |
+
start_time = whisper.utils.format_timestamp(start)
|
16 |
+
end_time = whisper.utils.format_timestamp(end)
|
17 |
+
print(f"Writing subtitle {i + 1}: {text.strip()} ({start_time} --> {end_time})") # Debug print
|
18 |
+
f.write(f"{i + 1}\n")
|
19 |
+
f.write(f"{start_time} --> {end_time}\n")
|
20 |
+
f.write(f"{text.strip()}\n\n")
|
21 |
+
|
22 |
def transcribe_video(video_file):
|
23 |
# Transcribe the video to generate subtitles
|
24 |
+
result = model.transcribe(video_file)
|
25 |
+
|
26 |
+
# Save the transcription to an .srt file
|
27 |
srt_file = "generated_subtitles.srt"
|
28 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Write the transcription as subtitles
|
30 |
write_srt(result, srt_file)
|
31 |
+
|
32 |
+
return srt_file
|
33 |
|
34 |
+
# Gradio interface
|
|
|
|
|
35 |
iface = gr.Interface(
|
36 |
fn=transcribe_video,
|
37 |
+
inputs=gr.File(label="Upload Video"),
|
38 |
+
outputs=gr.File(label="Download Subtitles"),
|
39 |
+
title="Video Subtitle Generator",
|
40 |
+
description="Upload a video file to generate subtitles using Whisper."
|
41 |
)
|
42 |
|
43 |
+
if __name__ == "__main__":
|
44 |
+
iface.launch()
|