Update app.py
Browse files5 unmodified lines678910910131112131417151617181 unmodified line2021222526232425282627302829303115 unmodified lines4748495253505152535457585556574 unmodified lines62636469656667686 unmodified lines757677827879808133 unmodified lines1151161171221181191201215 unmodified linesimport torchfrom deep_translator import GoogleTranslatorimport imageio_ffmpeg
# ZeroGPU Support အတွက် spaces library ကို import လုပ်ခြင်းimport spaces
# Server Logging ပြုလုပ်ခြင်း# Server Logging setuplogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
# 1. FFmpeg Executable Path စစ်ဆေးခြင်း# 1. FFmpeg Binary Path Configurationtry: FFMPEG_EXE = imageio_ffmpeg.get_ffmpeg_exe() logger.info(f"FFmpeg binary path: {FFMPEG_EXE}")1 unmodified line logger.error(f"Error locating FFmpeg: {e}") FFMPEG_EXE = "ffmpeg"
# 2. Whisper Model ကို Global အနေဖြင့် Load လုပ်ထားခြင်း# ZeroGPU ပေါ်တွင် CPU ဖြင့် အစောပိုင်း load လုပ်ထားပြီး GPU function ထဲရောက်မှ CUDA သို့ ပြောင်းပါမည်# 2. Whisper Model Loading for ZeroGPU# ZeroGPU standard အရ module level တွင် "cuda" သို့မဟုတ် PyTorch default ဖြင့် load လုပ်ပေးရပါမည်print("Loading Whisper Model...")whisper_model = whisper.load_model("small")whisper_model = whisper.load_model("small").to("cuda" if torch.cuda.is_available() else "cpu")
# 3. Translation Engine Setup (Auto Detect -> Target: Myanmar)# 3. Translation Engine Setup (Auto-Detect -> Target: Myanmar)translator = GoogleTranslator(source='auto', target='my')
def translate_to_myanmar(text: str) -> str:15 unmodified lines millis = 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 ကို အသုံးချနိုင်မည်ဖြစ်သည်# 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...") # ZeroGPU ထဲရောက်ပါက Model ကို CUDA (GPU) သို့ ရွှေ့ပေးသည် whisper_model.to("cuda") return whisper_model.transcribe(video_path, task="transcribe")
def process_video(video_path: str):4 unmodified lines try: logger.info(f"Processing video: {video_path}") # Step A: GPU ပေါ်တွင် Audio Detect & Transcribe ပြုလုပ်ခြင်း # 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 lines end_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) # 🎥 AI Animation Video Myanmar Subtitle Generator (ZeroGPU Optimized) Video Upload တင်လိုက်သည်နှင့် တရုတ်အသံထွက်ကို Auto Detect လုပ်ပေးပြီး မူရင်း တရုတ်စာတန်း၏ အပေါ် ကပ်လျက်တွင် မြန်မာစာတန်းထိုးပေးပါမည်။ """ )app.py+11-15