Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,607 +1,822 @@
|
|
| 1 |
-
"""
|
| 2 |
-
๐ Anime Translator with Lip-Sync
|
| 3 |
-
=================================
|
| 4 |
-
|
| 5 |
-
A Streamlit application that translates text between English and Hindi,
|
| 6 |
-
converts it to speech, and generates a lip-synced anime avatar animation.
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
import streamlit as st
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
import tempfile
|
| 12 |
-
import time
|
| 13 |
-
import shutil
|
| 14 |
-
import os
|
| 15 |
-
import subprocess
|
| 16 |
-
from shutil import which
|
| 17 |
-
from typing import Tuple, Optional
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
from utils.
|
| 22 |
-
from utils.
|
| 23 |
-
from utils.
|
| 24 |
-
from utils.
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
r"C:\
|
| 35 |
-
r"C:\Program Files
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
try:
|
| 49 |
-
from pydub import AudioSegment
|
| 50 |
-
AudioSegment.converter = os.path.join(path, "ffmpeg.exe")
|
| 51 |
-
AudioSegment.ffprobe = os.path.join(path, "ffprobe.exe")
|
| 52 |
-
except:
|
| 53 |
-
pass
|
| 54 |
-
return True
|
| 55 |
-
|
| 56 |
-
return False
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def check_ffmpeg_detailed():
|
| 60 |
-
"""Check FFmpeg installation and return detailed status."""
|
| 61 |
-
status = {
|
| 62 |
-
"ffmpeg_in_path": False,
|
| 63 |
-
"ffmpeg_works": False,
|
| 64 |
-
"ffprobe_works": False,
|
| 65 |
-
"pydub_works": False,
|
| 66 |
-
"error_message": None
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
ffmpeg_path = which("ffmpeg")
|
| 70 |
-
status["ffmpeg_in_path"] = ffmpeg_path is not None
|
| 71 |
-
|
| 72 |
-
try:
|
| 73 |
-
result = subprocess.run(
|
| 74 |
-
["ffmpeg", "-version"],
|
| 75 |
-
capture_output=True,
|
| 76 |
-
text=True,
|
| 77 |
-
timeout=5
|
| 78 |
-
)
|
| 79 |
-
status["ffmpeg_works"] = result.returncode == 0
|
| 80 |
-
except Exception as e:
|
| 81 |
-
status["error_message"] = str(e)
|
| 82 |
-
|
| 83 |
-
try:
|
| 84 |
-
result = subprocess.run(
|
| 85 |
-
["ffprobe", "-version"],
|
| 86 |
-
capture_output=True,
|
| 87 |
-
text=True,
|
| 88 |
-
timeout=5
|
| 89 |
-
)
|
| 90 |
-
status["ffprobe_works"] = result.returncode == 0
|
| 91 |
-
except Exception:
|
| 92 |
-
pass
|
| 93 |
-
|
| 94 |
-
try:
|
| 95 |
-
from pydub import AudioSegment
|
| 96 |
-
silence = AudioSegment.silent(duration=100)
|
| 97 |
-
status["pydub_works"] = True
|
| 98 |
-
except Exception as e:
|
| 99 |
-
status["pydub_works"] = False
|
| 100 |
-
if not status["error_message"]:
|
| 101 |
-
status["error_message"] = str(e)
|
| 102 |
-
|
| 103 |
-
return status
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
ffmpeg_found = configure_ffmpeg()
|
| 107 |
-
|
| 108 |
-
# =============================================================================
|
| 109 |
-
# Configuration
|
| 110 |
-
# =============================================================================
|
| 111 |
-
|
| 112 |
-
AVATARS_DIR = Path("./avatars")
|
| 113 |
-
TEMP_DIR = Path(tempfile.gettempdir()) / "anime_translator"
|
| 114 |
-
|
| 115 |
-
AVATARS_DIR.mkdir(parents=True, exist_ok=True)
|
| 116 |
-
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 117 |
-
|
| 118 |
-
# Page configuration
|
| 119 |
-
st.set_page_config(
|
| 120 |
-
page_title="๐ Anime Translator",
|
| 121 |
-
page_icon="๐",
|
| 122 |
-
layout="wide",
|
| 123 |
-
initial_sidebar_state="expanded"
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
st.
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
border
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
border-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
}
|
| 198 |
-
|
| 199 |
-
.stTabs [
|
| 200 |
-
background:
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
#
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
#
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
st.
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
st.
|
| 363 |
-
st.
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
unsafe_allow_html=True
|
| 397 |
-
)
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
)
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
st.
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
st.
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 607 |
)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
๐ Anime Translator with Lip-Sync
|
| 3 |
+
=================================
|
| 4 |
+
|
| 5 |
+
A Streamlit application that translates text between English and Hindi,
|
| 6 |
+
converts it to speech, and generates a lip-synced anime avatar animation.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import streamlit as st
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
import tempfile
|
| 12 |
+
import time
|
| 13 |
+
import shutil
|
| 14 |
+
import os
|
| 15 |
+
import subprocess
|
| 16 |
+
from shutil import which
|
| 17 |
+
from typing import Tuple, Optional
|
| 18 |
+
import base64
|
| 19 |
+
|
| 20 |
+
# Import utility modules
|
| 21 |
+
from utils.translator import translate_text, detect_language
|
| 22 |
+
from utils.tts_engine import synthesize_speech, get_audio_duration
|
| 23 |
+
from utils.lipsync import generate_lipsync_gif
|
| 24 |
+
from utils.speech_to_text import transcribe_audio, get_language_code
|
| 25 |
+
from utils.avatar_manager import list_avatars, get_avatar_preview, ensure_sample_avatar
|
| 26 |
+
|
| 27 |
+
# =============================================================================
|
| 28 |
+
# FFmpeg Configuration
|
| 29 |
+
# =============================================================================
|
| 30 |
+
|
| 31 |
+
def configure_ffmpeg():
|
| 32 |
+
"""Configure FFmpeg path for pydub on Windows."""
|
| 33 |
+
possible_paths = [
|
| 34 |
+
r"C:\ffmpeg\bin",
|
| 35 |
+
r"C:\Program Files\ffmpeg\bin",
|
| 36 |
+
r"C:\Program Files (x86)\ffmpeg\bin",
|
| 37 |
+
os.path.expanduser("~\\ffmpeg\\bin"),
|
| 38 |
+
r"C:\Users\Nishant Pratap\ffmpeg\bin",
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
if which("ffmpeg") is not None:
|
| 42 |
+
return True
|
| 43 |
+
|
| 44 |
+
for path in possible_paths:
|
| 45 |
+
ffmpeg_exe = os.path.join(path, "ffmpeg.exe")
|
| 46 |
+
if os.path.exists(ffmpeg_exe):
|
| 47 |
+
os.environ["PATH"] = path + os.pathsep + os.environ.get("PATH", "")
|
| 48 |
+
try:
|
| 49 |
+
from pydub import AudioSegment
|
| 50 |
+
AudioSegment.converter = os.path.join(path, "ffmpeg.exe")
|
| 51 |
+
AudioSegment.ffprobe = os.path.join(path, "ffprobe.exe")
|
| 52 |
+
except:
|
| 53 |
+
pass
|
| 54 |
+
return True
|
| 55 |
+
|
| 56 |
+
return False
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def check_ffmpeg_detailed():
|
| 60 |
+
"""Check FFmpeg installation and return detailed status."""
|
| 61 |
+
status = {
|
| 62 |
+
"ffmpeg_in_path": False,
|
| 63 |
+
"ffmpeg_works": False,
|
| 64 |
+
"ffprobe_works": False,
|
| 65 |
+
"pydub_works": False,
|
| 66 |
+
"error_message": None
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
ffmpeg_path = which("ffmpeg")
|
| 70 |
+
status["ffmpeg_in_path"] = ffmpeg_path is not None
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
result = subprocess.run(
|
| 74 |
+
["ffmpeg", "-version"],
|
| 75 |
+
capture_output=True,
|
| 76 |
+
text=True,
|
| 77 |
+
timeout=5
|
| 78 |
+
)
|
| 79 |
+
status["ffmpeg_works"] = result.returncode == 0
|
| 80 |
+
except Exception as e:
|
| 81 |
+
status["error_message"] = str(e)
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
result = subprocess.run(
|
| 85 |
+
["ffprobe", "-version"],
|
| 86 |
+
capture_output=True,
|
| 87 |
+
text=True,
|
| 88 |
+
timeout=5
|
| 89 |
+
)
|
| 90 |
+
status["ffprobe_works"] = result.returncode == 0
|
| 91 |
+
except Exception:
|
| 92 |
+
pass
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
from pydub import AudioSegment
|
| 96 |
+
silence = AudioSegment.silent(duration=100)
|
| 97 |
+
status["pydub_works"] = True
|
| 98 |
+
except Exception as e:
|
| 99 |
+
status["pydub_works"] = False
|
| 100 |
+
if not status["error_message"]:
|
| 101 |
+
status["error_message"] = str(e)
|
| 102 |
+
|
| 103 |
+
return status
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
ffmpeg_found = configure_ffmpeg()
|
| 107 |
+
|
| 108 |
+
# =============================================================================
|
| 109 |
+
# Configuration
|
| 110 |
+
# =============================================================================
|
| 111 |
+
|
| 112 |
+
AVATARS_DIR = Path("./avatars")
|
| 113 |
+
TEMP_DIR = Path(tempfile.gettempdir()) / "anime_translator"
|
| 114 |
+
|
| 115 |
+
AVATARS_DIR.mkdir(parents=True, exist_ok=True)
|
| 116 |
+
TEMP_DIR.mkdir(parents=True, exist_ok=True)
|
| 117 |
+
|
| 118 |
+
# Page configuration
|
| 119 |
+
st.set_page_config(
|
| 120 |
+
page_title="๐ Anime Translator",
|
| 121 |
+
page_icon="๐",
|
| 122 |
+
layout="wide",
|
| 123 |
+
initial_sidebar_state="expanded"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# Initialize session state for animation control
|
| 127 |
+
if 'animation_playing' not in st.session_state:
|
| 128 |
+
st.session_state.animation_playing = True
|
| 129 |
+
if 'current_gif_path' not in st.session_state:
|
| 130 |
+
st.session_state.current_gif_path = None
|
| 131 |
+
|
| 132 |
+
# =============================================================================
|
| 133 |
+
# Custom CSS Styling - UPDATED WITH ANIMATION FIX
|
| 134 |
+
# =============================================================================
|
| 135 |
+
|
| 136 |
+
st.markdown("""
|
| 137 |
+
<style>
|
| 138 |
+
.main {
|
| 139 |
+
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.main-header {
|
| 143 |
+
background: linear-gradient(90deg, #e94560, #ff6b6b);
|
| 144 |
+
-webkit-background-clip: text;
|
| 145 |
+
-webkit-text-fill-color: transparent;
|
| 146 |
+
font-size: 3rem;
|
| 147 |
+
font-weight: bold;
|
| 148 |
+
text-align: center;
|
| 149 |
+
padding: 1rem;
|
| 150 |
+
margin-bottom: 2rem;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
.stButton > button {
|
| 154 |
+
background: linear-gradient(90deg, #e94560, #ff6b6b);
|
| 155 |
+
color: white;
|
| 156 |
+
border: none;
|
| 157 |
+
border-radius: 25px;
|
| 158 |
+
padding: 0.75rem 2rem;
|
| 159 |
+
font-weight: bold;
|
| 160 |
+
transition: all 0.3s ease;
|
| 161 |
+
width: 100%;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.stButton > button:hover {
|
| 165 |
+
transform: translateY(-2px);
|
| 166 |
+
box-shadow: 0 5px 20px rgba(233, 69, 96, 0.4);
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
.result-box {
|
| 170 |
+
background: linear-gradient(135deg, rgba(233, 69, 96, 0.1), rgba(255, 107, 107, 0.1));
|
| 171 |
+
border-radius: 15px;
|
| 172 |
+
padding: 1.5rem;
|
| 173 |
+
border: 1px solid rgba(233, 69, 96, 0.3);
|
| 174 |
+
margin: 1rem 0;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
.info-box {
|
| 178 |
+
background: rgba(100, 200, 255, 0.1);
|
| 179 |
+
border-left: 4px solid #64c8ff;
|
| 180 |
+
padding: 1rem;
|
| 181 |
+
border-radius: 0 10px 10px 0;
|
| 182 |
+
margin: 1rem 0;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
.success-box {
|
| 186 |
+
background: rgba(100, 255, 150, 0.1);
|
| 187 |
+
border-left: 4px solid #64ff96;
|
| 188 |
+
padding: 1rem;
|
| 189 |
+
border-radius: 0 10px 10px 0;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
#MainMenu {visibility: hidden;}
|
| 193 |
+
footer {visibility: hidden;}
|
| 194 |
+
|
| 195 |
+
.stTabs [data-baseweb="tab-list"] {
|
| 196 |
+
gap: 8px;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
.stTabs [data-baseweb="tab"] {
|
| 200 |
+
background: rgba(255, 255, 255, 0.05);
|
| 201 |
+
border-radius: 10px;
|
| 202 |
+
padding: 10px 20px;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.stTabs [aria-selected="true"] {
|
| 206 |
+
background: linear-gradient(90deg, #e94560, #ff6b6b);
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
/* ============================================= */
|
| 210 |
+
/* ANIMATION CONTAINER - FIXED SIZE */
|
| 211 |
+
/* ============================================= */
|
| 212 |
+
|
| 213 |
+
.animation-container {
|
| 214 |
+
width: 100%;
|
| 215 |
+
max-width: 400px;
|
| 216 |
+
height: 400px;
|
| 217 |
+
margin: 0 auto;
|
| 218 |
+
display: flex;
|
| 219 |
+
align-items: center;
|
| 220 |
+
justify-content: center;
|
| 221 |
+
background: rgba(0, 0, 0, 0.2);
|
| 222 |
+
border-radius: 15px;
|
| 223 |
+
overflow: hidden;
|
| 224 |
+
position: relative;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
.animation-container img {
|
| 228 |
+
max-width: 100%;
|
| 229 |
+
max-height: 100%;
|
| 230 |
+
object-fit: contain;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
.animation-container.paused img {
|
| 234 |
+
animation-play-state: paused !important;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
/* Static image when paused */
|
| 238 |
+
.animation-static {
|
| 239 |
+
width: 100%;
|
| 240 |
+
max-width: 400px;
|
| 241 |
+
height: 400px;
|
| 242 |
+
margin: 0 auto;
|
| 243 |
+
display: flex;
|
| 244 |
+
align-items: center;
|
| 245 |
+
justify-content: center;
|
| 246 |
+
background: rgba(0, 0, 0, 0.2);
|
| 247 |
+
border-radius: 15px;
|
| 248 |
+
overflow: hidden;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
.animation-static img {
|
| 252 |
+
max-width: 100%;
|
| 253 |
+
max-height: 100%;
|
| 254 |
+
object-fit: contain;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
/* Animation controls */
|
| 258 |
+
.animation-controls {
|
| 259 |
+
display: flex;
|
| 260 |
+
justify-content: center;
|
| 261 |
+
gap: 10px;
|
| 262 |
+
margin-top: 10px;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
.control-btn {
|
| 266 |
+
background: linear-gradient(90deg, #e94560, #ff6b6b);
|
| 267 |
+
color: white;
|
| 268 |
+
border: none;
|
| 269 |
+
border-radius: 20px;
|
| 270 |
+
padding: 8px 20px;
|
| 271 |
+
cursor: pointer;
|
| 272 |
+
font-weight: bold;
|
| 273 |
+
transition: all 0.3s ease;
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
.control-btn:hover {
|
| 277 |
+
transform: scale(1.05);
|
| 278 |
+
box-shadow: 0 3px 15px rgba(233, 69, 96, 0.4);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
.control-btn.stop {
|
| 282 |
+
background: linear-gradient(90deg, #666, #888);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
/* Fixed height result column */
|
| 286 |
+
.result-column {
|
| 287 |
+
min-height: 600px;
|
| 288 |
+
}
|
| 289 |
+
</style>
|
| 290 |
+
""", unsafe_allow_html=True)
|
| 291 |
+
|
| 292 |
+
# =============================================================================
|
| 293 |
+
# Helper Functions
|
| 294 |
+
# =============================================================================
|
| 295 |
+
|
| 296 |
+
def cleanup_temp_files(older_than_sec: int = 3600) -> None:
|
| 297 |
+
"""Clean up old temporary files."""
|
| 298 |
+
now = time.time()
|
| 299 |
+
try:
|
| 300 |
+
for path in TEMP_DIR.iterdir():
|
| 301 |
+
try:
|
| 302 |
+
if now - path.stat().st_mtime > older_than_sec:
|
| 303 |
+
if path.is_file():
|
| 304 |
+
path.unlink()
|
| 305 |
+
elif path.is_dir():
|
| 306 |
+
shutil.rmtree(path)
|
| 307 |
+
except Exception:
|
| 308 |
+
pass
|
| 309 |
+
except Exception:
|
| 310 |
+
pass
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def get_gif_first_frame(gif_path: str) -> Optional[str]:
|
| 314 |
+
"""Extract the first frame of a GIF as a static image."""
|
| 315 |
+
try:
|
| 316 |
+
from PIL import Image
|
| 317 |
+
import io
|
| 318 |
+
|
| 319 |
+
with Image.open(gif_path) as img:
|
| 320 |
+
# Get first frame
|
| 321 |
+
img.seek(0)
|
| 322 |
+
first_frame = img.copy()
|
| 323 |
+
|
| 324 |
+
# Save to bytes
|
| 325 |
+
buffer = io.BytesIO()
|
| 326 |
+
first_frame.save(buffer, format='PNG')
|
| 327 |
+
buffer.seek(0)
|
| 328 |
+
|
| 329 |
+
# Convert to base64
|
| 330 |
+
img_base64 = base64.b64encode(buffer.getvalue()).decode()
|
| 331 |
+
return img_base64
|
| 332 |
+
except Exception as e:
|
| 333 |
+
print(f"Error extracting first frame: {e}")
|
| 334 |
+
return None
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
def display_animation_with_controls(gif_path: str, key_prefix: str = ""):
|
| 338 |
+
"""Display animation with play/pause/stop controls."""
|
| 339 |
+
|
| 340 |
+
if not gif_path or not os.path.exists(gif_path):
|
| 341 |
+
st.info("โน๏ธ No animation available")
|
| 342 |
+
return
|
| 343 |
+
|
| 344 |
+
# Read GIF file
|
| 345 |
+
with open(gif_path, "rb") as f:
|
| 346 |
+
gif_data = f.read()
|
| 347 |
+
gif_base64 = base64.b64encode(gif_data).decode()
|
| 348 |
+
|
| 349 |
+
# Get first frame for static display
|
| 350 |
+
first_frame_base64 = get_gif_first_frame(gif_path)
|
| 351 |
+
|
| 352 |
+
# Animation state key
|
| 353 |
+
state_key = f"{key_prefix}_playing"
|
| 354 |
+
if state_key not in st.session_state:
|
| 355 |
+
st.session_state[state_key] = True
|
| 356 |
+
|
| 357 |
+
# Control buttons
|
| 358 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 359 |
+
|
| 360 |
+
with col1:
|
| 361 |
+
if st.button("โถ๏ธ Play", key=f"{key_prefix}_play", use_container_width=True):
|
| 362 |
+
st.session_state[state_key] = True
|
| 363 |
+
st.rerun()
|
| 364 |
+
|
| 365 |
+
with col2:
|
| 366 |
+
if st.button("โธ๏ธ Pause", key=f"{key_prefix}_pause", use_container_width=True):
|
| 367 |
+
st.session_state[state_key] = False
|
| 368 |
+
st.rerun()
|
| 369 |
+
|
| 370 |
+
with col3:
|
| 371 |
+
if st.button("โน๏ธ Stop", key=f"{key_prefix}_stop", use_container_width=True):
|
| 372 |
+
st.session_state[state_key] = False
|
| 373 |
+
st.rerun()
|
| 374 |
+
|
| 375 |
+
# Display animation or static frame
|
| 376 |
+
if st.session_state[state_key]:
|
| 377 |
+
# Playing - show animated GIF
|
| 378 |
+
st.markdown(
|
| 379 |
+
f'''
|
| 380 |
+
<div class="animation-container">
|
| 381 |
+
<img src="data:image/gif;base64,{gif_base64}" alt="Lip-sync animation">
|
| 382 |
+
</div>
|
| 383 |
+
''',
|
| 384 |
+
unsafe_allow_html=True
|
| 385 |
+
)
|
| 386 |
+
else:
|
| 387 |
+
# Paused/Stopped - show first frame
|
| 388 |
+
if first_frame_base64:
|
| 389 |
+
st.markdown(
|
| 390 |
+
f'''
|
| 391 |
+
<div class="animation-static">
|
| 392 |
+
<img src="data:image/png;base64,{first_frame_base64}" alt="Animation paused">
|
| 393 |
+
</div>
|
| 394 |
+
<p style="text-align: center; color: #888; margin-top: 10px;">โธ๏ธ Animation Paused</p>
|
| 395 |
+
''',
|
| 396 |
+
unsafe_allow_html=True
|
| 397 |
+
)
|
| 398 |
+
else:
|
| 399 |
+
st.info("Animation paused")
|
| 400 |
+
|
| 401 |
+
# Download button
|
| 402 |
+
st.download_button(
|
| 403 |
+
label="๐ฅ Download Animation",
|
| 404 |
+
data=gif_data,
|
| 405 |
+
file_name="lipsync_animation.gif",
|
| 406 |
+
mime="image/gif",
|
| 407 |
+
key=f"{key_prefix}_download",
|
| 408 |
+
use_container_width=True
|
| 409 |
+
)
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
def process_translation_pipeline(
|
| 413 |
+
text: str,
|
| 414 |
+
source_lang: str,
|
| 415 |
+
target_lang: str,
|
| 416 |
+
avatar_name: str
|
| 417 |
+
) -> Tuple[str, Optional[str], Optional[str]]:
|
| 418 |
+
"""Main processing pipeline: translate, synthesize speech, generate animation."""
|
| 419 |
+
|
| 420 |
+
# Step 1: Translate text
|
| 421 |
+
try:
|
| 422 |
+
translated_text = translate_text(text, source_lang, target_lang)
|
| 423 |
+
except Exception as e:
|
| 424 |
+
raise Exception(f"Translation failed: {str(e)}")
|
| 425 |
+
|
| 426 |
+
# Step 2: Synthesize speech
|
| 427 |
+
try:
|
| 428 |
+
audio_path = synthesize_speech(translated_text, target_lang, TEMP_DIR)
|
| 429 |
+
except Exception as e:
|
| 430 |
+
raise Exception(f"Speech synthesis failed: {str(e)}")
|
| 431 |
+
|
| 432 |
+
# Step 3: Generate lip-sync animation
|
| 433 |
+
gif_path = None
|
| 434 |
+
try:
|
| 435 |
+
gif_path = generate_lipsync_gif(
|
| 436 |
+
avatar_name=avatar_name,
|
| 437 |
+
audio_path=audio_path,
|
| 438 |
+
avatars_dir=AVATARS_DIR,
|
| 439 |
+
output_dir=TEMP_DIR,
|
| 440 |
+
fps=12
|
| 441 |
+
)
|
| 442 |
+
except Exception as e:
|
| 443 |
+
print(f"Animation generation warning: {str(e)}")
|
| 444 |
+
gif_path = None
|
| 445 |
+
|
| 446 |
+
return translated_text, audio_path, gif_path
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
# =============================================================================
|
| 450 |
+
# Sidebar
|
| 451 |
+
# =============================================================================
|
| 452 |
+
|
| 453 |
+
with st.sidebar:
|
| 454 |
+
st.markdown("## โ๏ธ Settings")
|
| 455 |
+
|
| 456 |
+
# Avatar selection
|
| 457 |
+
st.markdown("### ๐ญ Avatar Selection")
|
| 458 |
+
avatars = list_avatars(AVATARS_DIR)
|
| 459 |
+
|
| 460 |
+
if avatars:
|
| 461 |
+
selected_avatar = st.selectbox(
|
| 462 |
+
"Choose your avatar",
|
| 463 |
+
options=avatars,
|
| 464 |
+
index=0,
|
| 465 |
+
help="Select an anime avatar for lip-sync animation"
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
preview = get_avatar_preview(selected_avatar, AVATARS_DIR)
|
| 469 |
+
if preview:
|
| 470 |
+
st.image(preview, caption=f"Preview: {selected_avatar}", use_container_width=True)
|
| 471 |
+
else:
|
| 472 |
+
st.warning("No avatars found. Creating sample avatar...")
|
| 473 |
+
ensure_sample_avatar(AVATARS_DIR)
|
| 474 |
+
selected_avatar = "sample"
|
| 475 |
+
st.rerun()
|
| 476 |
+
|
| 477 |
+
st.markdown("---")
|
| 478 |
+
|
| 479 |
+
# Language settings
|
| 480 |
+
st.markdown("### ๐ Language Settings")
|
| 481 |
+
|
| 482 |
+
source_language = st.selectbox(
|
| 483 |
+
"Source Language",
|
| 484 |
+
options=["auto", "en", "hi"],
|
| 485 |
+
format_func=lambda x: {"auto": "๐ Auto-detect", "en": "๐ฌ๐ง English", "hi": "๐ฎ๐ณ Hindi"}[x],
|
| 486 |
+
index=0
|
| 487 |
+
)
|
| 488 |
+
|
| 489 |
+
target_language = st.selectbox(
|
| 490 |
+
"Target Language",
|
| 491 |
+
options=["en", "hi"],
|
| 492 |
+
format_func=lambda x: {"en": "๐ฌ๐ง English", "hi": "๐ฎ๐ณ Hindi"}[x],
|
| 493 |
+
index=1
|
| 494 |
+
)
|
| 495 |
+
|
| 496 |
+
st.markdown("---")
|
| 497 |
+
|
| 498 |
+
# Animation settings
|
| 499 |
+
st.markdown("### ๐ฌ Animation Settings")
|
| 500 |
+
|
| 501 |
+
animation_size = st.slider(
|
| 502 |
+
"Animation Size",
|
| 503 |
+
min_value=200,
|
| 504 |
+
max_value=500,
|
| 505 |
+
value=350,
|
| 506 |
+
step=50,
|
| 507 |
+
help="Adjust the display size of the animation"
|
| 508 |
+
)
|
| 509 |
+
|
| 510 |
+
auto_play = st.checkbox("Auto-play animation", value=True)
|
| 511 |
+
|
| 512 |
+
st.markdown("---")
|
| 513 |
+
|
| 514 |
+
# System status
|
| 515 |
+
st.markdown("### ๐ง System Status")
|
| 516 |
+
|
| 517 |
+
ffmpeg_status = check_ffmpeg_detailed()
|
| 518 |
+
|
| 519 |
+
if ffmpeg_status["ffmpeg_works"]:
|
| 520 |
+
st.success("โ
FFmpeg: Working")
|
| 521 |
+
else:
|
| 522 |
+
st.error("โ FFmpeg: Not working")
|
| 523 |
+
|
| 524 |
+
if ffmpeg_status["pydub_works"]:
|
| 525 |
+
st.success("โ
Pydub: Working")
|
| 526 |
+
else:
|
| 527 |
+
st.warning("โ ๏ธ Pydub: Limited (fallback mode)")
|
| 528 |
+
|
| 529 |
+
if ffmpeg_status["error_message"]:
|
| 530 |
+
with st.expander("๐ Error Details"):
|
| 531 |
+
st.code(ffmpeg_status["error_message"])
|
| 532 |
+
st.markdown("""
|
| 533 |
+
**To fix FFmpeg:**
|
| 534 |
+
```bash
|
| 535 |
+
conda install -c conda-forge ffmpeg
|
| 536 |
+
```
|
| 537 |
+
Or download from: https://www.gyan.dev/ffmpeg/builds/
|
| 538 |
+
""")
|
| 539 |
+
|
| 540 |
+
st.markdown("---")
|
| 541 |
+
|
| 542 |
+
# Info section
|
| 543 |
+
st.markdown("### โน๏ธ About")
|
| 544 |
+
st.markdown("""
|
| 545 |
+
Translate text between English and Hindi with lip-synced avatar animation.
|
| 546 |
+
|
| 547 |
+
**Features:**
|
| 548 |
+
- ๐ค Voice input
|
| 549 |
+
- ๐ Auto detection
|
| 550 |
+
- ๐ฃ๏ธ Text-to-speech
|
| 551 |
+
- ๐ฌ Lip-sync animation
|
| 552 |
+
""")
|
| 553 |
+
|
| 554 |
+
if st.button("๐งน Clear Temp Files"):
|
| 555 |
+
cleanup_temp_files(older_than_sec=0)
|
| 556 |
+
st.success("Cleared!")
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
# =============================================================================
|
| 560 |
+
# Main Content
|
| 561 |
+
# =============================================================================
|
| 562 |
+
|
| 563 |
+
st.markdown('<h1 class="main-header">๐ Anime Translator</h1>', unsafe_allow_html=True)
|
| 564 |
+
st.markdown(
|
| 565 |
+
'<p style="text-align: center; color: #888; font-size: 1.2rem;">'
|
| 566 |
+
'Translate โข Speak โข Animate</p>',
|
| 567 |
+
unsafe_allow_html=True
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
# Tabs
|
| 571 |
+
tab1, tab2 = st.tabs(["๐ Text Input", "๐ค Voice Input"])
|
| 572 |
+
|
| 573 |
+
# =============================================================================
|
| 574 |
+
# Tab 1: Text Input
|
| 575 |
+
# =============================================================================
|
| 576 |
+
|
| 577 |
+
with tab1:
|
| 578 |
+
col1, col2 = st.columns([1, 1])
|
| 579 |
+
|
| 580 |
+
with col1:
|
| 581 |
+
st.markdown("### ๐ Enter Your Text")
|
| 582 |
+
|
| 583 |
+
text_input = st.text_area(
|
| 584 |
+
"Type or paste your text here",
|
| 585 |
+
height=150,
|
| 586 |
+
placeholder="Enter text in English or Hindi...\nเคเคฆเคพเคนเคฐเคฃ: เคจเคฎเคธเฅเคคเฅ, เคเคช เคเฅเคธเฅ เคนเฅเค?\nExample: Hello, how are you?",
|
| 587 |
+
key="text_input"
|
| 588 |
+
)
|
| 589 |
+
|
| 590 |
+
if text_input:
|
| 591 |
+
detected = detect_language(text_input)
|
| 592 |
+
st.markdown(
|
| 593 |
+
f'<div class="info-box">'
|
| 594 |
+
f'๐ Characters: {len(text_input)} | '
|
| 595 |
+
f'๐ Detected: {"๐ฎ๐ณ Hindi" if detected == "hi" else "๐ฌ๐ง English"}'
|
| 596 |
+
f'</div>',
|
| 597 |
+
unsafe_allow_html=True
|
| 598 |
+
)
|
| 599 |
+
|
| 600 |
+
translate_btn = st.button(
|
| 601 |
+
"๐ Translate & Animate",
|
| 602 |
+
key="translate_text_btn",
|
| 603 |
+
use_container_width=True
|
| 604 |
+
)
|
| 605 |
+
|
| 606 |
+
with col2:
|
| 607 |
+
st.markdown("### ๐ฌ Result")
|
| 608 |
+
|
| 609 |
+
# Create a container with fixed height
|
| 610 |
+
result_container = st.container()
|
| 611 |
+
|
| 612 |
+
with result_container:
|
| 613 |
+
if translate_btn and text_input:
|
| 614 |
+
with st.spinner("๐ Processing..."):
|
| 615 |
+
progress = st.progress(0)
|
| 616 |
+
status_text = st.empty()
|
| 617 |
+
|
| 618 |
+
try:
|
| 619 |
+
status_text.text("๐ Translating...")
|
| 620 |
+
progress.progress(33)
|
| 621 |
+
|
| 622 |
+
translated, audio_path, gif_path = process_translation_pipeline(
|
| 623 |
+
text_input,
|
| 624 |
+
source_language,
|
| 625 |
+
target_language,
|
| 626 |
+
selected_avatar
|
| 627 |
+
)
|
| 628 |
+
|
| 629 |
+
status_text.text("๐ฃ๏ธ Generating speech...")
|
| 630 |
+
progress.progress(66)
|
| 631 |
+
|
| 632 |
+
status_text.text("๐ฌ Creating animation...")
|
| 633 |
+
progress.progress(100)
|
| 634 |
+
|
| 635 |
+
progress.empty()
|
| 636 |
+
status_text.empty()
|
| 637 |
+
|
| 638 |
+
# Store results in session state
|
| 639 |
+
st.session_state['text_result'] = {
|
| 640 |
+
'translated': translated,
|
| 641 |
+
'audio_path': audio_path,
|
| 642 |
+
'gif_path': gif_path
|
| 643 |
+
}
|
| 644 |
+
st.session_state['text_animation_playing'] = auto_play
|
| 645 |
+
|
| 646 |
+
except Exception as e:
|
| 647 |
+
progress.empty()
|
| 648 |
+
status_text.empty()
|
| 649 |
+
st.error(f"โ Error: {str(e)}")
|
| 650 |
+
|
| 651 |
+
elif translate_btn:
|
| 652 |
+
st.warning("โ ๏ธ Please enter some text to translate.")
|
| 653 |
+
|
| 654 |
+
# Display stored results
|
| 655 |
+
if 'text_result' in st.session_state:
|
| 656 |
+
result = st.session_state['text_result']
|
| 657 |
+
|
| 658 |
+
# Display translated text
|
| 659 |
+
st.markdown(
|
| 660 |
+
f'<div class="result-box">'
|
| 661 |
+
f'<h4>๐ Translated Text:</h4>'
|
| 662 |
+
f'<p style="font-size: 1.2rem;">{result["translated"]}</p>'
|
| 663 |
+
f'</div>',
|
| 664 |
+
unsafe_allow_html=True
|
| 665 |
+
)
|
| 666 |
+
|
| 667 |
+
# Audio player
|
| 668 |
+
if result['audio_path'] and os.path.exists(result['audio_path']):
|
| 669 |
+
st.markdown("#### ๐ Audio")
|
| 670 |
+
st.audio(result['audio_path'], format="audio/mp3")
|
| 671 |
+
|
| 672 |
+
# Animation display with controls
|
| 673 |
+
if result['gif_path'] and os.path.exists(result['gif_path']):
|
| 674 |
+
st.markdown("#### ๐ญ Lip-Sync Animation")
|
| 675 |
+
display_animation_with_controls(result['gif_path'], key_prefix="text")
|
| 676 |
+
else:
|
| 677 |
+
st.info("โน๏ธ Animation not available (FFmpeg may be missing)")
|
| 678 |
+
|
| 679 |
+
|
| 680 |
+
# =============================================================================
|
| 681 |
+
# Tab 2: Voice Input
|
| 682 |
+
# =============================================================================
|
| 683 |
+
|
| 684 |
+
with tab2:
|
| 685 |
+
col1, col2 = st.columns([1, 1])
|
| 686 |
+
|
| 687 |
+
with col1:
|
| 688 |
+
st.markdown("### ๐ค Voice Recording")
|
| 689 |
+
|
| 690 |
+
st.markdown("""
|
| 691 |
+
<div class="info-box">
|
| 692 |
+
<strong>Instructions:</strong><br>
|
| 693 |
+
1. Upload an audio file (WAV, MP3, etc.)<br>
|
| 694 |
+
2. Or use the audio recorder below<br>
|
| 695 |
+
3. Click "Transcribe & Translate"
|
| 696 |
+
</div>
|
| 697 |
+
""", unsafe_allow_html=True)
|
| 698 |
+
|
| 699 |
+
uploaded_audio = st.file_uploader(
|
| 700 |
+
"Upload an audio file",
|
| 701 |
+
type=["wav", "mp3", "ogg", "flac", "m4a"],
|
| 702 |
+
help="Supported formats: WAV, MP3, OGG, FLAC, M4A"
|
| 703 |
+
)
|
| 704 |
+
|
| 705 |
+
recorded_audio = None
|
| 706 |
+
try:
|
| 707 |
+
from audio_recorder_streamlit import audio_recorder
|
| 708 |
+
st.markdown("**Or record directly:**")
|
| 709 |
+
recorded_audio = audio_recorder(
|
| 710 |
+
text="๐๏ธ Click to record",
|
| 711 |
+
recording_color="#e94560",
|
| 712 |
+
neutral_color="#6c757d",
|
| 713 |
+
icon_name="microphone",
|
| 714 |
+
icon_size="2x"
|
| 715 |
+
)
|
| 716 |
+
except ImportError:
|
| 717 |
+
st.info("๐ก For recording: `pip install audio-recorder-streamlit`")
|
| 718 |
+
|
| 719 |
+
voice_lang = st.selectbox(
|
| 720 |
+
"Recording Language",
|
| 721 |
+
options=["en", "hi"],
|
| 722 |
+
format_func=lambda x: {"en": "๐ฌ๐ง English", "hi": "๐ฎ๐ณ Hindi"}[x]
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
voice_btn = st.button(
|
| 726 |
+
"๐ฏ Transcribe & Translate",
|
| 727 |
+
key="voice_btn",
|
| 728 |
+
use_container_width=True
|
| 729 |
+
)
|
| 730 |
+
|
| 731 |
+
with col2:
|
| 732 |
+
st.markdown("### ๐ฌ Result")
|
| 733 |
+
|
| 734 |
+
audio_to_process = None
|
| 735 |
+
|
| 736 |
+
if uploaded_audio is not None:
|
| 737 |
+
temp_audio_path = TEMP_DIR / f"uploaded_{int(time.time()*1000)}.wav"
|
| 738 |
+
with open(temp_audio_path, "wb") as f:
|
| 739 |
+
f.write(uploaded_audio.getbuffer())
|
| 740 |
+
audio_to_process = str(temp_audio_path)
|
| 741 |
+
st.audio(uploaded_audio)
|
| 742 |
+
|
| 743 |
+
elif recorded_audio is not None:
|
| 744 |
+
temp_audio_path = TEMP_DIR / f"recorded_{int(time.time()*1000)}.wav"
|
| 745 |
+
with open(temp_audio_path, "wb") as f:
|
| 746 |
+
f.write(recorded_audio)
|
| 747 |
+
audio_to_process = str(temp_audio_path)
|
| 748 |
+
st.audio(recorded_audio, format="audio/wav")
|
| 749 |
+
|
| 750 |
+
if voice_btn:
|
| 751 |
+
if audio_to_process:
|
| 752 |
+
with st.spinner("๐ Processing voice..."):
|
| 753 |
+
try:
|
| 754 |
+
st.text("๐ค Transcribing...")
|
| 755 |
+
lang_code = get_language_code(voice_lang)
|
| 756 |
+
transcribed_text, success = transcribe_audio(audio_to_process, lang_code)
|
| 757 |
+
|
| 758 |
+
if success:
|
| 759 |
+
translated, audio_path, gif_path = process_translation_pipeline(
|
| 760 |
+
transcribed_text,
|
| 761 |
+
voice_lang,
|
| 762 |
+
target_language,
|
| 763 |
+
selected_avatar
|
| 764 |
+
)
|
| 765 |
+
|
| 766 |
+
# Store results in session state
|
| 767 |
+
st.session_state['voice_result'] = {
|
| 768 |
+
'transcribed': transcribed_text,
|
| 769 |
+
'translated': translated,
|
| 770 |
+
'audio_path': audio_path,
|
| 771 |
+
'gif_path': gif_path
|
| 772 |
+
}
|
| 773 |
+
st.session_state['voice_animation_playing'] = auto_play
|
| 774 |
+
|
| 775 |
+
else:
|
| 776 |
+
st.error(f"โ {transcribed_text}")
|
| 777 |
+
except Exception as e:
|
| 778 |
+
st.error(f"โ Error: {str(e)}")
|
| 779 |
+
else:
|
| 780 |
+
st.warning("โ ๏ธ Please upload or record audio first.")
|
| 781 |
+
|
| 782 |
+
# Display stored results
|
| 783 |
+
if 'voice_result' in st.session_state:
|
| 784 |
+
result = st.session_state['voice_result']
|
| 785 |
+
|
| 786 |
+
st.markdown(
|
| 787 |
+
f'<div class="success-box">'
|
| 788 |
+
f'<strong>๐ Transcribed:</strong> {result["transcribed"]}'
|
| 789 |
+
f'</div>',
|
| 790 |
+
unsafe_allow_html=True
|
| 791 |
+
)
|
| 792 |
+
|
| 793 |
+
st.markdown(
|
| 794 |
+
f'<div class="result-box">'
|
| 795 |
+
f'<h4>๐ Translated:</h4>'
|
| 796 |
+
f'<p style="font-size: 1.2rem;">{result["translated"]}</p>'
|
| 797 |
+
f'</div>',
|
| 798 |
+
unsafe_allow_html=True
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
if result['audio_path'] and os.path.exists(result['audio_path']):
|
| 802 |
+
st.markdown("#### ๐ Audio")
|
| 803 |
+
st.audio(result['audio_path'], format="audio/mp3")
|
| 804 |
+
|
| 805 |
+
if result['gif_path'] and os.path.exists(result['gif_path']):
|
| 806 |
+
st.markdown("#### ๐ญ Animation")
|
| 807 |
+
display_animation_with_controls(result['gif_path'], key_prefix="voice")
|
| 808 |
+
|
| 809 |
+
|
| 810 |
+
# =============================================================================
|
| 811 |
+
# Footer
|
| 812 |
+
# =============================================================================
|
| 813 |
+
|
| 814 |
+
st.markdown("---")
|
| 815 |
+
st.markdown(
|
| 816 |
+
"""
|
| 817 |
+
<div style="text-align: center; color: #666; padding: 1rem;">
|
| 818 |
+
<p>Made By Praveen</p>
|
| 819 |
+
</div>
|
| 820 |
+
""",
|
| 821 |
+
unsafe_allow_html=True
|
| 822 |
)
|