ChiBenevisamPas's picture
Update app.py
442428c verified
raw
history blame
1.46 kB
import gradio as gr
import whisper
import os
# Load the Whisper model
model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
def write_srt(transcription, output_file):
with open(output_file, "w") as f:
for i, segment in enumerate(transcription['segments']):
start = segment['start']
end = segment['end']
text = segment['text']
# Format timestamps for SRT
start_time = whisper.utils.format_timestamp(start)
end_time = whisper.utils.format_timestamp(end)
print(f"Writing subtitle {i + 1}: {text.strip()} ({start_time} --> {end_time})") # Debug print
f.write(f"{i + 1}\n")
f.write(f"{start_time} --> {end_time}\n")
f.write(f"{text.strip()}\n\n")
def transcribe_video(video_file):
# Transcribe the video to generate subtitles
result = model.transcribe(video_file)
# Save the transcription to an .srt file
srt_file = "generated_subtitles.srt"
# Write the transcription as subtitles
write_srt(result, srt_file)
return srt_file
# Gradio interface
iface = gr.Interface(
fn=transcribe_video,
inputs=gr.File(label="Upload Video"),
outputs=gr.File(label="Download Subtitles"),
title="Video Subtitle Generator",
description="Upload a video file to generate subtitles using Whisper."
)
if __name__ == "__main__":
iface.launch()