Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -11,10 +11,10 @@ from google import genai
|
|
| 11 |
# ==========================================
|
| 12 |
# C O N F I G U R A T I O N
|
| 13 |
# ==========================================
|
| 14 |
-
#
|
| 15 |
TG_BOT_TOKEN = "8874546727:AAGmbXosMwLcmqBKijFixrBQzAOkxxroQu4"
|
| 16 |
|
| 17 |
-
#
|
| 18 |
GEMINI_SERVERS = {
|
| 19 |
"1": "Ab8RN6JWlQI2BLk0ec7iQAlj1EykGJTsRksNpfQuOC-Ir3czLA",
|
| 20 |
"2": "AQ.Ab8RN6L3lkQVRqM8Z-9eISP0ZFlk_4U5aY1dVw9b3sVHCIf3Cg",
|
|
@@ -62,7 +62,6 @@ async def has_audio_stream(filepath: str) -> bool:
|
|
| 62 |
return len(stdout.decode().strip()) > 0
|
| 63 |
|
| 64 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 65 |
-
# Server ရွေးချယ်ရန် Button များဖန်တီးခြင်း
|
| 66 |
keyboard = [
|
| 67 |
[InlineKeyboardButton("🖥 Server 1", callback_data='server_1'), InlineKeyboardButton("🖥 Server 2", callback_data='server_2')],
|
| 68 |
[InlineKeyboardButton("🖥 Server 3", callback_data='server_3'), InlineKeyboardButton("🖥 Server 4", callback_data='server_4')],
|
|
@@ -73,7 +72,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 73 |
|
| 74 |
await update.message.reply_text(
|
| 75 |
"မင်္ဂလာပါ။ 🇲🇲\n"
|
| 76 |
-
"Video ဖိုင်တစ်ခုကို ပို့ပေးပါ။ Video ထဲက စကားပြောတွေကို မြန်မာလို ဘာသာပြန်ပြီး အသံထွက်ရော စာတန်းထိုးပါ ပြန်ပေါင်းထည့်ပေးပါမယ်။\n\n"
|
| 77 |
"API Limit ပြည့်သွားပါက အောက်ပါ Server များကို ပြောင်းလဲရွေးချယ်နိုင်ပါသည်။",
|
| 78 |
reply_markup=reply_markup
|
| 79 |
)
|
|
@@ -82,7 +81,6 @@ async def button_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 82 |
query = update.callback_query
|
| 83 |
await query.answer()
|
| 84 |
|
| 85 |
-
# ရွေးချယ်လိုက်သော server နံပါတ်ကို သိမ်းဆည်းခြင်း
|
| 86 |
server_id = query.data.split('_')[1]
|
| 87 |
context.user_data['selected_server'] = server_id
|
| 88 |
|
|
@@ -97,7 +95,12 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 97 |
if msg.document and not msg.document.mime_type.startswith('video/'):
|
| 98 |
return
|
| 99 |
|
| 100 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
selected_server_id = context.user_data.get('selected_server', '8')
|
| 102 |
current_api_key = GEMINI_SERVERS.get(selected_server_id)
|
| 103 |
|
|
@@ -105,14 +108,12 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 105 |
await msg.reply_text("❌ မှားယွင်းသော Server ဖြစ်နေပါသည်။")
|
| 106 |
return
|
| 107 |
|
| 108 |
-
# ရွေးချယ်ထားသော Key ဖြင့် Gemini Client ကို တည်ဆောက်ခြင်း
|
| 109 |
client = genai.Client(api_key=current_api_key)
|
| 110 |
|
| 111 |
-
status_msg = await msg.reply_text(f"📥 Video ကို ဒေါင်းလုဒ်ဆွဲနေပါတယ်... (
|
| 112 |
|
| 113 |
try:
|
| 114 |
-
|
| 115 |
-
tg_file = await context.bot.get_file(file_obj.file_id)
|
| 116 |
|
| 117 |
with tempfile.TemporaryDirectory() as work_dir:
|
| 118 |
video_path = os.path.join(work_dir, "input.mp4")
|
|
@@ -180,7 +181,7 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 180 |
all_subs.append({'start_ms': start_ms, 'end_ms': end_ms, 'text': clean_text})
|
| 181 |
|
| 182 |
if c_idx < len(chunk_files) - 1:
|
| 183 |
-
await asyncio.sleep(8)
|
| 184 |
|
| 185 |
if not all_subs:
|
| 186 |
await status_msg.edit_text("❌ ဘာသာပြန်ရန် အသံရှာမတွေ့ပါ။ (သို့မဟုတ်) API Limit ပြည့်နေပါသည်။ အခြား Server သို့ပြောင်းကြည့်ပါ။")
|
|
@@ -219,7 +220,7 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 219 |
if os.path.exists(tts_path):
|
| 220 |
chunk_paths.append({'path': tts_path, 'start_ms': sub['start_ms'], 'end_ms': sub['end_ms']})
|
| 221 |
|
| 222 |
-
# --- 5. Custom Font Download
|
| 223 |
font_path = os.path.join(work_dir, 'NotoSansMyanmar-Regular.ttf')
|
| 224 |
try:
|
| 225 |
urllib.request.urlretrieve('https://github.com/google/fonts/raw/main/ofl/notosansmyanmar/NotoSansMyanmar-Regular.ttf', font_path)
|
|
@@ -244,7 +245,6 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 244 |
args = ['ffmpeg', '-y', '-i', video_path]
|
| 245 |
filter_graph = ""
|
| 246 |
|
| 247 |
-
# Audio Mixing Logic
|
| 248 |
if use_audio_mix:
|
| 249 |
if keep_original:
|
| 250 |
filter_graph += "[0:a]volume=1.0[orig_a];"
|
|
@@ -269,11 +269,10 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 269 |
if in_duration <= 0: in_duration = 1.0
|
| 270 |
target_duration = max(0.1, (c['end_ms'] - safe_start_ms) / 1000)
|
| 271 |
ratio = in_duration / target_duration
|
| 272 |
-
ratio = max(0.85, min(ratio, 1.6))
|
| 273 |
final_duration_ms = (in_duration / ratio) * 1000
|
| 274 |
last_end_ms = safe_start_ms + final_duration_ms
|
| 275 |
|
| 276 |
-
# Calculate FFmpeg atempo chain
|
| 277 |
atempo_str = ""
|
| 278 |
r = ratio
|
| 279 |
while r > 2.0:
|
|
@@ -295,14 +294,12 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 295 |
filter_graph += f"{tts_amix_str}apad[ttsmix];"
|
| 296 |
|
| 297 |
if keep_original:
|
| 298 |
-
# Sidechain compression (Ducking) to lower original audio when TTS is speaking
|
| 299 |
filter_graph += f"[ttsmix]asplit=2[tts_sc][tts_out];"
|
| 300 |
filter_graph += f"[orig_a][tts_sc]sidechaincompress=threshold=0.04:ratio=6.0:attack=10:release=400[ducked_orig];"
|
| 301 |
filter_graph += f"[ducked_orig][tts_out]amix=inputs=2:duration=first:dropout_transition=0,volume=2[outa]"
|
| 302 |
else:
|
| 303 |
filter_graph += f"[orig_a][ttsmix]amix=inputs=2:duration=first:dropout_transition=0,volume=2[outa]"
|
| 304 |
|
| 305 |
-
# Hard Subtitle Burning
|
| 306 |
safe_srt = srt_path.replace('\\', '/').replace(":", "\\\\:")
|
| 307 |
safe_dir = work_dir.replace('\\', '/').replace(":", "\\\\:")
|
| 308 |
|
|
@@ -321,7 +318,6 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 321 |
|
| 322 |
args.append(out_video)
|
| 323 |
|
| 324 |
-
# Run FFmpeg
|
| 325 |
ffmpeg_env = os.environ.copy()
|
| 326 |
ffmpeg_env['FONTCONFIG_FILE'] = fonts_conf
|
| 327 |
|
|
@@ -343,22 +339,25 @@ async def process_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 343 |
except Exception as e:
|
| 344 |
import traceback
|
| 345 |
traceback.print_exc()
|
| 346 |
-
|
| 347 |
-
await status_msg.edit_text(f"❌ အမှားအယွင်းဖြစ်သွားပါသည်: {str(e)}\n\n💡 အကယ်၍ API Error ဖြစ်နေပါက /start ကို နှိပ်ပြီး အခြား Server သို့ ပြောင်းလဲအသုံးပြုကြည့်ပါ။")
|
| 348 |
|
| 349 |
def main():
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
return
|
| 353 |
-
|
| 354 |
-
application = Application.builder().token(TG_BOT_TOKEN).build()
|
| 355 |
|
| 356 |
application.add_handler(CommandHandler("start", start))
|
| 357 |
application.add_handler(CallbackQueryHandler(button_callback, pattern='^server_'))
|
| 358 |
application.add_handler(MessageHandler(filters.VIDEO | filters.Document.ALL, process_video))
|
| 359 |
|
| 360 |
-
print("
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
if __name__ == '__main__':
|
| 364 |
main()
|
|
|
|
| 11 |
# ==========================================
|
| 12 |
# C O N F I G U R A T I O N
|
| 13 |
# ==========================================
|
| 14 |
+
# Telegram Bot Token
|
| 15 |
TG_BOT_TOKEN = "8874546727:AAGmbXosMwLcmqBKijFixrBQzAOkxxroQu4"
|
| 16 |
|
| 17 |
+
# Gemini API Key Servers
|
| 18 |
GEMINI_SERVERS = {
|
| 19 |
"1": "Ab8RN6JWlQI2BLk0ec7iQAlj1EykGJTsRksNpfQuOC-Ir3czLA",
|
| 20 |
"2": "AQ.Ab8RN6L3lkQVRqM8Z-9eISP0ZFlk_4U5aY1dVw9b3sVHCIf3Cg",
|
|
|
|
| 62 |
return len(stdout.decode().strip()) > 0
|
| 63 |
|
| 64 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
|
|
| 65 |
keyboard = [
|
| 66 |
[InlineKeyboardButton("🖥 Server 1", callback_data='server_1'), InlineKeyboardButton("🖥 Server 2", callback_data='server_2')],
|
| 67 |
[InlineKeyboardButton("🖥 Server 3", callback_data='server_3'), InlineKeyboardButton("🖥 Server 4", callback_data='server_4')],
|
|
|
|
| 72 |
|
| 73 |
await update.message.reply_text(
|
| 74 |
"မင်္ဂလာပါ။ 🇲🇲\n"
|
| 75 |
+
"MyVideoBot မှ ကြိုဆိုပါတယ်။ Video ဖိုင်တစ်ခုကို ပို့ပေးပါ။ Video ထဲက စကားပြောတွေကို မြန်မာလို ဘာသာပြန်ပြီး အသံထွက်ရော စာတန်းထိုးပါ ပြန်ပေါင်းထည့်ပေးပါမယ်။\n\n"
|
| 76 |
"API Limit ပြည့်သွားပါက အောက်ပါ Server များကို ပြောင်းလဲရွေးချယ်နိုင်ပါသည်။",
|
| 77 |
reply_markup=reply_markup
|
| 78 |
)
|
|
|
|
| 81 |
query = update.callback_query
|
| 82 |
await query.answer()
|
| 83 |
|
|
|
|
| 84 |
server_id = query.data.split('_')[1]
|
| 85 |
context.user_data['selected_server'] = server_id
|
| 86 |
|
|
|
|
| 95 |
if msg.document and not msg.document.mime_type.startswith('video/'):
|
| 96 |
return
|
| 97 |
|
| 98 |
+
# Check File Size Limit (20MB)
|
| 99 |
+
file_size = file_obj.file_size
|
| 100 |
+
if file_size and file_size > 20 * 1024 * 1024:
|
| 101 |
+
await msg.reply_text("❌ ဖိုင်ဆိုဒ်ကြီးလွန်းနေပါသည်။ 20MB အောက် ဗီဒီယိုများကိုသာ ပို့ပေးပါ။")
|
| 102 |
+
return
|
| 103 |
+
|
| 104 |
selected_server_id = context.user_data.get('selected_server', '8')
|
| 105 |
current_api_key = GEMINI_SERVERS.get(selected_server_id)
|
| 106 |
|
|
|
|
| 108 |
await msg.reply_text("❌ မှားယွင်းသော Server ဖြစ်နေပါသည်။")
|
| 109 |
return
|
| 110 |
|
|
|
|
| 111 |
client = genai.Client(api_key=current_api_key)
|
| 112 |
|
| 113 |
+
status_msg = await msg.reply_text(f"📥 Video ကို ဒေါင်းလုဒ်ဆွဲနေပါတယ်... (Server - {selected_server_id})")
|
| 114 |
|
| 115 |
try:
|
| 116 |
+
tg_file = await context.bot.get_file(file_obj.file_id, read_timeout=120)
|
|
|
|
| 117 |
|
| 118 |
with tempfile.TemporaryDirectory() as work_dir:
|
| 119 |
video_path = os.path.join(work_dir, "input.mp4")
|
|
|
|
| 181 |
all_subs.append({'start_ms': start_ms, 'end_ms': end_ms, 'text': clean_text})
|
| 182 |
|
| 183 |
if c_idx < len(chunk_files) - 1:
|
| 184 |
+
await asyncio.sleep(8)
|
| 185 |
|
| 186 |
if not all_subs:
|
| 187 |
await status_msg.edit_text("❌ ဘာသာပြန်ရန် အသံရှာမတွေ့ပါ။ (သို့မဟုတ်) API Limit ပြည့်နေပါသည်။ အခြား Server သို့ပြောင်းကြည့်ပါ။")
|
|
|
|
| 220 |
if os.path.exists(tts_path):
|
| 221 |
chunk_paths.append({'path': tts_path, 'start_ms': sub['start_ms'], 'end_ms': sub['end_ms']})
|
| 222 |
|
| 223 |
+
# --- 5. Custom Font Download ---
|
| 224 |
font_path = os.path.join(work_dir, 'NotoSansMyanmar-Regular.ttf')
|
| 225 |
try:
|
| 226 |
urllib.request.urlretrieve('https://github.com/google/fonts/raw/main/ofl/notosansmyanmar/NotoSansMyanmar-Regular.ttf', font_path)
|
|
|
|
| 245 |
args = ['ffmpeg', '-y', '-i', video_path]
|
| 246 |
filter_graph = ""
|
| 247 |
|
|
|
|
| 248 |
if use_audio_mix:
|
| 249 |
if keep_original:
|
| 250 |
filter_graph += "[0:a]volume=1.0[orig_a];"
|
|
|
|
| 269 |
if in_duration <= 0: in_duration = 1.0
|
| 270 |
target_duration = max(0.1, (c['end_ms'] - safe_start_ms) / 1000)
|
| 271 |
ratio = in_duration / target_duration
|
| 272 |
+
ratio = max(0.85, min(ratio, 1.6))
|
| 273 |
final_duration_ms = (in_duration / ratio) * 1000
|
| 274 |
last_end_ms = safe_start_ms + final_duration_ms
|
| 275 |
|
|
|
|
| 276 |
atempo_str = ""
|
| 277 |
r = ratio
|
| 278 |
while r > 2.0:
|
|
|
|
| 294 |
filter_graph += f"{tts_amix_str}apad[ttsmix];"
|
| 295 |
|
| 296 |
if keep_original:
|
|
|
|
| 297 |
filter_graph += f"[ttsmix]asplit=2[tts_sc][tts_out];"
|
| 298 |
filter_graph += f"[orig_a][tts_sc]sidechaincompress=threshold=0.04:ratio=6.0:attack=10:release=400[ducked_orig];"
|
| 299 |
filter_graph += f"[ducked_orig][tts_out]amix=inputs=2:duration=first:dropout_transition=0,volume=2[outa]"
|
| 300 |
else:
|
| 301 |
filter_graph += f"[orig_a][ttsmix]amix=inputs=2:duration=first:dropout_transition=0,volume=2[outa]"
|
| 302 |
|
|
|
|
| 303 |
safe_srt = srt_path.replace('\\', '/').replace(":", "\\\\:")
|
| 304 |
safe_dir = work_dir.replace('\\', '/').replace(":", "\\\\:")
|
| 305 |
|
|
|
|
| 318 |
|
| 319 |
args.append(out_video)
|
| 320 |
|
|
|
|
| 321 |
ffmpeg_env = os.environ.copy()
|
| 322 |
ffmpeg_env['FONTCONFIG_FILE'] = fonts_conf
|
| 323 |
|
|
|
|
| 339 |
except Exception as e:
|
| 340 |
import traceback
|
| 341 |
traceback.print_exc()
|
| 342 |
+
await status_msg.edit_text(f"❌ အမှားအယွင်းဖြစ်သွားပါသည်: {str(e)}\n\n💡 API Error ဖြစ်ပါက /start ကိုနှိပ်၍ အခြား Server ပြောင်းပါ။")
|
|
|
|
| 343 |
|
| 344 |
def main():
|
| 345 |
+
# Webhook စနစ်ကို အသုံးပြု၍ Timeout Error ကို ဖြေရှင်းထားသည်
|
| 346 |
+
application = Application.builder().token(TG_BOT_TOKEN).connect_timeout(60.0).read_timeout(60.0).build()
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
application.add_handler(CommandHandler("start", start))
|
| 349 |
application.add_handler(CallbackQueryHandler(button_callback, pattern='^server_'))
|
| 350 |
application.add_handler(MessageHandler(filters.VIDEO | filters.Document.ALL, process_video))
|
| 351 |
|
| 352 |
+
print("MyVideoBot is starting on Hugging Face Spaces (Webhook Mode)...")
|
| 353 |
+
|
| 354 |
+
# Hugging Face ၏ Port 7860 တွင် Webhook ဖွင့်လှစ်ခြင်း
|
| 355 |
+
application.run_webhook(
|
| 356 |
+
listen="0.0.0.0",
|
| 357 |
+
port=7860,
|
| 358 |
+
url_path=TG_BOT_TOKEN,
|
| 359 |
+
webhook_url=f"https://aung990-myvideobot.hf.space/{TG_BOT_TOKEN}"
|
| 360 |
+
)
|
| 361 |
|
| 362 |
if __name__ == '__main__':
|
| 363 |
main()
|