Update app.py
Browse files5 unmodified lines678910111213141516171819201 unmodified line22232425262728293031323315 unmodified lines495051525354555657585960614 unmodified lines666768697071726 unmodified lines7980818283848533 unmodified lines1191201211221231241255 unmodified linesimport torchfrom deep_translator import GoogleTranslatorimport imageio_ffmpeg# ZeroGPU Support အတွက် spaces library ကို import လုပ်ခြင်းimport spaces# Server Logging ပြုလုပ်ခြင်းlogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)# 1. FFmpeg Executable Path စစ်ဆေးခြင်းtry:FFMPEG_EXE = imageio_ffmpeg.get_ffmpeg_exe()logger.info(f"FFmpeg binary path: {FFMPEG_EXE}")1 unmodified linelogger.error(f"Error locating FFmpeg: {e}")FFMPEG_EXE = "ffmpeg"# 2. Whisper Model ကို Global အနေဖြင့် Load လုပ်ထားခြင်း# ZeroGPU ပေါ်တွင် CPU ဖြင့် အစောပိုင်း load လုပ်ထားပြီး GPU function ထဲရောက်မှ CUDA သို့ ပြောင်းပါမည်print("Loading Whisper Model...")whisper_model = whisper.load_model("small")# 3. Translation Engine Setup (Auto Detect -> Target: Myanmar)translator = GoogleTranslator(source='auto', target='my')def translate_to_myanmar(text: str) -> str:15 unmodified linesmillis = int((seconds - int(seconds)) * 1000)return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"# 4. Hugging Face ZeroGPU တိုက်ရိုက်ခေါ်ယူသည့် Function# @spaces.GPU decorator ပါမှသာ Hugging Face GPU ကို အသုံးချနိုင်မည်ဖြစ်သည်@spaces.GPUdef transcribe_video_on_gpu(video_path):logger.info("Running Whisper transcription on ZeroGPU...")# ZeroGPU ထဲရောက်ပါက Model ကို CUDA (GPU) သို့ ရွှေ့ပေးသည်whisper_model.to("cuda")return whisper_model.transcribe(video_path, task="transcribe")def process_video(video_path: str):4 unmodified linestry:logger.info(f"Processing video: {video_path}")# Step A: GPU ပေါ်တွင် Audio Detect & Transcribe ပြုလုပ်ခြင်းresult = transcribe_video_on_gpu(video_path)detected_lang = result.get("language", "Unknown").upper()segments = result.get("segments", [])6 unmodified linesend_time = format_timestamp(segment['end'])original_text = segment['text']# တရုတ်စာသားကို မြန်မာလို ဘာသာပြန်ခြင်းmyanmar_text = translate_to_myanmar(original_text)f.write(f"{idx}\n")33 unmodified lineswith gr.Blocks(title="Auto Video Myanmar Subtitle Generator", theme=gr.themes.Soft()) as demo:gr.Markdown("""# 🎥 AI Animation Video Myanmar Subtitle Generator (GPU Accelerated)Video Upload တင်လိုက်သည်နှင့် တရုတ်အသံထွက်ကို Auto Detect လုပ်ပေးပြီး မူရင်း တရုတ်စာတန်း၏ အပေါ် ကပ်လျက်တွင် မြန်မာစာတန်းထိုးပေးပါမည်။""")5 unmodified lines67891011121314151617181 unmodified line20212223242526272829303115 unmodified lines47484950515253545556574 unmodified lines626364656667686 unmodified lines7576777879808133 unmodified lines1151161171181191201215 unmodified linesimport torchfrom deep_translator import GoogleTranslatorimport imageio_ffmpegimport spaces# Server Logging setuplogging.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}")1 unmodified linelogger.error(f"Error locating FFmpeg: {e}")FFMPEG_EXE = "ffmpeg"# 2. Whisper Model Loading for ZeroGPU# ZeroGPU standard အရ module level တွင် "cuda" သို့မဟုတ် PyTorch default ဖြင့် load လုပ်ပေးရပါမည်print("Loading Whisper Model...")whisper_model = whisper.load_model("small").to("cuda" if torch.cuda.is_available() else "cpu")# 3. Translation Engine Setup (Auto-Detect -> Target: Myanmar)translator = GoogleTranslator(source='auto', target='my')def translate_to_myanmar(text: str) -> str:15 unmodified linesmillis = int((seconds - int(seconds)) * 1000)return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"# 4. Hugging Face ZeroGPU Dedicated Function# @spaces.GPU တင်ထားသော function ထဲတွင် runtime model moving (.to("cuda")) မလုပ်ရပါ။ တိုက်ရိုက် run ရပါမည်။@spaces.GPUdef transcribe_video_on_gpu(video_path):logger.info("Running Whisper transcription on ZeroGPU...")return whisper_model.transcribe(video_path, task="transcribe")def process_video(video_path: str):4 unmodified linestry:logger.info(f"Processing video: {video_path}")# Step A: GPU ပေါ်တွင် Audio Transcribe ပြုလုပ်ခြင်းresult = transcribe_video_on_gpu(video_path)detected_lang = result.get("language", "Unknown").upper()segments = result.get("segments", [])6 unmodified linesend_time = format_timestamp(segment['end'])original_text = segment['text']# မြန်မာလို ဘာသာပြန်ခြင်းmyanmar_text = translate_to_myanmar(original_text)f.write(f"{idx}\n")33 unmodified lineswith gr.Blocks(title="Auto Video Myanmar Subtitle Generator", theme=gr.themes.Soft()) as demo:gr.Markdown("""# 🎥 AI Animation Video Myanmar Subtitle Generator (ZeroGPU Optimized)Video Upload တင်လိုက်သည်နှင့် တရုတ်အသံထွက်ကို Auto Detect လုပ်ပေးပြီး မူရင်း တရုတ်စာတန်း၏ အပေါ် ကပ်လျက်တွင် မြန်မာစာတန်းထိုးပေးပါမည်။""")
app.py+11-15