Update app.py
Browse files17 unmodified lines181920212223243 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989917 unmodified linesif not video_path:return None, "⚠️ ဗီဒီယို ဖိုင် တင်ပါ။"if not groq_api_key or not groq_api_key.strip():return None, "❌ Groq API Key ထည့်သွင်းပါ။ (Free at console.groq.com)"try:progress(0.1, desc="Whisper Model ကို Load လုပ်နေသည်...")3 unmodified linesapi_key=groq_api_key.strip())progress(0.3, desc="Whisper ASR ဖြင့် အသံမှ Text & Timestamps များ ထုတ်ယူနေသည်...")result = whisper_model.transcribe(video_path, task="transcribe")segments = result.get("segments", [])if not segments:return None, "❌ ဗီဒီယိုထဲတွင် ပြောဆိုသံ မတွေ့ရှိပါ။"progress(0.6, desc="စကားပြောစတိုင် မြန်မာဘာသာသို့ LLM ဖြင့် ပြန်ဆိုနေသည်...")srt_lines = []for index, segment in enumerate(segments, start=1):start_time = format_timestamp(segment['start'])end_time = format_timestamp(segment['end'])original_text = segment['text'].strip()# LLM Prompt for Spoken Myanmar Translationsystem_prompt = ("You are a professional movie subtitler. Translate the following text into natural, casual spoken Myanmar (Burmese) language. ""Do NOT use formal/literary Burmese endings like '၏', 'သည်', 'ဖြစ်သတည်း'. ""Use conversational endings like 'တယ်', 'မယ်', 'ပါ', 'တယ်နော်', 'လား'. ""Return ONLY the translated Myanmar text without quotes or explanations.")try:response = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": original_text}],temperature=0.3)translated_text = response.choices[0].message.content.strip()except Exception as translation_err:translated_text = original_textsrt_lines.append(f"{index}")srt_lines.append(f"{start_time} --> {end_time}")srt_lines.append(f"{translated_text}\n")srt_content = "\n".join(srt_lines)srt_filename = "output_spoken_myanmar.srt"with open(srt_filename, "w", encoding="utf-8-sig") as f:f.write(srt_content)progress(1.0, desc="ပြီးမြောက်ပါပြီ!")return srt_filename, "✅ စကားပြောစတိုင် မြန်မာ Subtitle (.srt) ထုတ်ယူပြီးပါပြီ!"except Exception as e:return None, f"❌ Error: {str(e)}"with gr.Blocks(title="Spoken Myanmar Video Subtitle Generator") as app:gr.Markdown("# 🎬 Casual Spoken Myanmar Subtitle (.srt) Generator")gr.Markdown("Whisper + LLM သုံး၍ စာပေသုံး ၏/သည် မဟုတ်ဘဲ **သဘာဝကျသော စကားပြောစတိုင်** မြန်မာ Subtitle ထုတ်ပေးသည့် စနစ်")with gr.Row():groq_key_input = gr.Textbox(label="Groq API Key (Free)",placeholder="gsk_... (console.groq.com တွင် Free ယူပါ)",type="password")with gr.Row():with gr.Column():video_input = gr.Video(label="Short Video Upload တင်ရန်")generate_btn = gr.Button("🚀 Subtitle (.srt) ထုတ်မည်", variant="primary")with gr.Column():17 unmodified lines181920212223243 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111217 unmodified linesif not video_path:return None, "⚠️ ဗီဒီယို ဖိုင် တင်ပါ။"if not groq_api_key or not groq_api_key.strip():return None, "❌ Groq API Key ထည့်သွင်းပါ။ (console.groq.com တွင် Free ယူပါ)"try:progress(0.1, desc="Whisper Model ကို Load လုပ်နေသည်...")3 unmodified linesapi_key=groq_api_key.strip())progress(0.3, desc="Whisper ASR ဖြင့် အသံမှ စာသားနှင့် Timestamps များ ထုတ်ယူနေသည်...")result = whisper_model.transcribe(video_path, task="transcribe")segments = result.get("segments", [])if not segments:return None, "❌ ဗီဒီယိုထဲတွင် ပြောဆိုသံ မတွေ့ရှိပါ။"progress(0.6, desc="စာသားများကို ဘာသာပြန်ရန် ပြင်ဆင်နေသည်...")original_sentences = [f"{i+1}. {seg['text'].strip()}" for i, seg in enumerate(segments)]combined_text = "\n".join(original_sentences)system_prompt = ("You are a professional subtitle translator into natural spoken Myanmar (Burmese) language.\n""Translate each numbered line into natural conversational Burmese.\n""RULES:\n""1. Absolutely DO NOT use formal/literary endings like '၏', 'သည်', 'ဖြစ်သတည်း', 'ပါသနည်း'.\n""2. Use natural spoken Burmese style (e.g., 'တယ်', 'မယ်', 'ပါ', 'လဲ', 'နော်', 'ဘူး').\n""3. Keep the exact same numbering line by line.\n""4. Return ONLY the numbered translated lines in Myanmar script. Do not output English or explanation.")progress(0.7, desc="LLM ဖြင့် မြန်မာစကားပြောစတိုင် သို့ ဘာသာပြန်ဆိုနေသည်...")response = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": combined_text}],temperature=0.3)translated_block = response.choices[0].message.content.strip()translated_lines = {}for line in translated_block.split("\n"):line = line.strip()match = re.match(r"^(\d+)[\.\)]\s*(.*)", line)if match:idx = int(match.group(1))text = match.group(2).strip()translated_lines[idx] = textprogress(0.9, desc="CapCut SRT File ထုတ်လုပ်နေသည်...")srt_blocks = []for index, segment in enumerate(segments, start=1):start_time = format_timestamp(segment['start'])end_time = format_timestamp(segment['end'])myanmar_text = translated_lines.get(index, segment['text'].strip())srt_blocks.append(f"{index}\n{start_time} --> {end_time}\n{myanmar_text}\n")srt_content = "\n".join(srt_blocks)srt_filename = "output_spoken_myanmar.srt"with open(srt_filename, "w", encoding="utf-8-sig") as f:f.write(srt_content)progress(1.0, desc="ပြီးမြောက်ပါပြီ!")return srt_filename, "✅ မြန်မာ စကားပြောစတိုင် Subtitle (.srt) အောင်မြင်စွာ ထုတ်ပြီးပါပြီ!"except Exception as e:return None, f"❌ Error: {str(e)}"with gr.Blocks(title="Spoken Myanmar Subtitle SRT Generator") as app:gr.Markdown("# 🎬 Spoken Myanmar Subtitle (.srt) Generator")gr.Markdown("Whisper + Groq LLM သုံး၍ စာပေသုံး '၏/သည်' လုံးဝမပါဘဲ **သဘာဝကျသော မြန်မာစကားပြောစတိုင်** Subtitle ထုတ်ပေးသည့် စနစ်")with gr.Row():groq_key_input = gr.Textbox(label="Groq API Key (Free)",placeholder="gsk_... (console.groq.com တွင် အခမဲ့ယူပါ)",type="password")with gr.Row():with gr.Column():video_input = gr.Video(label="Video Upload တင်ရန်")generate_btn = gr.Button("🚀 Subtitle (.srt) ထုတ်မည်", variant="primary")with gr.Column():
app.py+51-38