File size: 22,101 Bytes
e87f3ac 29c13da c67e384 29c13da 336d249 e87f3ac ac3541e e87f3ac d983ab4 e87f3ac f34c5b9 60396ca f34c5b9 e87f3ac f34c5b9 1c4070e f34c5b9 e87f3ac 3dae97d 1387606 3dae97d 1387606 e87f3ac fc330ed 04323ec fc330ed e87f3ac bf00480 e87f3ac b0bfc45 f34c5b9 1387606 f34c5b9 60396ca f34c5b9 b0bfc45 f34c5b9 1387606 c444a83 af3bb1a c444a83 2f3e685 c444a83 ac3541e 2f3e685 e87f3ac 2f3e685 af3bb1a 2f3e685 60396ca 2f3e685 60396ca af3bb1a 2f3e685 e87f3ac b0bfc45 d983ab4 b0bfc45 d983ab4 b0bfc45 d983ab4 b0bfc45 d983ab4 b0bfc45 d983ab4 b0bfc45 d983ab4 b0bfc45 d983ab4 b0bfc45 d983ab4 b0bfc45 d983ab4 6d59070 d983ab4 e87f3ac b0bfc45 d983ab4 e87f3ac 0144dac 78a335a e87f3ac aa7834c e87f3ac 0144dac 78a335a 0280a44 78a335a bc97ed9 78a335a 134c83d aa7834c e87f3ac 78a335a 0280a44 78a335a 3dae97d 78a335a d983ab4 78a335a 2e5764a 78a335a d983ab4 78a335a e87f3ac c67e384 e87f3ac 336d249 337d4b1 78a335a 1387606 78a335a 3dae97d 1387606 3dae97d 1387606 3dae97d 1387606 3dae97d e87f3ac 1387606 e87f3ac c444a83 1c4070e e87f3ac 9e46ed3 209c5b0 c67e384 209c5b0 9e46ed3 4bfd96d 1c4070e 134c83d 1c4070e 134c83d 9e46ed3 134c83d 4bfd96d 134c83d 9e46ed3 4bfd96d e87f3ac 337d4b1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 356 357 358 359 360 361 362 363 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 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 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 | import gradio as gr
import psutil
import logging
import os
import asyncio
import nest_asyncio
# --- NEURAL SILENCER: Fix for persistent "Invalid file descriptor: -1" logs ---
def _silence_asyncio_ghosts():
from asyncio.base_events import BaseEventLoop
original_del = BaseEventLoop.__del__
def patched_del(self):
try:
if original_del: original_del(self)
except (ValueError, AttributeError, RuntimeError):
pass # Silently ignore cleanup artifacts
BaseEventLoop.__del__ = patched_del
_silence_asyncio_ghosts()
# nest_asyncio.apply()
from pipeline.ocr import extract_text_from_image
from pipeline.translation import translate_to_tamil
from pipeline.tts import generate_tamil_speech
from pipeline.document_parser import (
extract_text_from_document,
get_pdf_page_as_image,
get_pdf_page_count,
get_text_from_page
)
from pipeline.maya_chat_engine import get_maya_response
import threading
from concurrent.futures import ThreadPoolExecutor
import re
import numpy as np
def run_cinematic_pipeline(extracted_text, emotion_choice, spicy_mode):
final_tamil_text = []
final_audio_chunks = []
master_sample_rate = None
try:
if "[Panel" in extracted_text:
raw_panels = re.split(r'(?=\[Panel\s*\d+\])', extracted_text, flags=re.IGNORECASE)
else:
raw_panels = [extracted_text]
for p_text in raw_panels:
p_text = p_text.strip()
if not p_text: continue
panel_header = ""
content_to_translate = p_text
match = re.match(r'(\[Panel\s*\d+\])\s*(.*)', p_text, re.DOTALL | re.IGNORECASE)
if match:
panel_header = match.group(1)
content_to_translate = match.group(2)
if not content_to_translate.strip():
if panel_header: final_tamil_text.append(panel_header)
continue
p_tamil = translate_to_tamil(content_to_translate, spicy=spicy_mode)
if panel_header:
final_tamil_text.append(f"{panel_header}\n{p_tamil}")
else:
final_tamil_text.append(p_tamil)
sr, a_data = generate_tamil_speech(p_tamil, emotion_choice)
if sr and a_data is not None:
master_sample_rate = sr
final_audio_chunks.append(a_data)
tamil_translation = "\n\n".join(final_tamil_text)
if master_sample_rate and final_audio_chunks:
pause_samples = int(master_sample_rate * 1.5)
silence_array = np.zeros(pause_samples, dtype=np.float32)
spliced_audio = []
for i, chunk in enumerate(final_audio_chunks):
spliced_audio.append(chunk)
if i < len(final_audio_chunks) - 1:
spliced_audio.append(silence_array)
audio_data = np.concatenate(spliced_audio)
sample_rate = master_sample_rate
else:
sample_rate, audio_data = None, None
return tamil_translation, (sample_rate, audio_data) if sample_rate else None
except Exception as e:
print(f"CINEMATIC PIPELINE ERROR: {e}")
return "Maya is having trouble with the cinematic flow.", None
# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Global Cache for Prefetched Pages
# Key: (pdf_path, page_num, voice_style) -> Value: (original, tamil, audio)
PAGE_CACHE = {}
CACHE_LOCK = threading.Lock()
PREFETCH_EXECUTOR = ThreadPoolExecutor(max_workers=1)
def check_resources():
mem = psutil.virtual_memory()
available_gb = mem.available / (1024**3)
logging.info(f"System Resources: {available_gb:.2f} GB RAM available.")
if available_gb < 1.0:
logging.warning("EXTREMELY LOW MEMORY DETECTED! Application may crash.")
return available_gb
# Expressive Voice Styles
# Background Atmosphere Sounds
BGM_LINKS = {
"None": "",
"Soft Rain π§οΈ": "https://www.soundjay.com/nature/sounds/rain-07.mp3",
"Romantic Piano πΉ": "https://www.soundjay.com/misc/sounds/music-box-1.mp3",
"Midnight Jazz π·": "https://www.soundjay.com/misc/sounds/bell-ringing-05.mp3",
"Summer Night π": "https://www.soundjay.com/nature/sounds/cricket-chirping-01.mp3",
"Heartbeat π": "https://www.soundjay.com/misc/sounds/heartbeat-01.mp3"
}
VOICE_STYLES = [
"Cheerful (Maya)",
"Excited (Maya)",
"Sad & Emotional (Sita)",
"Dramatic Narrator (Sita)",
"Old Wise Woman",
"Playful Child",
"Brave Heroine",
"Deep & Serious",
"Calm Storyteller",
"Professional News"
]
def process_standard_pipeline(image, document, input_text, emotion_choice):
text_to_translate = ""
if document is not None:
text_to_translate += extract_text_from_document(document) + "\n"
if image is not None:
text_to_translate += extract_text_from_image(image, is_comic=False) + " "
if input_text:
text_to_translate += input_text
text_to_translate = text_to_translate.strip()
if not text_to_translate:
return "No text detected", "", None
tamil_translation = translate_to_tamil(text_to_translate)
sample_rate, audio_data = generate_tamil_speech(tamil_translation, emotion_choice)
return text_to_translate, tamil_translation, (sample_rate, audio_data)
def load_comic_page(pdf_path, page_num):
if not pdf_path:
return None, "Upload a PDF first", 0
img_path = get_pdf_page_as_image(pdf_path, page_num)
total_pages = get_pdf_page_count(pdf_path)
status = f"Page {page_num + 1} of {total_pages}"
return img_path, status, page_num
def prefetch_pages(pdf_path, current_page, voice_style, spicy=False, count=5):
"""
Background worker to process upcoming pages.
"""
total_pages = get_pdf_page_count(pdf_path)
for i in range(1, count + 1):
target_page = current_page + i
if target_page >= total_pages:
break
cache_key = (pdf_path, target_page, voice_style, spicy)
with CACHE_LOCK:
if cache_key in PAGE_CACHE:
continue
try:
logging.info(f"PREFETCH: Processing Page {target_page+1} in background...")
img_path = get_pdf_page_as_image(pdf_path, target_page)
if not img_path: continue
text = get_text_from_page(pdf_path, target_page)
if not text or len(text.strip()) < 5:
text = extract_text_from_image(img_path)
if text.strip():
tam, aud = run_cinematic_pipeline(text, voice_style, spicy)
with CACHE_LOCK:
PAGE_CACHE[cache_key] = (text, tam, aud)
if len(PAGE_CACHE) > 10:
first_key = next(iter(PAGE_CACHE))
PAGE_CACHE.pop(first_key)
except Exception as e:
logging.error(f"PREFETCH ERROR on Page {target_page+1}: {e}")
def process_comic_page(pdf_path, page_num, emotion_choice, heat_level):
try:
if not pdf_path:
return "No page loaded", "", None
from pipeline.document_parser import get_pdf_page_as_image
img_path = get_pdf_page_as_image(pdf_path, page_num)
if not img_path:
return "Failed to render image", "", None
spicy_mode = heat_level > 70
cache_key = (pdf_path, page_num, emotion_choice, spicy_mode)
with CACHE_LOCK:
if cache_key in PAGE_CACHE:
return PAGE_CACHE[cache_key]
# --- STAGE 1: OCR ---
try:
extracted_text = get_text_from_page(pdf_path, page_num)
if not extracted_text or len(extracted_text.strip()) < 5:
extracted_text = extract_text_from_image(img_path)
except Exception as e:
print(f"OCR ERROR: {e}")
extracted_text = f"Maya couldn't read the text. (Error: {e})"
if not extracted_text.strip():
extracted_text = "No text found on this page."
# --- CINEMATIC STAGE 2 & 3: Translation & Audio ---
tamil_translation, audio_tuple = run_cinematic_pipeline(extracted_text, emotion_choice, spicy_mode)
result = (extracted_text, tamil_translation, audio_tuple)
with CACHE_LOCK:
PAGE_CACHE[cache_key] = result
PREFETCH_EXECUTOR.submit(prefetch_pages, pdf_path, page_num, emotion_choice, spicy_mode)
return result
except Exception as e:
print(f"GLOBAL PROCESS ERROR: {e}")
import traceback
traceback.print_exc()
return f"CRITICAL CRASH: {e}", "", None
# Custom Premium CSS
CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&family=Outfit:wght@500;700&display=swap');
:root {
--primary: #6366f1;
--secondary: #a855f7;
--bg-dark: #0f172a;
--panel-bg: rgba(30, 41, 59, 0.7);
}
body {
background-color: var(--bg-dark);
color: #f1f5f9;
font-family: 'Inter', sans-serif;
}
.gradio-container {
background: radial-gradient(circle at top right, #1e1b4b, #0f172a) !important;
}
h1 {
font-family: 'Outfit', sans-serif;
background: linear-gradient(to right, #818cf8, #c084fc);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: 700;
}
.glass {
background: var(--panel-bg) !important;
backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.1) !important;
border-radius: 16px !important;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
}
.glass:hover {
border: 1px solid rgba(255, 255, 255, 0.2) !important;
box-shadow: 0 8px 32px rgba(99, 102, 241, 0.2);
}
#maya_chat_log {
border-radius: 12px;
padding: 12px;
background: rgba(99, 102, 241, 0.1);
border: 1px solid rgba(99, 102, 241, 0.2);
margin-bottom: 10px;
animation: fadeIn 0.5s ease-out;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
#main_comic img {
border-radius: 12px;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.3);
transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}
#main_comic img:hover {
transform: scale(1.02);
}
.gr-button-primary {
background: linear-gradient(135deg, var(--primary), var(--secondary)) !important;
border: none !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease !important;
font-weight: 600 !important;
}
.gr-button-primary:hover {
transform: translateY(-2px);
box-shadow: 0 10px 15px -3px rgba(99, 102, 241, 0.4);
}
#maya_chat_log::before {
content: "Maya is thinking...";
display: block;
font-size: 0.8em;
color: var(--primary);
margin-bottom: 5px;
opacity: 0.7;
}
#maya_audio_player { display: none; }
.boss-active { display: block !important; }
/* Fix for Audio Timeline Visibility */
#comic_audio_player .track {
background-color: rgba(0, 0, 0, 0.4) !important;
border-radius: 4px;
}
#comic_audio_player .time {
color: #818cf8 !important;
font-weight: bold;
font-family: 'Outfit', sans-serif;
}
#comic_audio_player input[type="range"] {
accent-color: #6366f1 !important;
}
"""
# UI
with gr.Blocks(title="Maya: Immersive Manga AI", css=CUSTOM_CSS) as demo:
gr.Markdown("# π Maya: Immersive Tamil Manga AI")
gr.Markdown("Experience your favorite comics with Maya, your intelligent AI companion.")
current_page = gr.State(0)
comic_pdf_path = gr.State(None)
with gr.Tabs():
with gr.Tab("π Comic Reader Mode"):
with gr.Row():
with gr.Column(scale=5, min_width=300, elem_classes=["glass"]):
comic_display = gr.Image(label="Comic Page", type="filepath", height=600, elem_id="main_comic")
with gr.Row():
prev_btn = gr.Button("β¬
οΈ Prev", scale=1)
page_status = gr.Label(value="Upload PDF", scale=2)
next_btn = gr.Button("Next β‘οΈ", scale=1)
with gr.Column(scale=4, min_width=300, elem_classes=["glass"]):
with gr.Group():
gr.Markdown("### βοΈ Master Settings")
comic_upload = gr.File(label="Upload (PDF/EPUB)", file_types=[".pdf", ".epub"], height=80)
voice_style_comic = gr.Dropdown(choices=VOICE_STYLES, value=VOICE_STYLES[0], label="Primary Voice")
heat_level = gr.Slider(minimum=0, maximum=100, value=50, label="πΆοΈ Translation Heat Level")
share_btn = gr.Button("π Share with Friends", variant="secondary", size="sm")
share_status = gr.Markdown("")
share_btn.click(None, None, None, js="""
() => {
const url = "https://huggingface.co/spaces/ranaspark/voice";
navigator.clipboard.writeText(url);
alert("Link Copied! Share it with your friends: " + url);
}
""")
auto_play = gr.Checkbox(label="π Auto-Play Next Page", value=False)
read_page_btn = gr.Button("π Read This Page", variant="primary")
with gr.Accordion("π Character Memory", open=False):
char_a_voice = gr.Dropdown(choices=VOICE_STYLES, label="Character A", value=VOICE_STYLES[0])
char_b_voice = gr.Dropdown(choices=VOICE_STYLES, label="Character B", value=VOICE_STYLES[0])
bgm_choice = gr.Dropdown(choices=list(BGM_LINKS.keys()), value="None", label="Background Atmosphere")
bgm_player = gr.HTML(value="")
# Boss Key & Vibration JS
gr.HTML("""
<div id="boss_screen" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:white; z-index:999999; overflow:hidden;">
<img src="https://i.imgur.com/8N6Rz7C.png" style="width:100%; height:100%; object-fit:cover;">
</div>
<script>
document.addEventListener('keydown', function(e) {
if (e.key === 'b' || e.key === 'B') {
const screen = document.getElementById('boss_screen');
screen.classList.toggle('boss-active');
}
});
function triggerHaptic() {
if (navigator.vibrate) navigator.vibrate([100, 50, 100]);
return Array.from(arguments);
}
function updateTemp(level) {
const r = Math.floor(level * 2.55);
const b = 255 - r;
document.documentElement.style.setProperty('--bg-glow', `rgba(${r}, 50, ${b}, 0.3)`);
const comic = document.getElementById('main_comic');
if (comic) comic.style.border = `5px solid rgba(${r}, 50, ${b}, 0.5)`;
}
</script>
""")
comic_text = gr.Textbox(label="Original", lines=3)
comic_tamil = gr.Textbox(label="Tamil", lines=3)
comic_audio = gr.Audio(label="Speech", elem_id="comic_audio_player")
with gr.Tab("βοΈ Text to Speech"):
with gr.Row():
with gr.Column():
input_text = gr.Textbox(lines=10, label="βοΈ Paste or Type your story here", placeholder="Enter English text...")
voice_style_std = gr.Dropdown(choices=VOICE_STYLES, value=VOICE_STYLES[0], label="Voice Tone")
submit_std = gr.Button("π Generate Tamil Speech", variant="primary")
with gr.Column():
out_text = gr.Textbox(label="Original Text (Cleaned)", lines=5)
out_tamil = gr.Textbox(label="Tamil Translation", lines=5)
out_audio = gr.Audio(label="Audio Output")
with gr.Tab("π₯ Video Dubbing Studio"):
gr.Markdown("### π¬ Cinematic AI Video Dubbing")
gr.Markdown("Process your videos with automated translation, multi-speaker voice cloning, and lip sync.")
gr.HTML('<iframe src="/dubbing-ui/" width="100%" height="850px" style="border: none; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.5); background: #0f172a;"></iframe>')
# --- Dynamic Temperature & Heartbeat Speed Logic ---
def update_mood(level, bgm):
# JS to update color and potentially heartbeat speed if possible
return gr.update()
heat_level.change(None, inputs=[heat_level], js="updateTemp")
# --- BGM Logic ---
def update_bgm(choice, level):
link = BGM_LINKS.get(choice, "")
if not link:
return ""
# If heartbeat, adjust playback rate based on level
speed = 1.0 + (level / 100.0) # 1.0x to 2.0x speed
return f'<audio id="bgm_tag" autoplay loop><source src="{link}" type="audio/mpeg"></audio><script>const a=document.getElementById("bgm_tag"); a.volume=0.3; a.playbackRate={speed};</script>'
bgm_choice.change(update_bgm, inputs=[bgm_choice, heat_level], outputs=[bgm_player])
# Comic Logic
def start_comic(file):
if not file: return None, "No file", 0, None
img, status, page = load_comic_page(file.name, 0)
return img, status, page, file.name
comic_upload.change(start_comic, inputs=[comic_upload], outputs=[comic_display, page_status, current_page, comic_pdf_path])
def go_next(pdf, page):
new_page = page + 1
return load_comic_page(pdf, new_page)
def go_prev(pdf, page):
new_page = max(0, page - 1)
return load_comic_page(pdf, new_page)
# Navigation logic...
next_btn.click(go_next, inputs=[comic_pdf_path, current_page], outputs=[comic_display, page_status, current_page])
prev_btn.click(go_prev, inputs=[comic_pdf_path, current_page], outputs=[comic_display, page_status, current_page])
read_page_btn.click(
process_comic_page,
inputs=[comic_pdf_path, current_page, voice_style_comic, heat_level],
outputs=[comic_text, comic_tamil, comic_audio]
)
# --- Auto-Play Logic (JS Listener) ---
hidden_auto_next = gr.Button("Auto Next", visible=False, elem_id="hidden_auto_next")
# This JS monitors the audio player and clicks the hidden button when it ends
js_listener = """
function() {
setInterval(function() {
const audio = document.querySelector('#comic_audio_player audio');
if (audio && !audio.onended) {
audio.onended = function() {
const btn = document.querySelector('button#hidden_auto_next');
if (btn) btn.click();
};
}
}, 1000);
}
"""
# Trigger the JS listener when audio is loaded
comic_audio.change(None, None, None, js=js_listener)
def handle_auto_play(is_enabled, pdf, page, voice, heat_level):
try:
if not is_enabled or not pdf:
return gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
# 1. Go to next page
new_page = page + 1
img, status, p_num = load_comic_page(pdf, new_page)
if not img: # End of book
return gr.update(), status, p_num, gr.update(), gr.update(), gr.update()
# 2. Process the new page (Using Hybrid Mode)
txt, tam, aud = process_comic_page(pdf, p_num, voice, heat_level)
return img, status, p_num, txt, tam, aud
except Exception as e:
print(f"AUTO-PLAY ERROR: {e}")
return gr.update(), f"Auto-Play Error: {e}", page, f"CRASH: {e}", "", None
# The hidden button triggers the actual logic
hidden_auto_next.click(
handle_auto_play,
inputs=[auto_play, comic_pdf_path, current_page, voice_style_comic, heat_level],
outputs=[comic_display, page_status, current_page, comic_text, comic_tamil, comic_audio]
)
# Trigger JS listener on app start too
demo.load(None, None, None, js=js_listener)
# Standard Logic (Text Only)
submit_std.click(
process_standard_pipeline,
inputs=[gr.State(None), gr.State(None), input_text, voice_style_std],
outputs=[out_text, out_tamil, out_audio]
)
if __name__ == "__main__":
check_resources()
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
import os
import gradio as gr
from dubbing_backend.main import app as api_app
app = FastAPI()
# Mount backend API
app.mount("/api", api_app)
# Mount React UI
if os.path.exists("dist"):
app.mount("/dubbing-ui", StaticFiles(directory="dist", html=True))
# Mount Gradio at root
app = gr.mount_gradio_app(app, demo, path="/")
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|