killian31
commited on
Commit
·
7aa414b
1
Parent(s):
cc9d80e
feat: progress gradio
Browse files
app.py
CHANGED
|
@@ -5,13 +5,18 @@ from moviepy.editor import AudioFileClip, ColorClip, concatenate_videoclips
|
|
| 5 |
from moviepy.video.VideoClip import TextClip
|
| 6 |
|
| 7 |
|
| 8 |
-
def generate_video(audio_path, language, lag):
|
| 9 |
# Transcribe audio
|
|
|
|
| 10 |
result = model.transcribe(audio_path, language=language)
|
|
|
|
| 11 |
|
| 12 |
# Prepare video clips from transcription segments
|
| 13 |
clips = []
|
|
|
|
|
|
|
| 14 |
for segment in result["segments"]:
|
|
|
|
| 15 |
text_clip = (
|
| 16 |
TextClip(
|
| 17 |
segment["text"],
|
|
@@ -25,29 +30,30 @@ def generate_video(audio_path, language, lag):
|
|
| 25 |
.set_start(segment["start"])
|
| 26 |
)
|
| 27 |
clips.append(text_clip)
|
|
|
|
| 28 |
|
| 29 |
if lag > 0:
|
| 30 |
clips.insert(0, ColorClip((1280, 720), color=(0, 0, 0)).set_duration(lag))
|
|
|
|
| 31 |
|
| 32 |
# Concatenate clips and set audio
|
|
|
|
| 33 |
video = concatenate_videoclips(clips, method="compose")
|
| 34 |
|
| 35 |
# Add audio to the video
|
|
|
|
| 36 |
video = video.set_audio(AudioFileClip(audio_path))
|
| 37 |
|
| 38 |
# Export video to a buffer
|
|
|
|
| 39 |
output_path = "./transcribed_video.mp4"
|
| 40 |
video.write_videofile(output_path, fps=6, codec="libx264", audio_codec="aac")
|
| 41 |
-
|
| 42 |
return output_path
|
| 43 |
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
DEVICE = (
|
| 47 |
-
"cuda"
|
| 48 |
-
if torch.cuda.is_available()
|
| 49 |
-
else "cpu"
|
| 50 |
-
)
|
| 51 |
model = whisper.load_model("base", device=DEVICE)
|
| 52 |
|
| 53 |
# Gradio interface
|
|
|
|
| 5 |
from moviepy.video.VideoClip import TextClip
|
| 6 |
|
| 7 |
|
| 8 |
+
def generate_video(audio_path, language, lag, progress=gr.Progress(track_tqdm=True)):
|
| 9 |
# Transcribe audio
|
| 10 |
+
progress(0.0, "Transcribing audio...")
|
| 11 |
result = model.transcribe(audio_path, language=language)
|
| 12 |
+
progress(0.30, "Audio transcribed!")
|
| 13 |
|
| 14 |
# Prepare video clips from transcription segments
|
| 15 |
clips = []
|
| 16 |
+
total_segments = len(result["segments"])
|
| 17 |
+
running_progress = 0.0
|
| 18 |
for segment in result["segments"]:
|
| 19 |
+
running_progress += 0.4 / total_segments
|
| 20 |
text_clip = (
|
| 21 |
TextClip(
|
| 22 |
segment["text"],
|
|
|
|
| 30 |
.set_start(segment["start"])
|
| 31 |
)
|
| 32 |
clips.append(text_clip)
|
| 33 |
+
progress(min(0.3 + running_progress, 0.7), "Generating video frames...")
|
| 34 |
|
| 35 |
if lag > 0:
|
| 36 |
clips.insert(0, ColorClip((1280, 720), color=(0, 0, 0)).set_duration(lag))
|
| 37 |
+
progress(0.7, "Video frames generated!")
|
| 38 |
|
| 39 |
# Concatenate clips and set audio
|
| 40 |
+
progress(0.75, "Concatenating video clips...")
|
| 41 |
video = concatenate_videoclips(clips, method="compose")
|
| 42 |
|
| 43 |
# Add audio to the video
|
| 44 |
+
progress(0.85, "Adding audio to video...")
|
| 45 |
video = video.set_audio(AudioFileClip(audio_path))
|
| 46 |
|
| 47 |
# Export video to a buffer
|
| 48 |
+
progress(0.90, "Exporting video...")
|
| 49 |
output_path = "./transcribed_video.mp4"
|
| 50 |
video.write_videofile(output_path, fps=6, codec="libx264", audio_codec="aac")
|
| 51 |
+
progress(1.0, "Video exported!")
|
| 52 |
return output_path
|
| 53 |
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
model = whisper.load_model("base", device=DEVICE)
|
| 58 |
|
| 59 |
# Gradio interface
|