Commit Β·
fc7755f
1
Parent(s): beae951
Fix file handling and FFmpeg conversion
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from utils import process_video
|
| 3 |
|
| 4 |
# Define supported languages
|
| 5 |
language_map = {
|
|
@@ -16,33 +16,85 @@ language_map = {
|
|
| 16 |
"Japanese": "Helsinki-NLP/opus-mt-en-jap"
|
| 17 |
}
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Define Gradio Interface
|
| 27 |
-
with gr.Blocks() as demo:
|
| 28 |
-
gr.Markdown("# AI-Powered Video Subtitling")
|
| 29 |
-
gr.Markdown("Upload a video and select a language to generate subtitles.")
|
| 30 |
|
| 31 |
with gr.Row():
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
output_srt = gr.File(label="Download Subtitles")
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
generate_button.click(
|
| 43 |
generate_subtitles,
|
| 44 |
inputs=[video_input, language_dropdown],
|
| 45 |
-
outputs=output_srt
|
| 46 |
)
|
| 47 |
|
| 48 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from utils import process_video # Ensure this points to the updated utils.py
|
| 3 |
|
| 4 |
# Define supported languages
|
| 5 |
language_map = {
|
|
|
|
| 16 |
"Japanese": "Helsinki-NLP/opus-mt-en-jap"
|
| 17 |
}
|
| 18 |
|
| 19 |
+
# Custom CSS for dark mode and animations
|
| 20 |
+
css = """
|
| 21 |
+
body {
|
| 22 |
+
background-color: #1a1a1a;
|
| 23 |
+
color: #e0e0e0;
|
| 24 |
+
font-family: 'Arial', sans-serif;
|
| 25 |
+
}
|
| 26 |
+
.gradio-container {
|
| 27 |
+
max-width: 1200px;
|
| 28 |
+
margin: 0 auto;
|
| 29 |
+
padding: 20px;
|
| 30 |
+
border-radius: 10px;
|
| 31 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 32 |
+
}
|
| 33 |
+
.file-preview {
|
| 34 |
+
border: 2px dashed #6c757d;
|
| 35 |
+
padding: 20px;
|
| 36 |
+
border-radius: 10px;
|
| 37 |
+
}
|
| 38 |
+
.progress-text {
|
| 39 |
+
font-size: 16px;
|
| 40 |
+
color: #28a745;
|
| 41 |
+
animation: blink 1s infinite;
|
| 42 |
+
}
|
| 43 |
+
@keyframes blink {
|
| 44 |
+
50% { opacity: 0.5; }
|
| 45 |
+
}
|
| 46 |
+
"""
|
| 47 |
|
| 48 |
# Define Gradio Interface
|
| 49 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
|
| 50 |
+
gr.Markdown("# π₯ AI-Powered Video Subtitling")
|
| 51 |
+
gr.Markdown("Upload a video (MP4/MKV/AVI) and select a language to generate subtitles.")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
+
with gr.Column(scale=2):
|
| 55 |
+
video_input = gr.File(
|
| 56 |
+
label="Upload Video File",
|
| 57 |
+
file_types=["mp4", "mkv", "avi"],
|
| 58 |
+
elem_classes=["file-preview"]
|
| 59 |
+
)
|
| 60 |
+
with gr.Column(scale=1):
|
| 61 |
+
language_dropdown = gr.Dropdown(
|
| 62 |
+
choices=list(language_map.keys()),
|
| 63 |
+
label="Select Subtitle Language",
|
| 64 |
+
value="English"
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
generate_button = gr.Button("Generate Subtitles π")
|
| 68 |
+
progress_text = gr.Textbox(
|
| 69 |
+
label="Progress",
|
| 70 |
+
interactive=False,
|
| 71 |
+
elem_classes=["progress-text"]
|
| 72 |
+
)
|
| 73 |
output_srt = gr.File(label="Download Subtitles")
|
| 74 |
|
| 75 |
+
def generate_subtitles(video_file, language):
|
| 76 |
+
try:
|
| 77 |
+
# Validate file type
|
| 78 |
+
if not video_file.name.lower().endswith(('.mp4', '.mkv', '.avi')):
|
| 79 |
+
return None, "β Invalid file type. Please upload an MP4, MKV, or AVI file."
|
| 80 |
+
|
| 81 |
+
# Update progress
|
| 82 |
+
progress = "π Processing video..."
|
| 83 |
+
yield None, progress # Initial progress update
|
| 84 |
+
|
| 85 |
+
# Process video
|
| 86 |
+
srt_path = process_video(video_file.name, language)
|
| 87 |
+
if srt_path:
|
| 88 |
+
yield gr.File(srt_path), "β
Subtitles generated successfully!"
|
| 89 |
+
else:
|
| 90 |
+
yield None, "β Error during processing. Check logs."
|
| 91 |
+
except Exception as e:
|
| 92 |
+
yield None, f"β Error: {str(e)}"
|
| 93 |
+
|
| 94 |
generate_button.click(
|
| 95 |
generate_subtitles,
|
| 96 |
inputs=[video_input, language_dropdown],
|
| 97 |
+
outputs=[output_srt, progress_text]
|
| 98 |
)
|
| 99 |
|
| 100 |
demo.launch()
|
utils.py
CHANGED
|
@@ -7,16 +7,18 @@ import subprocess
|
|
| 7 |
# Load Whisper model
|
| 8 |
model = whisper.load_model("base")
|
| 9 |
|
| 10 |
-
def process_video(video_path, language):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
# Convert video to MP4 using ffmpeg
|
| 16 |
-
print("Converting video to MP4...")
|
| 17 |
subprocess.run(
|
| 18 |
["ffmpeg", "-i", video_path, "-c:v", "libx264", "-preset", "fast", output_video_path],
|
| 19 |
-
check=True,
|
| 20 |
stdout=subprocess.PIPE,
|
| 21 |
stderr=subprocess.PIPE
|
| 22 |
)
|
|
@@ -48,6 +50,7 @@ def process_video(video_path, language): # Accept file path, not file object
|
|
| 48 |
if not model_name:
|
| 49 |
return f"Unsupported language: {language}"
|
| 50 |
|
|
|
|
| 51 |
print(f"Loading translation model: {model_name}")
|
| 52 |
if language == "Telugu":
|
| 53 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
@@ -76,7 +79,7 @@ def process_video(video_path, language): # Accept file path, not file object
|
|
| 76 |
end = f"{segment['end']:.3f}".replace(".", ",")
|
| 77 |
text = segment["text"].strip()
|
| 78 |
f.write(f"{i}\n00:00:{start} --> 00:00:{end}\n{text}\n\n")
|
| 79 |
-
print("SRT file
|
| 80 |
return srt_path
|
| 81 |
|
| 82 |
except subprocess.CalledProcessError as e:
|
|
|
|
| 7 |
# Load Whisper model
|
| 8 |
model = whisper.load_model("base")
|
| 9 |
|
| 10 |
+
def process_video(video_path, language):
|
| 11 |
+
# Create a temporary directory
|
| 12 |
+
temp_dir = tempfile.gettempdir()
|
| 13 |
+
output_video_path = os.path.join(temp_dir, "converted_video.mp4")
|
| 14 |
+
srt_path = os.path.join(temp_dir, "subtitles.srt")
|
| 15 |
|
| 16 |
try:
|
| 17 |
# Convert video to MP4 using ffmpeg
|
| 18 |
+
print(f"Converting video: {video_path} to MP4...")
|
| 19 |
subprocess.run(
|
| 20 |
["ffmpeg", "-i", video_path, "-c:v", "libx264", "-preset", "fast", output_video_path],
|
| 21 |
+
check=True,
|
| 22 |
stdout=subprocess.PIPE,
|
| 23 |
stderr=subprocess.PIPE
|
| 24 |
)
|
|
|
|
| 50 |
if not model_name:
|
| 51 |
return f"Unsupported language: {language}"
|
| 52 |
|
| 53 |
+
# Load translation model
|
| 54 |
print(f"Loading translation model: {model_name}")
|
| 55 |
if language == "Telugu":
|
| 56 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 79 |
end = f"{segment['end']:.3f}".replace(".", ",")
|
| 80 |
text = segment["text"].strip()
|
| 81 |
f.write(f"{i}\n00:00:{start} --> 00:00:{end}\n{text}\n\n")
|
| 82 |
+
print(f"SRT file saved to {srt_path}")
|
| 83 |
return srt_path
|
| 84 |
|
| 85 |
except subprocess.CalledProcessError as e:
|