Update app.py
Browse files10 unmodified lines111213141516175 unmodified lines232425262728291 unmodified line313233343536373839404111 unmodified lines5354555657585960616263646566676869707116 unmodified lines8889909192939495969798991001011021031041051061071088 unmodified lines11711811910 unmodified lineslogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)# 1. FFmpeg Binary Path ကို ရယူခြင်းtry:FFMPEG_EXE = imageio_ffmpeg.get_ffmpeg_exe()logger.info(f"FFmpeg binary path: {FFMPEG_EXE}")5 unmodified linesdevice = "cuda" if torch.cuda.is_available() else "cpu"logger.info(f"Using device: {device}")# Whisper Model Load လုပ်ခြင်း ('small' မော်ဒယ်သည် တရုတ်စကားပြော Detect လုပ်ရာတွင် တိကျမှုမြင့်မားသည်)try:whisper_model = whisper.load_model("small", device=device)logger.info("Whisper model loaded successfully.")1 unmodified linelogger.error(f"Failed to load Whisper model: {e}")whisper_model = None# 3. Translation Engine Setup (Auto-Detect Source -> Target: Myanmar)translator = GoogleTranslator(source='auto', target='my')def translate_to_myanmar(text: str) -> str:"""တရုတ် သို့မဟုတ် အင်္ဂလိပ် စာသားများကို မြန်မာလို ဘာသာပြန်ပေးသည့် Function"""if not text or not text.strip():return ""try:11 unmodified linesmillis = int((seconds - int(seconds)) * 1000)return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"# ZeroGPU Compatibility Checktry:import spaces@spaces.GPUdef run_whisper(video_path):# task="transcribe" ဖြင့် မူရင်းအသံကို မူရင်းစာသားအတိုင်း ထုတ်ယူသည်return whisper_model.transcribe(video_path, task="transcribe")except Exception:def run_whisper(video_path):return whisper_model.transcribe(video_path, task="transcribe")def process_video(video_path: str, position_choice: str):"""Main Function: Audio Detection -> Translation -> SRT Subtitle -> Video Encoding"""if video_path is None:return None, None, "❌ ကျေးဇူးပြု၍ Video Upload တင်ပေးပါဦး။"16 unmodified linesend_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: Subtitle Style & Positioning Configuration# BorderStyle=3 (Text Background Box ဖုံးပေးခြင်း)# BackColour=&H80000000& (Solid/Semi-transparent Black Box)# MarginV=25 (မူရင်း အောက်ခြေ စာတန်းနေရာတွင် ကွက်တိ ကျစေရန်)if position_choice == "Original Sub နေရာတွင် အကွက်မည်းဖုံး၍ မြန်မာစာတန်းထိုးမည်":style_setting = "Alignment=2,MarginV=25,BorderStyle=3,BackColour=&H80000000&,PrimaryColour=&H00FFFF&,FontSize=18"else:# ဗီဒီယို၏ ထိပ်ဆုံး (Top) တွင် ထိုးလိုပါကstyle_setting = "Alignment=6,MarginV=20,BorderStyle=1,PrimaryColour=&H00FFFF&,OutlineColour=&H000000&,FontSize=18"output_video_path = "output_processed_video.mp4"8 unmodified lineslogger.info("Running FFmpeg encoding...")subprocess.run(ffmpeg_cmd, check=True)10 unmodified lines111213141516175 unmodified lines232425262728291 unmodified line313233343536373839404111 unmodified lines53545556575859606162636465666768697016 unmodified lines878889909192939495969798991001011021031048 unmodified lines11311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215310 unmodified lineslogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)# 1. FFmpeg Binary Path Configurationtry:FFMPEG_EXE = imageio_ffmpeg.get_ffmpeg_exe()logger.info(f"FFmpeg binary path: {FFMPEG_EXE}")5 unmodified linesdevice = "cuda" if torch.cuda.is_available() else "cpu"logger.info(f"Using device: {device}")# Whisper Model Load လုပ်ခြင်း ('small' မော်ဒယ်သည် တရုတ်စကားပြော Detect လုပ်ရာတွင် အလွန်တိကျပါသည်)try:whisper_model = whisper.load_model("small", device=device)logger.info("Whisper model loaded successfully.")1 unmodified linelogger.error(f"Failed to load Whisper model: {e}")whisper_model = None# 3. Translation Engine Setup (Auto Detect -> Target: Myanmar)translator = GoogleTranslator(source='auto', target='my')def translate_to_myanmar(text: str) -> str:"""တရုတ် သို့မဟုတ် အခြားစကားပြော စာသားများကို မြန်မာလို ဘာသာပြန်ပေးသည့် Function"""if not text or not text.strip():return ""try:11 unmodified linesmillis = int((seconds - int(seconds)) * 1000)return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"# ZeroGPU Compatibility Integrationtry:import spaces@spaces.GPUdef run_whisper(video_path):return whisper_model.transcribe(video_path, task="transcribe")except Exception:def run_whisper(video_path):return whisper_model.transcribe(video_path, task="transcribe")def process_video(video_path: str):"""Main Function: Audio Speech-to-Text -> Translate to Myanmar -> Stack Above Chinese Subtitle"""if video_path is None:return None, None, "❌ ကျေးဇူးပြု၍ Video Upload တင်ပေးပါဦး။"16 unmodified linesend_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: Subtitle Style & Positioning# Alignment=2: ဗီဒီယို၏ အောက်ခြေအလယ်# MarginV=55: အောက်ခြေရှိ Original တရုတ်စာတန်း၏ 'အပေါ်နား ကပ်လျက်' တွင် မြန်မာစာတန်း ကွက်တိ ကျစေရန် နေရာချခြင်း# BorderStyle=1, OutlineColour=&H000000&: စ��လုံးဘောင် အနက်ရောင်ပါသော မြန်မာစာတန်း ပေါ်စေခြင်းstyle_setting = "Alignment=2,MarginV=55,FontSize=18,PrimaryColour=&H00FFFF&,OutlineColour=&H000000&,BorderStyle=1"output_video_path = "output_processed_video.mp4"8 unmodified lineslogger.info("Running FFmpeg encoding...")subprocess.run(ffmpeg_cmd, check=True)status_msg = f"✅ မြန်မာစာတန်းထိုးပြီးပါပြီ! (Detected Language: {detected_lang})"return output_video_path, srt_path, status_msgexcept subprocess.CalledProcessError as e:logger.error(f"FFmpeg Error: {e}")return None, None, "❌ FFmpeg ဖြင့် ဗီဒီယို ပြုလုပ်စဉ် Error တက်သွားပါသည်။"except Exception as e:logger.error(f"General Processing Error: {e}")return None, None, f"❌ Error: {str(e)}"# 4. Gradio UI Layoutwith gr.Blocks(title="Auto Video Myanmar Subtitle Generator", theme=gr.themes.Soft()) as demo:gr.Markdown("""# 🎥 AI Animation Video Myanmar Subtitle GeneratorVideo Upload တင်လိုက်သည်နှင့် တရုတ်အသံထွက်ကို Auto Detect လုပ်ပေးပြီး မူရင်း တရုတ်စာတန်း၏ အပေါ် ကပ်လျက်တွင် မြန်မာစာတန်းထိုးပေးပါမည်။""")with gr.Row():with gr.Column(scale=1):video_input = gr.Video(label="Upload Original Video")submit_btn = gr.Button("မြန်မာစာတန်းထိုးမည် (Generate)", variant="primary")with gr.Column(scale=1):video_output = gr.Video(label="Result Video (Download လုပ်နိုင်သည်)")srt_output = gr.File(label="Download Subtitle File (.srt)")status_output = gr.Textbox(label="Status Logging", interactive=False)submit_btn.click(fn=process_video,inputs=[video_input],outputs=[video_output, srt_output, status_output])if __name__ == "__main__":demo.queue().launch()
app.py+52-18