Update app.py
Browse files123456789101112131415161718192021222324252627287 unmodified lines3637383940414243444546471 unmodified line49505152535455565758596061626364652 unmodified lines68697071727374757677787980import osimport gradio as grimport whisperfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLMimport ffmpegimport subprocess# 1. Load Whisper Model (for language detection and transcribing)print("Loading Whisper Model...")whisper_model = whisper.load_model("small") # 'tiny', 'base', 'small', 'medium' စသဖြင့် သုံးနိုင်သည်# 2. Load Translation Model (English/Other to Myanmar - MarianMT)print("Loading Translation Model...")model_name = "Helsinki-NLP/opus-mt-en-my"tokenizer = AutoTokenizer.from_pretrained(model_name)translate_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)def translate_to_myanmar(text):if not text.strip():return ""try:inputs = tokenizer(text, return_tensors="pt", padding=True)translated = translate_model.generate(**inputs)return tokenizer.decode(translated[0], skip_special_tokens=True)except Exception as e:return textdef format_timestamp(seconds):7 unmodified linesif video_path is None:return None, "Video တင်ပေးပါဦး။"# Step A: Detect Language & Transcriberesult = whisper_model.transcribe(video_path)detected_language = result.get("language", "unknown")segments = result.get("segments", [])# Step B: Create SRT Subtitle File with Myanmar Translationsrt_path = "output_sub.srt"with open(srt_path, "w", encoding="utf-8") as f:for idx, segment in enumerate(segments, start=1):1 unmodified lineend_time = format_timestamp(segment['end'])original_text = segment['text']# Translate text to Myanmarmyanmar_text = translate_to_myanmar(original_text)f.write(f"{idx}\n")f.write(f"{start_time} --> {end_time}\n")f.write(f"{myanmar_text}\n\n")# Step C: Burn Subtitles into Video using FFmpegoutput_video_path = "output_video.mp4"# FFmpeg command to hardcode subtitles at the bottom centerffmpeg_cmd = ["ffmpeg", "-y", "-i", video_path,"-vf", f"subtitles={srt_path}:force_style='FontSize=16,PrimaryColour=&H00FFFF&,Alignment=2'",2 unmodified linessubprocess.run(ffmpeg_cmd, check=True)status_msg = f"Detected Language: {detected_language.upper()}"return output_video_path, status_msg# Gradio Interface Buildwith gr.Blocks(title="Video Myanmar Subtitle Generator") as demo:gr.Markdown("### 🎥 Video Auto Language Detect & Myanmar Subtitle Generator")gr.Markdown("Video တင်လိုက်သည်နှင့် Whisper AI က ဘာသာစကားကို auto တိုင်းတာပြီး မြန်မာလို စာတန်းထိုးပေးပါမည်။")with gr.Row():with gr.Column():1234567891011121314151617181920212223247 unmodified lines3233343536373839404142431 unmodified line45464748495051525354555657582 unmodified lines6162636465666768697071import osimport subprocessimport gradio as grimport whisperfrom deep_translator import GoogleTranslator# 1. Load Whisper Modelprint("Loading Whisper Model...")whisper_model = whisper.load_model("small")# 2. Translation Function (Google Translate)translator = GoogleTranslator(source='auto', target='my')def translate_to_myanmar(text):if not text.strip():return ""try:# Auto-detect language and translate to Myanmarreturn translator.translate(text)except Exception as e:print(f"Translation Error: {e}")return textdef format_timestamp(seconds):7 unmodified linesif video_path is None:return None, "Video တင်ပေးပါဦး။"# Step A: Transcribe & Detect Languageresult = whisper_model.transcribe(video_path)detected_language = result.get("language", "unknown")segments = result.get("segments", [])# Step B: Write Subtitle Filesrt_path = "output_sub.srt"with open(srt_path, "w", encoding="utf-8") as f:for idx, segment in enumerate(segments, start=1):1 unmodified lineend_time = format_timestamp(segment['end'])original_text = segment['text']myanmar_text = translate_to_myanmar(original_text)f.write(f"{idx}\n")f.write(f"{start_time} --> {end_time}\n")f.write(f"{myanmar_text}\n\n")# Step C: Burn Subtitles with FFmpegoutput_video_path = "output_video.mp4"ffmpeg_cmd = ["ffmpeg", "-y", "-i", video_path,"-vf", f"subtitles={srt_path}:force_style='FontSize=16,PrimaryColour=&H00FFFF&,Alignment=2'",2 unmodified linessubprocess.run(ffmpeg_cmd, check=True)return output_video_path, f"Detected Language: {detected_language.upper()}"# Gradio Interfacewith gr.Blocks(title="Video Myanmar Subtitle Generator") as demo:gr.Markdown("### 🎥 Video Auto Language Detect & Myanmar Subtitle Generator")with gr.Row():with gr.Column():
app.py+14-23