Spaces:
Sleeping
Sleeping
Upload 6 files
Browse filesFiles changed (2)
- webui/app.py+841-816
- webui/library.py+4-4
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816import gradio as grimport subprocessimport osimport sysimport jsonimport psutilimport datetimeimport timeimport urllib.parsefrom fastapi import FastAPIfrom fastapi.staticfiles import StaticFilesimport uvicornimport reimport library # Module for Library Logicimport subtitle_handler as subs # Module for Subtitlesimport subtitle_editor as editor # Module for Editor Logic# Path to the main scriptMAIN_SCRIPT_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "main_improved.py")WORKING_DIR = os.path.dirname(MAIN_SCRIPT_PATH)sys.path.append(WORKING_DIR)from i18n.i18n import I18nAutoi18n = I18nAuto()# --- PRESETS DEFINITIONS ---FACE_PRESETS = {"Default (Balanced)": {"thresh": 0.35, "two_face": 0.60, "conf": 0.40, "dead_zone": 150},"Stable (Focus Main)": {"thresh": 0.60, "two_face": 0.80, "conf": 0.60, "dead_zone": 200},"Sensitive (Catch All)": {"thresh": 0.10, "two_face": 0.40, "conf": 0.30, "dead_zone": 100},"High Precision": {"thresh": 0.40, "two_face": 0.65, "conf": 0.75, "dead_zone": 150},}EXPERIMENTAL_PRESETS = {"Default (Off)": {"focus": False, "mar": 0.03, "score": 1.5, "motion": False, "motion_th": 3.0, "motion_sens": 0.05, "decay": 2.0},"Active Speaker (Balanced)": {"focus": True, "mar": 0.03, "score": 1.5, "motion": True, "motion_th": 3.0, "motion_sens": 0.05, "decay": 2.0},"Active Speaker (Sensitive)": {"focus": True, "mar": 0.02, "score": 1.0, "motion": True, "motion_th": 2.0, "motion_sens": 0.10, "decay": 1.0},"Active Speaker (Stable)": {"focus": True, "mar": 0.05, "score": 2.5, "motion": False, "motion_th": 5.0, "motion_sens": 0.02, "decay": 3.0},}# ---------------------------VIRALS_DIR = os.path.join(WORKING_DIR, "VIRALS")MODELS_DIR = os.path.join(WORKING_DIR, "models")# Ensure directories existif not os.path.exists(VIRALS_DIR):os.makedirs(VIRALS_DIR, exist_ok=True)if not os.path.exists(MODELS_DIR):os.makedirs(MODELS_DIR, exist_ok=True)# Global variablescurrent_process = None# Helpersdef convert_color_to_ass(hex_color, alpha="00"):if not hex_color or not hex_color.startswith("#"):return f"&H{alpha}FFFFFF&"hex_clean = hex_color.lstrip('#')if len(hex_clean) == 6:r = hex_clean[0:2]g = hex_clean[2:4]b = hex_clean[4:6]return f"&H{alpha}{b}{g}{r}&"return f"&H{alpha}FFFFFF&"def kill_process():global current_processif current_process:try:parent = psutil.Process(current_process.pid)for child in parent.children(recursive=True):child.kill()parent.kill()current_process = Nonereturn i18n("Process terminated.")except Exception as e:return i18n("Error terminating process: {}").format(e)return i18n("No process running.")GEMINI_MODELS = ['gemini-3-pro-preview','gemini-2.5-flash','gemini-2.5-flash-preview-09-2025','gemini-2.5-flash-lite','gemini-2.5-flash-lite-preview-09-2025','gemini-2.5-pro','gemini-2.0-flash','gemini-2.0-flash-lite']G4F_MODELS = ['gpt-4o','gpt-4o-mini','gpt-4','o1-mini','o1','deepseek-r1','deepseek-v3','llama-3.3-70b','llama-3.1-405b','claude-3.5-sonnet','claude-3.7-sonnet','gemini-2.0-flash','qwen-2.5-72b']def get_local_models():if not os.path.exists(MODELS_DIR): return []return [f for f in os.listdir(MODELS_DIR) if f.endswith(".gguf")]def apply_face_preset(preset_name):if preset_name not in FACE_PRESETS:return [gr.update() for _ in range(4)] # No changep = FACE_PRESETS[preset_name]return p["thresh"], p["two_face"], p["conf"], p["dead_zone"]def apply_experimental_preset(preset_name):if preset_name not in EXPERIMENTAL_PRESETS:return [gr.update() for _ in range(7)] # No changep = EXPERIMENTAL_PRESETS[preset_name]return p["focus"], p["mar"], p["score"], p["motion"], p["motion_th"], p["motion_sens"], p["decay"]# Subtitle logic moved to subtitle_handler.pydef run_viral_cutter(input_source, project_name, url, segments, viral, themes, min_duration, max_duration, model, ai_backend, api_key, ai_model_name, chunk_size, workflow, face_model, face_mode, face_detect_interval,face_filter_thresh, face_two_thresh, face_conf_thresh, face_dead_zone, focus_active_speaker, active_speaker_mar, active_speaker_score_diff, include_motion, active_speaker_motion_threshold, active_speaker_motion_sensitivity, active_speaker_decay,use_custom_subs, font_name, font_size, font_color, highlight_color, outline_color, outline_thickness, shadow_color, shadow_size, is_bold, is_italic, is_uppercase, vertical_pos, alignment,h_size, w_block, gap, mode, under, strike, border_s, remove_punc, video_quality, use_youtube_subs, translate_target):global current_processyield "", gr.update(value=i18n("Running..."), interactive=False), gr.update(visible=True), Nonecmd = [sys.executable, MAIN_SCRIPT_PATH]# Input Source Logicif input_source == "Existing Project":if not project_name:yield i18n("Error: No project selected."), gr.update(value=i18n("Start Processing"), interactive=True), gr.update(visible=False), Nonereturnfull_project_path = os.path.join(VIRALS_DIR, project_name)cmd.extend(["--project-path", full_project_path])else:if url: cmd.extend(["--url", url])# Pass Video Qualityif video_quality: cmd.extend(["--video-quality", video_quality])# Pass Subtitle Option (if False, we skip)if not use_youtube_subs: cmd.append("--skip-youtube-subs")# Translationif translate_target and translate_target != "None":cmd.extend(["--translate-target", translate_target])cmd.extend(["--segments", str(int(segments))])if viral: cmd.append("--viral")if themes: cmd.extend(["--themes", themes])cmd.extend(["--min-duration", str(int(min_duration))])cmd.extend(["--max-duration", str(int(max_duration))])cmd.extend(["--model", model])cmd.extend(["--ai-backend", ai_backend])if api_key: cmd.extend(["--api-key", api_key])# New AI Paramsif ai_model_name: cmd.extend(["--ai-model-name", str(ai_model_name)])if chunk_size: cmd.extend(["--chunk-size", str(int(chunk_size))])workflow_map = {"Full": "1", "Cut Only": "2", "Subtitles Only": "3"}cmd.extend(["--workflow", workflow_map.get(workflow, "1")])cmd.extend(["--face-model", face_model])cmd.extend(["--face-mode", face_mode])if face_detect_interval: cmd.extend(["--face-detect-interval", str(face_detect_interval)])# New Face Paramsif face_filter_thresh is not None: cmd.extend(["--face-filter-threshold", str(face_filter_thresh)])if face_two_thresh is not None: cmd.extend(["--face-two-threshold", str(face_two_thresh)])if face_conf_thresh is not None: cmd.extend(["--face-confidence-threshold", str(face_conf_thresh)])if face_dead_zone is not None: cmd.extend(["--face-dead-zone", str(face_dead_zone)])cmd.append("--skip-prompts")if focus_active_speaker:cmd.append("--focus-active-speaker")if active_speaker_mar is not None: cmd.extend(["--active-speaker-mar", str(active_speaker_mar)])if active_speaker_score_diff is not None: cmd.extend(["--active-speaker-score-diff", str(active_speaker_score_diff)])if include_motion: cmd.append("--include-motion")if active_speaker_motion_threshold is not None: cmd.extend(["--active-speaker-motion-threshold", str(active_speaker_motion_threshold)])if active_speaker_motion_sensitivity is not None: cmd.extend(["--active-speaker-motion-sensitivity", str(active_speaker_motion_sensitivity)])if active_speaker_decay is not None: cmd.extend(["--active-speaker-decay", str(active_speaker_decay)])cmd.append("--skip-prompts") # Always skip prompts in WebUI to prevent freezingif use_custom_subs:subtitle_config = {"font": font_name, "base_size": int(font_size), "base_color": convert_color_to_ass(font_color), "highlight_color": convert_color_to_ass(highlight_color),"outline_color": convert_color_to_ass(outline_color), "outline_thickness": outline_thickness, "shadow_color": convert_color_to_ass(shadow_color),"shadow_size": shadow_size, "vertical_position": vertical_pos, "alignment": alignment, "bold": 1 if is_bold else 0, "italic": 1 if is_italic else 0,"underline": 1 if under else 0, "strikeout": 1 if strike else 0, "border_style": border_s, "words_per_block": int(w_block), "gap_limit": gap,"mode": mode, "highlight_size": int(h_size), "remove_punctuation": remove_punc}# Uppercase is handled in main script or logic?# Actually subtitle_config doesn't seem to natively support "uppercase" in get_subtitle_config default, but app.py was using it.# I should probably add it back if I want to support it, but user said "PROHIBITED to remove existing ones".# I'll re-add 'uppercase': 1 if is_uppercase else 0 to the dict if the backend supports it, otherwise it's just ignored.# But wait, main_improved.py doesn't have 'uppercase' in get_subtitle_config.# I'll keep it in the dict just in case logic uses it elsewhere or if I missed it.# Actually, standard ASS doesn't support uppercase flag directly in Style, it needs to be text transform.# But I'll leave it in the dict.subtitle_config["uppercase"] = 1 if is_uppercase else 0subtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")try:with open(subtitle_config_path, "w", encoding="utf-8") as f:json.dump(subtitle_config, f, indent=4)cmd.extend(["--subtitle-config", subtitle_config_path])except Exception: passenv = os.environ.copy()env["PYTHONUNBUFFERED"] = "1"try:current_process = subprocess.Popen(cmd, cwd=WORKING_DIR, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True, env=env)logs = ""project_folder_path = Noneif input_source == "Existing Project" and project_name:# If using existing project, we already know the path, but let's see if logs confirm itproject_folder_path = os.path.join(VIRALS_DIR, project_name)while True:line = current_process.stdout.readline()if not line and current_process.poll() is not None:breakif line:logs += lineif "Project Folder:" in line:parts = line.split("Project Folder:")if len(parts) > 1: project_folder_path = parts[1].strip()yield logs, gr.update(visible=True, interactive=False), gr.update(visible=True), Noneexcept Exception as e:logs += f"\nError running process: {str(e)}\n"yield logs, gr.update(visible=True, interactive=False), gr.update(visible=True), Nonefinally:if current_process:if current_process.stdout:try:current_process.stdout.close()except Exception: passif current_process.poll() is None:# If we are here, it means we finished reading or errored out, but process is still running.# If it was a normal break from loop, process should be done or close to done.# If we are stopping, current_process.terminate() might be needed outside?# But here we just wait.try:current_process.wait()except Exception: passcurrent_process = None# Wait to ensure filesystem flushtime.sleep(1.0)html_output = ""if project_folder_path and os.path.exists(project_folder_path):html_output = library.generate_project_gallery(project_folder_path, is_full_path=True)else:html_output = f"<h3>{i18n('Error: Project folder could not be determined from logs.')}</h3>"yield logs, gr.update(value=i18n("Start Processing"), interactive=True), gr.update(visible=False), html_outputcss = """/* Global Dark Theme Overrides */body, .gradio-container {background-color: #0b0b0b !important;color: #ffffff !important;}/* Force dark background for specific inputs that might be white */input[type="password"], textarea, select {background-color: #1f1f1f !important;color: #ffffff !important;border: 1px solid #333 !important;}/* Hide Footer */footer {visibility: hidden}/* Container Width */.gradio-container {max-width: 98% !important;width: 98% !important;margin: 0 auto !important;}"""import headerwith gr.Blocks(title=i18n("ViralCutter WebUI"), theme=gr.themes.Default(primary_hue="orange", neutral_hue="slate"), css=css) as demo:gr.Markdown(header.badges)gr.Markdown(header.description)with gr.Tabs():with gr.Tab(i18n("Create New")):with gr.Row():with gr.Column(scale=1):input_source = gr.Radio([(i18n("YouTube URL"), "YouTube URL"), (i18n("Existing Project"), "Existing Project")], label=i18n("Input Source"), value="YouTube URL")url_input = gr.Textbox(label=i18n("YouTube URL"), placeholder="https://www.youtube.com/watch?v=...", visible=True)with gr.Row():video_quality_input = gr.Dropdown(choices=["best", "1080p", "720p", "480p"], label=i18n("Video Quality"), value="best")translate_input = gr.Dropdown(choices=["None", "pt", "en", "es", "fr", "de", "it", "ru", "ja", "ko", "zh-CN"], label=i18n("Translate Subtitles To"), value="None")use_youtube_subs_input = gr.Checkbox(label=i18n("Use YouTube Subs"), value=True, info=i18n("Download and use official subtitles if available. (Recommended, it speeds up the process)"))project_selector = gr.Dropdown(choices=[], label=i18n("Select Project"), visible=False)def on_source_change(source):if source == "YouTube URL":return gr.update(visible=True), gr.update(visible=False), gr.update(value="Full") # Reset to Full if URL is picked? Optional.else:# Load projectsprojs = library.get_existing_projects()return gr.update(visible=False), gr.update(choices=projs, visible=True), gr.update(value="Subtitles Only") # Auto-switch to Subs Onlywith gr.Row():segments_input = gr.Number(label=i18n("Segments"), value=3, precision=0)viral_input = gr.Checkbox(label=i18n("Viral Mode"), value=True)themes_input = gr.Textbox(label=i18n("Themes"), placeholder=i18n("funny, sad..."), visible=False)viral_input.change(lambda x: gr.update(visible=not x), viral_input, themes_input)with gr.Row():min_dur_input = gr.Number(label=i18n("Min Duration (s)"), value=15)max_dur_input = gr.Number(label=i18n("Max Duration (s)"), value=90)with gr.Column(scale=1):with gr.Row():ai_backend_input = gr.Dropdown(choices=[(i18n("Gemini"), "gemini"), (i18n("G4F"), "g4f"), (i18n("Local (GGUF)"), "local"), (i18n("Manual"), "manual")], label=i18n("AI Backend"), value="gemini", scale=2)api_key_input = gr.Textbox(label=i18n("Gemini API Key"), type="password", scale=3)# New Dynamic Inputswith gr.Row():ai_model_input = gr.Dropdown(choices=GEMINI_MODELS, label=i18n("AI Model"), value=GEMINI_MODELS[1], allow_custom_value=True, visible=True, scale=5)refresh_models_btn = gr.Button("🔄", size="sm", visible=False, scale=0, min_width=50) # Only localchunk_size_input = gr.Number(label=i18n("Chunk Size"), value=20000, precision=0, scale=2)# Update listeners with logic to hide/show API keydef update_ai_ui(backend):show_api = (backend == "gemini")show_refresh = (backend == "local")# Definições padrão para evitar que fiquem vaziosnew_choices = []new_val = ""new_chunk = 20000if backend == "gemini":new_choices = GEMINI_MODELSnew_val = GEMINI_MODELS[1]new_chunk = 20000elif backend == "g4f":new_choices = G4F_MODELSnew_val = G4F_MODELS[0]new_chunk = 3000elif backend == "local":models = get_local_models()new_choices = models if models else ["No models found"]new_val = new_choices[0]new_chunk = 15000else: # Manualpassreturn (gr.update(visible=show_api), # API Key Visibility (Fixes hole 1)gr.update(choices=new_choices, value=new_val, visible=(backend != "manual")), # Model Dropdowngr.update(visible=show_refresh), # Refresh Buttongr.update(value=new_chunk) # Chunk Size)def refresh_local_models():models = get_local_models()val = models[0] if models else "No models found"return gr.update(choices=models, value=val)refresh_models_btn.click(refresh_local_models, outputs=ai_model_input)ai_backend_input.change(update_ai_ui, inputs=ai_backend_input, outputs=[api_key_input, ai_model_input, refresh_models_btn, chunk_size_input])model_input = gr.Dropdown(["tiny", "small", "medium", "large", "large-v1", "large-v2", "large-v3", "turbo", "large-v3-turbo", "distil-large-v2", "distil-medium.en", "distil-small.en", "distil-large-v3"], label=i18n("Whisper Model"), value="small")with gr.Row():workflow_input = gr.Dropdown(choices=[(i18n("Full"), "Full"), (i18n("Cut Only"), "Cut Only"), (i18n("Subtitles Only"), "Subtitles Only")], label=i18n("Workflow"), value="Full")face_model_input = gr.Dropdown(["insightface", "mediapipe"], label=i18n("Face Model"), value="insightface")with gr.Row():face_mode_input = gr.Dropdown(choices=[(i18n("Auto"), "auto"), ("1", "1"), ("2", "2")], label=i18n("Face Mode"), value="auto")face_detect_interval_input = gr.Textbox(label=i18n("Face Det. Interval"), value="0.17,1.0")# Update listeners now that all components are definedinput_source.change(on_source_change, inputs=input_source, outputs=[url_input, project_selector, workflow_input])with gr.Accordion(i18n("Advanced Face Settings"), open=False):face_preset_input = gr.Dropdown(choices=list(FACE_PRESETS.keys()), label=i18n("Configuration Presets"), value="Default (Balanced)", interactive=True)with gr.Row():face_filter_thresh_input = gr.Slider(label=i18n("Ignore Small Faces (0.0 - 1.0)"), minimum=0.0, maximum=1.0, value=0.35, step=0.05, info=i18n("Relative size to ignore background."))face_two_thresh_input = gr.Slider(label=i18n("Threshold for 2 Faces (0.0 - 1.0)"), minimum=0.0, maximum=1.0, value=0.60, step=0.05, info=i18n("Size of 2nd face to activate split mode."))face_conf_thresh_input = gr.Slider(label=i18n("Minimum Confidence (0.0 - 1.0)"), minimum=0.0, maximum=1.0, value=0.40, step=0.05, info=i18n("Ignore detections with low confidence."))face_dead_zone_input = gr.Slider(label=i18n("Dead Zone (Stabilization)"), minimum=0, maximum=200, value=150, step=5, info=i18n("Movement pixels to ignore."))face_preset_input.change(apply_face_preset, inputs=face_preset_input, outputs=[face_filter_thresh_input, face_two_thresh_input, face_conf_thresh_input, face_dead_zone_input])with gr.Accordion(i18n("Experimental: Active Speaker & Motion"), open=False):experimental_preset_input = gr.Dropdown(choices=list(EXPERIMENTAL_PRESETS.keys()), label=i18n("Configuration Presets"), value="Default (Off)", interactive=True)focus_active_speaker_input = gr.Checkbox(label=i18n("Experimental: Focus on Speaker"), value=False, info=i18n("Tries to focus only on the speaking person instead of split screen."))with gr.Row():active_speaker_mar_input = gr.Slider(label=i18n("MAR Threshold (Mouth Open)"), minimum=0.01, maximum=0.20, value=0.03, step=0.005, info=i18n("Mouth open sensitivity."))active_speaker_score_diff_input = gr.Slider(label=i18n("Score Difference"), minimum=0.5, maximum=10.0, value=1.5, step=0.5, info=i18n("Minimum difference to focus on 1 face."))with gr.Row():include_motion_input = gr.Checkbox(label=i18n("Consider Motion"), value=False, info=i18n("Increases score with motion (gestures)."))with gr.Row():active_speaker_motion_threshold_input = gr.Slider(label=i18n("Motion Dead Zone"), minimum=0.0, maximum=20.0, value=3.0, step=0.5, info=i18n("Pixels ignored."))active_speaker_motion_sensitivity_input = gr.Slider(label=i18n("Motion Sensitivity"), minimum=0.01, maximum=0.5, value=0.05, step=0.01, info=i18n("Points per pixel."))active_speaker_decay_input = gr.Slider(label=i18n("Switch Speed"), minimum=0.5, maximum=5.0, value=2.0, step=0.5, info=i18n("Speed to lose focus."))experimental_preset_input.change(apply_experimental_preset, inputs=experimental_preset_input, outputs=[focus_active_speaker_input, active_speaker_mar_input, active_speaker_score_diff_input, include_motion_input, active_speaker_motion_threshold_input, active_speaker_motion_sensitivity_input, active_speaker_decay_input])with gr.Accordion(i18n("Subtitle Settings (alpha)"), open=False):preset_input = gr.Dropdown(choices=[(i18n("Manual"), "Manual")] + [(k, k) for k in subs.SUBTITLE_PRESETS.keys()], label=i18n("Quick Presets"), value="Hormozi (Classic)")use_custom_subs = gr.Checkbox(label=i18n("Enable Subtitle Customization (Includes Preset)"), value=True)# Previews (Always Visible)preview_html = gr.HTML(value=f"<div style='text-align:center; padding:10px; color:#666;'>{i18n('Select options or preset to preview')}</div>")with gr.Row():preview_vid_btn = gr.Button(i18n("🎬 Render Animated Preview (Slow)"), size="sm")preview_vid = gr.Video(label=i18n("Animated Preview"), height=300, autoplay=True, interactive=False)with gr.Accordion(i18n("Advanced Settings"), open=False):gr.Markdown(f"### {i18n('Appearance')}")with gr.Row():font_name_input = gr.Textbox(label=i18n("Font Name"), value="Montserrat-Regular")font_size_input = gr.Slider(label=i18n("Font Size (Base)"), minimum=8, maximum=80, value=12)highlight_size_input = gr.Slider(label=i18n("Highlight Size"), minimum=8, maximum=80, value=14)with gr.Row():font_color_input = gr.ColorPicker(label=i18n("Base Color"), value="#FFFFFF")highlight_color_input = gr.ColorPicker(label=i18n("Highlight Color"), value="#00FF00")outline_color_input = gr.ColorPicker(label=i18n("Outline Color"), value="#000000")shadow_color_input = gr.ColorPicker(label=i18n("Shadow Color"), value="#000000")gr.Markdown(f"### {i18n('Styling & Effects')}")with gr.Row():outline_thickness_input = gr.Slider(label=i18n("Outline Thickness"), minimum=0, maximum=10, value=1.5)shadow_size_input = gr.Slider(label=i18n("Shadow Size"), minimum=0, maximum=10, value=2)border_style_input = gr.Dropdown(choices=[(i18n("Outline"), 1), (i18n("Opaque Box"), 3)], label=i18n("Border Style"), value=1)with gr.Row():bold_input = gr.Checkbox(label=i18n("Bold"))italic_input = gr.Checkbox(label=i18n("Italic"))uppercase_input = gr.Checkbox(label=i18n("Uppercase"))remove_punc_input = gr.Checkbox(label=i18n("Remove Punctuation"), value=True)underline_input = gr.Checkbox(label=i18n("Underline"))strikeout_input = gr.Checkbox(label=i18n("Strikeout"))gr.Markdown(f"### {i18n('Positioning & Layout')}")with gr.Row():vertical_pos_input = gr.Slider(label=i18n("V-Pos (Margin V)"), minimum=0, maximum=500, value=210)alignment_input = gr.Dropdown(choices=[(i18n("Left"), 1), (i18n("Center"), 2), (i18n("Right"), 3)], label=i18n("Alignment"), value=2)gap_limit_input = gr.Slider(label=i18n("Gap Limit"), minimum=0.0, maximum=5.0, value=0.5, step=0.1)mode_input = gr.Dropdown(choices=[(i18n("Highlight"), "highlight"), (i18n("Word by Word"), "word_by_word"), (i18n("No Highlight"), "no_highlight")], label=i18n("Mode"), value="highlight")words_per_block_input = gr.Slider(label=i18n("Words per Block"), minimum=1, maximum=20, value=3, step=1)manual_inputs = [font_name_input, font_size_input, font_color_input, highlight_color_input,outline_color_input, outline_thickness_input, shadow_color_input, shadow_size_input,bold_input, italic_input, uppercase_input,highlight_size_input, words_per_block_input, gap_limit_input, mode_input,underline_input, strikeout_input, border_style_input,vertical_pos_input, alignment_input,remove_punc_input]# Update manual inputs when preset changespreset_input.change(subs.apply_preset, inputs=[preset_input], outputs=manual_inputs)# Auto-update PREVIEW HTML on any changefor inp in manual_inputs:inp.change(subs.generate_preview_html, inputs=manual_inputs, outputs=preview_html)# Render video buttonpreview_vid_btn.click(subs.render_preview_video,inputs=manual_inputs,outputs=preview_vid)# Initial loaddemo.load(subs.generate_preview_html, inputs=manual_inputs, outputs=preview_html)demo.load(subs.apply_preset, inputs=[preset_input], outputs=manual_inputs) # Apply default preset on loadwith gr.Row():start_btn = gr.Button(i18n("Start Processing"), variant="primary")stop_btn = gr.Button(i18n("Stop"), variant="stop", visible=False)stop_btn.click(kill_process, outputs=[])logs_output = gr.Textbox(label=i18n("Logs"), lines=10, autoscroll=True, elem_id="logs_output")# Force scroll to bottom via JSlogs_output.change(fn=None, inputs=[], outputs=[], js="""function() {var ta = document.querySelector('#logs_output textarea');if(ta) {ta.scrollTop = ta.scrollHeight;}}""")results_html = gr.HTML(label=i18n("Results"))# MUST pass all all new inputs to the run functionstart_btn.click(run_viral_cutter, inputs=[input_source, project_selector, url_input, segments_input, viral_input, themes_input, min_dur_input, max_dur_input,model_input, ai_backend_input, api_key_input, ai_model_input, chunk_size_input,workflow_input, face_model_input, face_mode_input, face_detect_interval_input,face_filter_thresh_input, face_two_thresh_input, face_conf_thresh_input, face_dead_zone_input, focus_active_speaker_input,active_speaker_mar_input, active_speaker_score_diff_input, include_motion_input, active_speaker_motion_threshold_input, active_speaker_motion_sensitivity_input, active_speaker_decay_input,use_custom_subs,# Expanded Manual Inputs mappingfont_name_input, font_size_input, font_color_input, highlight_color_input,outline_color_input, outline_thickness_input, shadow_color_input, shadow_size_input,bold_input, italic_input, uppercase_input, vertical_pos_input, alignment_input,# New Inputshighlight_size_input, words_per_block_input, gap_limit_input, mode_input,underline_input, strikeout_input, border_style_input, remove_punc_input,video_quality_input, use_youtube_subs_input, translate_input], outputs=[logs_output, start_btn, stop_btn, results_html])with gr.Tab(i18n("Subtitle Editor")):gr.Markdown(f"### {i18n('Edit Subtitles (Smart Mode)')}")with gr.Group():editor_project_dropdown = gr.Dropdown(choices=library.get_existing_projects(), label=i18n("Select Project"), value=None)editor_refresh_btn = gr.Button(i18n("Refresh"), size="sm")with gr.Group():editor_file_dropdown = gr.Dropdown(choices=[], label=i18n("Select Subtitle File"), interactive=True)editor_load_btn = gr.Button(i18n("Load Subtitles"), variant="secondary")# Hidden state to store full path of currently loaded JSONcurrent_json_path = gr.State()# The Dataframe Editor# Headers: Start, End, Textsubtitle_dataframe = gr.Dataframe(headers=["Start", "End", "Text"],datatype=["str", "str", "str"],col_count=(3, "fixed"),interactive=True,label=i18n("Subtitle Segments"),wrap=True)with gr.Row():editor_save_btn = gr.Button(i18n("💾 Save Changes"), variant="primary")editor_render_single_btn = gr.Button(i18n("⚡ Render This Segment (Very-Fast)"), variant="secondary")editor_render_all_btn = gr.Button(i18n("🎬 Render All (Fast)"), variant="stop")editor_status = gr.Textbox(label=i18n("Status"), interactive=False)# --- Callbacks for Editor ---editor_refresh_btn.click(library.refresh_projects, outputs=editor_project_dropdown)def update_file_list(proj_name):if not proj_name: return gr.update(choices=[])proj_path = os.path.join(VIRALS_DIR, proj_name)files = editor.list_editable_files(proj_path)return gr.update(choices=files, value=files[0] if files else None)editor_project_dropdown.change(update_file_list, inputs=editor_project_dropdown, outputs=editor_file_dropdown)def load_subs(proj_name, file_name):if not proj_name or not file_name:return [], None, i18n("Please select project and file.")full_path = os.path.join(VIRALS_DIR, proj_name, 'subs', file_name)data = editor.load_transcription_for_editor(full_path)return data, full_path, i18n("Loaded {} segments.").format(len(data))editor_load_btn.click(load_subs, inputs=[editor_project_dropdown, editor_file_dropdown], outputs=[subtitle_dataframe, current_json_path, editor_status])def save_subs(json_path, df):if not json_path: return i18n("No file loaded.")data_list = df.values.tolist() if hasattr(df, 'values') else dfmsg = editor.save_editor_changes(json_path, data_list)return msgeditor_save_btn.click(save_subs, inputs=[current_json_path, subtitle_dataframe], outputs=editor_status)def render_single(json_path, use_custom, font_name, font_size, font_color, highlight_color,outline_color, outline_thickness, shadow_color, shadow_size,is_bold, is_italic, is_uppercase,h_size, w_block, gap, mode, under, strike, border_s,vertical_pos, alignment, remove_punc):if not json_path: return i18n("No file loaded.")subtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")# Save config if custom subs enabledif use_custom:subtitle_config = {"font": font_name, "base_size": int(font_size),"base_color": convert_color_to_ass(font_color),"highlight_color": convert_color_to_ass(highlight_color),"outline_color": convert_color_to_ass(outline_color),"outline_thickness": outline_thickness,"shadow_color": convert_color_to_ass(shadow_color),"shadow_size": shadow_size, "vertical_position": vertical_pos,"alignment": alignment, "bold": 1 if is_bold else 0,"italic": 1 if is_italic else 0,"underline": 1 if under else 0, "strikeout": 1 if strike else 0,"border_style": border_s, "words_per_block": int(w_block),"gap_limit": gap, "mode": mode, "highlight_size": int(h_size),"uppercase": 1 if is_uppercase else 0,"remove_punctuation": remove_punc}try:with open(subtitle_config_path, "w", encoding="utf-8") as f:json.dump(subtitle_config, f, indent=4)except Exception: passelse:# Remove temp config if it exists to ensure defaults are usedtry:if os.path.exists(subtitle_config_path):os.remove(subtitle_config_path)except Exception: pass# We expect user to SAVE first, but we could auto-save.# For now assume saved.msg = editor.render_specific_video(json_path)return msgeditor_render_single_btn.click(render_single,inputs=[current_json_path, use_custom_subs] + manual_inputs,outputs=editor_status)def render_all(proj_name, use_custom, font_name, font_size, font_color, highlight_color,outline_color, outline_thickness, shadow_color, shadow_size,is_bold, is_italic, is_uppercase,h_size, w_block, gap, mode, under, strike, border_s,vertical_pos, alignment, remove_punc):if not proj_name: return i18n("No project selected.")# Save configif use_custom:subtitle_config = {"font": font_name, "base_size": int(font_size),"base_color": convert_color_to_ass(font_color),"highlight_color": convert_color_to_ass(highlight_color),"outline_color": convert_color_to_ass(outline_color),"outline_thickness": outline_thickness,"shadow_color": convert_color_to_ass(shadow_color),"shadow_size": shadow_size, "vertical_position": vertical_pos,"alignment": alignment, "bold": 1 if is_bold else 0,"italic": 1 if is_italic else 0,"underline": 1 if under else 0, "strikeout": 1 if strike else 0,"border_style": border_s, "words_per_block": int(w_block),"gap_limit": gap, "mode": mode, "highlight_size": int(h_size),"uppercase": 1 if is_uppercase else 0,"remove_punctuation": remove_punc}subtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")try:with open(subtitle_config_path, "w", encoding="utf-8") as f:json.dump(subtitle_config, f, indent=4)except Exception: passproj_path = os.path.join(VIRALS_DIR, proj_name)# IMPORTANT: Pass the config file path to the commandsubtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")cmd = [sys.executable, MAIN_SCRIPT_PATH, "--project-path", proj_path, "--workflow", "3", "--skip-prompts"]if use_custom and os.path.exists(subtitle_config_path):cmd.extend(["--subtitle-config", subtitle_config_path])try:subprocess.Popen(cmd, cwd=WORKING_DIR)return i18n("Render All started in background... Check terminal/logs.")except Exception as e:return f"Error starting render: {e}"editor_render_all_btn.click(render_all,inputs=[editor_project_dropdown, use_custom_subs] + manual_inputs,outputs=editor_status)with gr.Tab(i18n("Library")):gr.Markdown(f"### {i18n('Existing Projects')}")with gr.Row():project_dropdown = gr.Dropdown(choices=library.get_existing_projects(), label=i18n("Select Project"), value=None)refresh_btn = gr.Button(i18n("Refresh List"))project_gallery_html = gr.HTML()refresh_btn.click(library.refresh_projects, outputs=project_dropdown)def on_select_project(proj_name): return library.generate_project_gallery(proj_name)project_dropdown.change(on_select_project, project_dropdown, project_gallery_html)gr.Markdown(f"""<hr><div style='text-align: center; font-size: 0.9em; color: #777;'><p><strong>{i18n('Desenvolvido por Rafael Godoy')}</strong><br>{i18n('Apoie o projeto, qualquer valor é bem-vindo:')}<a href='https://nubank.com.br/pagar/1ls6a4/0QpSSbWBSq' target='_blank'><strong>{i18n('Apoiar via PIX')}</strong></a></p></div>""")if __name__ == "__main__":import webbrowserimport threadingimport timeimport argparseparser = argparse.ArgumentParser()parser.add_argument("--colab", action="store_true", help="Run in Google Colab mode")args = parser.parse_args()if args.colab:print("Running in Colab mode. Generating public link with Static Mounts...")library.set_url_mode("fastapi")# Broaden allowed paths for Colaballowed_dirs = [VIRALS_DIR, WORKING_DIR, os.getcwd(), "."]# Explicitly set static pathstry:gr.set_static_paths(paths=allowed_dirs)print(f"DEBUG: Registered static paths: {allowed_dirs}")except AttributeError:print("DEBUG: gr.set_static_paths not available")print(f"DEBUG: Allowed paths for Gradio: {allowed_dirs}")# Launch with prevent_thread_lock to allow mountingapp, local_url, share_url = demo.queue().launch(share=True,allowed_paths=allowed_dirs,prevent_thread_lock=True)# Mount the VIRALS directory explicitlyapp.mount("/virals", StaticFiles(directory=VIRALS_DIR), name="virals")print(f"Mounted /virals to {VIRALS_DIR}")demo.block_thread()else:print("Running in Local mode (FastAPI + Uvicorn) with Share enabled (Mounting StaticFiles).")library.set_url_mode("fastapi")# Broaden allowed paths (good practice even if using mount)allowed_dirs = [VIRALS_DIR, WORKING_DIR, os.getcwd(), "."]try:gr.set_static_paths(paths=allowed_dirs)except AttributeError:pass# Create explicit FastAPI appapp = FastAPI()# Mount the VIRALS directory to /virals using standard FastAPI StaticFilesapp.mount("/virals", StaticFiles(directory=VIRALS_DIR), name="virals")from fastapi.responses import FileResponsefrom fastapi import BackgroundTasks@app.get("/export_xml_api")def export_xml_api(project: str, segment: int, background_tasks: BackgroundTasks, format: str = "premiere"):"""Trigger XML export for a segment and return the file."""try:project_path = os.path.join(VIRALS_DIR, project)script_path = os.path.join(WORKING_DIR, "scripts", "export_xml.py")# Run script synchronously (threadpool)cmd = [sys.executable, script_path, "--project", project_path, "--segment", str(segment), "--format", format]subprocess.run(cmd, check=True)# New Logic: Script now creates a ZIP file named export_PROJECT_segX.zipproj_name = os.path.basename(project_path)zip_filename = f"export_{proj_name}_seg{segment}.zip"file_path = os.path.join(project_path, zip_filename)if os.path.exists(file_path):return FileResponse(file_path, filename=zip_filename, media_type='application/zip')else:return {"error": f"File generation failed. Expected: {file_path}"}except Exception as e:return {"error": str(e)}print(f"Mounted /virals to {VIRALS_DIR}")# Mount Gradio ON TOP of FastAPI# path="/" means Gradio handles the root# Disable SSR to prevent Node proxying issues on HF Spaces (port 7861 errors)app = gr.mount_gradio_app(app, demo.queue(), path="/", allowed_paths=allowed_dirs, ssr_mode=False)# Run with Uvicorn# This gives Uvicorn control over the main thread and Loop, avoiding blocking/GC issuesuvicorn.run(app, host="0.0.0.0", port=7860)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841import gradio as grimport subprocessimport osimport sysimport jsonimport psutilimport shutilimport datetimeimport timeimport urllib.parsefrom fastapi import FastAPIfrom fastapi.staticfiles import StaticFilesimport uvicornimport reimport library # Module for Library Logicimport subtitle_handler as subs # Module for Subtitlesimport subtitle_editor as editor # Module for Editor Logic# Path to the main scriptMAIN_SCRIPT_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "main_improved.py")WORKING_DIR = os.path.dirname(MAIN_SCRIPT_PATH)sys.path.append(WORKING_DIR)from i18n.i18n import I18nAutoi18n = I18nAuto()# --- PRESETS DEFINITIONS ---FACE_PRESETS = {"Default (Balanced)": {"thresh": 0.35, "two_face": 0.60, "conf": 0.40, "dead_zone": 150},"Stable (Focus Main)": {"thresh": 0.60, "two_face": 0.80, "conf": 0.60, "dead_zone": 200},"Sensitive (Catch All)": {"thresh": 0.10, "two_face": 0.40, "conf": 0.30, "dead_zone": 100},"High Precision": {"thresh": 0.40, "two_face": 0.65, "conf": 0.75, "dead_zone": 150},}EXPERIMENTAL_PRESETS = {"Default (Off)": {"focus": False, "mar": 0.03, "score": 1.5, "motion": False, "motion_th": 3.0, "motion_sens": 0.05, "decay": 2.0},"Active Speaker (Balanced)": {"focus": True, "mar": 0.03, "score": 1.5, "motion": True, "motion_th": 3.0, "motion_sens": 0.05, "decay": 2.0},"Active Speaker (Sensitive)": {"focus": True, "mar": 0.02, "score": 1.0, "motion": True, "motion_th": 2.0, "motion_sens": 0.10, "decay": 1.0},"Active Speaker (Stable)": {"focus": True, "mar": 0.05, "score": 2.5, "motion": False, "motion_th": 5.0, "motion_sens": 0.02, "decay": 3.0},}# ---------------------------VIRALS_DIR = os.path.join(WORKING_DIR, "VIRALS")MODELS_DIR = os.path.join(WORKING_DIR, "models")# Ensure directories existif not os.path.exists(VIRALS_DIR):os.makedirs(VIRALS_DIR, exist_ok=True)if not os.path.exists(MODELS_DIR):os.makedirs(MODELS_DIR, exist_ok=True)# Global variablescurrent_process = None# Helpersdef convert_color_to_ass(hex_color, alpha="00"):if not hex_color or not hex_color.startswith("#"):return f"&H{alpha}FFFFFF&"hex_clean = hex_color.lstrip('#')if len(hex_clean) == 6:r = hex_clean[0:2]g = hex_clean[2:4]b = hex_clean[4:6]return f"&H{alpha}{b}{g}{r}&"return f"&H{alpha}FFFFFF&"def kill_process():global current_processif current_process:try:parent = psutil.Process(current_process.pid)for child in parent.children(recursive=True):child.kill()parent.kill()current_process = Nonereturn i18n("Process terminated.")except Exception as e:return i18n("Error terminating process: {}").format(e)return i18n("No process running.")GEMINI_MODELS = ['gemini-3-pro-preview','gemini-2.5-flash','gemini-2.5-flash-preview-09-2025','gemini-2.5-flash-lite','gemini-2.5-flash-lite-preview-09-2025','gemini-2.5-pro','gemini-2.0-flash','gemini-2.0-flash-lite']G4F_MODELS = ['gpt-4o','gpt-4o-mini','gpt-4','o1-mini','o1','deepseek-r1','deepseek-v3','llama-3.3-70b','llama-3.1-405b','claude-3.5-sonnet','claude-3.7-sonnet','gemini-2.0-flash','qwen-2.5-72b']def get_local_models():if not os.path.exists(MODELS_DIR): return []return [f for f in os.listdir(MODELS_DIR) if f.endswith(".gguf")]def apply_face_preset(preset_name):if preset_name not in FACE_PRESETS:return [gr.update() for _ in range(4)] # No changep = FACE_PRESETS[preset_name]return p["thresh"], p["two_face"], p["conf"], p["dead_zone"]def apply_experimental_preset(preset_name):if preset_name not in EXPERIMENTAL_PRESETS:return [gr.update() for _ in range(7)] # No changep = EXPERIMENTAL_PRESETS[preset_name]return p["focus"], p["mar"], p["score"], p["motion"], p["motion_th"], p["motion_sens"], p["decay"]# Subtitle logic moved to subtitle_handler.pydef run_viral_cutter(input_source, project_name, url, video_file, segments, viral, themes, min_duration, max_duration, model, ai_backend, api_key, ai_model_name, chunk_size, workflow, face_model, face_mode, face_detect_interval,face_filter_thresh, face_two_thresh, face_conf_thresh, face_dead_zone, focus_active_speaker, active_speaker_mar, active_speaker_score_diff, include_motion, active_speaker_motion_threshold, active_speaker_motion_sensitivity, active_speaker_decay,use_custom_subs, font_name, font_size, font_color, highlight_color, outline_color, outline_thickness, shadow_color, shadow_size, is_bold, is_italic, is_uppercase, vertical_pos, alignment,h_size, w_block, gap, mode, under, strike, border_s, remove_punc, video_quality, use_youtube_subs, translate_target):global current_processyield "", gr.update(value=i18n("Running..."), interactive=False), gr.update(visible=True), Nonecmd = [sys.executable, MAIN_SCRIPT_PATH]# Input Source Logicif input_source == "Existing Project":if not project_name:yield i18n("Error: No project selected."), gr.update(value=i18n("Start Processing"), interactive=True), gr.update(visible=False), Nonereturnfull_project_path = os.path.join(VIRALS_DIR, project_name)cmd.extend(["--project-path", full_project_path])elif input_source == "Upload Video":if not video_file:yield i18n("Error: No video file uploaded."), gr.update(value=i18n("Start Processing"), interactive=True), gr.update(visible=False), Nonereturn# Create new project for uploadtimestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")project_name_upload = f"Upload_{timestamp}"project_path = os.path.join(VIRALS_DIR, project_name_upload)os.makedirs(project_path, exist_ok=True)target_path = os.path.join(project_path, "input.mp4")shutil.copy(video_file, target_path)cmd.extend(["--project-path", project_path])# Skip YouTube subs as it is a local uploadcmd.append("--skip-youtube-subs")else:if url: cmd.extend(["--url", url])# Pass Video Qualityif video_quality: cmd.extend(["--video-quality", video_quality])# Pass Subtitle Option (if False, we skip)if not use_youtube_subs: cmd.append("--skip-youtube-subs")# Translationif translate_target and translate_target != "None":cmd.extend(["--translate-target", translate_target])cmd.extend(["--segments", str(int(segments))])if viral: cmd.append("--viral")if themes: cmd.extend(["--themes", themes])cmd.extend(["--min-duration", str(int(min_duration))])cmd.extend(["--max-duration", str(int(max_duration))])cmd.extend(["--model", model])cmd.extend(["--ai-backend", ai_backend])if api_key: cmd.extend(["--api-key", api_key])# New AI Paramsif ai_model_name: cmd.extend(["--ai-model-name", str(ai_model_name)])if chunk_size: cmd.extend(["--chunk-size", str(int(chunk_size))])workflow_map = {"Full": "1", "Cut Only": "2", "Subtitles Only": "3"}cmd.extend(["--workflow", workflow_map.get(workflow, "1")])cmd.extend(["--face-model", face_model])cmd.extend(["--face-mode", face_mode])if face_detect_interval: cmd.extend(["--face-detect-interval", str(face_detect_interval)])# New Face Paramsif face_filter_thresh is not None: cmd.extend(["--face-filter-threshold", str(face_filter_thresh)])if face_two_thresh is not None: cmd.extend(["--face-two-threshold", str(face_two_thresh)])if face_conf_thresh is not None: cmd.extend(["--face-confidence-threshold", str(face_conf_thresh)])if face_dead_zone is not None: cmd.extend(["--face-dead-zone", str(face_dead_zone)])cmd.append("--skip-prompts")if focus_active_speaker:cmd.append("--focus-active-speaker")if active_speaker_mar is not None: cmd.extend(["--active-speaker-mar", str(active_speaker_mar)])if active_speaker_score_diff is not None: cmd.extend(["--active-speaker-score-diff", str(active_speaker_score_diff)])if include_motion: cmd.append("--include-motion")if active_speaker_motion_threshold is not None: cmd.extend(["--active-speaker-motion-threshold", str(active_speaker_motion_threshold)])if active_speaker_motion_sensitivity is not None: cmd.extend(["--active-speaker-motion-sensitivity", str(active_speaker_motion_sensitivity)])if active_speaker_decay is not None: cmd.extend(["--active-speaker-decay", str(active_speaker_decay)])cmd.append("--skip-prompts") # Always skip prompts in WebUI to prevent freezingif use_custom_subs:subtitle_config = {"font": font_name, "base_size": int(font_size), "base_color": convert_color_to_ass(font_color), "highlight_color": convert_color_to_ass(highlight_color),"outline_color": convert_color_to_ass(outline_color), "outline_thickness": outline_thickness, "shadow_color": convert_color_to_ass(shadow_color),"shadow_size": shadow_size, "vertical_position": vertical_pos, "alignment": alignment, "bold": 1 if is_bold else 0, "italic": 1 if is_italic else 0,"underline": 1 if under else 0, "strikeout": 1 if strike else 0, "border_style": border_s, "words_per_block": int(w_block), "gap_limit": gap,"mode": mode, "highlight_size": int(h_size), "remove_punctuation": remove_punc}# Uppercase is handled in main script or logic?# Actually subtitle_config doesn't seem to natively support "uppercase" in get_subtitle_config default, but app.py was using it.# I should probably add it back if I want to support it, but user said "PROHIBITED to remove existing ones".# I'll re-add 'uppercase': 1 if is_uppercase else 0 to the dict if the backend supports it, otherwise it's just ignored.# But wait, main_improved.py doesn't have 'uppercase' in get_subtitle_config.# I'll keep it in the dict just in case logic uses it elsewhere or if I missed it.# Actually, standard ASS doesn't support uppercase flag directly in Style, it needs to be text transform.# But I'll leave it in the dict.subtitle_config["uppercase"] = 1 if is_uppercase else 0subtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")try:with open(subtitle_config_path, "w", encoding="utf-8") as f:json.dump(subtitle_config, f, indent=4)cmd.extend(["--subtitle-config", subtitle_config_path])except Exception: passenv = os.environ.copy()env["PYTHONUNBUFFERED"] = "1"try:current_process = subprocess.Popen(cmd, cwd=WORKING_DIR, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True, env=env)logs = ""project_folder_path = Noneif input_source == "Existing Project" and project_name:# If using existing project, we already know the path, but let's see if logs confirm itproject_folder_path = os.path.join(VIRALS_DIR, project_name)while True:line = current_process.stdout.readline()if not line and current_process.poll() is not None:breakif line:logs += lineif "Project Folder:" in line:parts = line.split("Project Folder:")if len(parts) > 1: project_folder_path = parts[1].strip()yield logs, gr.update(visible=True, interactive=False), gr.update(visible=True), Noneexcept Exception as e:logs += f"\nError running process: {str(e)}\n"yield logs, gr.update(visible=True, interactive=False), gr.update(visible=True), Nonefinally:if current_process:if current_process.stdout:try:current_process.stdout.close()except Exception: passif current_process.poll() is None:# If we are here, it means we finished reading or errored out, but process is still running.# If it was a normal break from loop, process should be done or close to done.# If we are stopping, current_process.terminate() might be needed outside?# But here we just wait.try:current_process.wait()except Exception: passcurrent_process = None# Wait to ensure filesystem flushtime.sleep(1.0)html_output = ""if project_folder_path and os.path.exists(project_folder_path):html_output = library.generate_project_gallery(project_folder_path, is_full_path=True)else:html_output = f"<h3>{i18n('Error: Project folder could not be determined from logs.')}</h3>"yield logs, gr.update(value=i18n("Start Processing"), interactive=True), gr.update(visible=False), html_outputcss = """/* Global Dark Theme Overrides */body, .gradio-container {background-color: #0b0b0b !important;color: #ffffff !important;}/* Force dark background for specific inputs that might be white */input[type="password"], textarea, select {background-color: #1f1f1f !important;color: #ffffff !important;border: 1px solid #333 !important;}/* Hide Footer */footer {visibility: hidden}/* Container Width */.gradio-container {max-width: 98% !important;width: 98% !important;margin: 0 auto !important;}"""import headerwith gr.Blocks(title=i18n("ViralCutter WebUI"), theme=gr.themes.Default(primary_hue="orange", neutral_hue="slate"), css=css) as demo:gr.Markdown(header.badges)gr.Markdown(header.description)with gr.Tabs():with gr.Tab(i18n("Create New")):with gr.Row():with gr.Column(scale=1):input_source = gr.Radio([(i18n("YouTube URL"), "YouTube URL"), (i18n("Existing Project"), "Existing Project"), (i18n("Upload Video"), "Upload Video")], label=i18n("Input Source"), value="YouTube URL")url_input = gr.Textbox(label=i18n("YouTube URL"), placeholder="https://www.youtube.com/watch?v=...", visible=True)video_upload = gr.File(label=i18n("Upload Video"), file_count="single", file_types=["video"], visible=False)with gr.Row():video_quality_input = gr.Dropdown(choices=["best", "1080p", "720p", "480p"], label=i18n("Video Quality"), value="best")translate_input = gr.Dropdown(choices=["None", "pt", "en", "es", "fr", "de", "it", "ru", "ja", "ko", "zh-CN"], label=i18n("Translate Subtitles To"), value="None")use_youtube_subs_input = gr.Checkbox(label=i18n("Use YouTube Subs"), value=True, info=i18n("Download and use official subtitles if available. (Recommended, it speeds up the process)"))project_selector = gr.Dropdown(choices=[], label=i18n("Select Project"), visible=False)def on_source_change(source):if source == "YouTube URL":return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(value="Full")elif source == "Upload Video":return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(value="Full")else:# Load projectsprojs = library.get_existing_projects()return gr.update(visible=False), gr.update(choices=projs, visible=True), gr.update(visible=False), gr.update(value="Subtitles Only")with gr.Row():segments_input = gr.Number(label=i18n("Segments"), value=3, precision=0)viral_input = gr.Checkbox(label=i18n("Viral Mode"), value=True)themes_input = gr.Textbox(label=i18n("Themes"), placeholder=i18n("funny, sad..."), visible=False)viral_input.change(lambda x: gr.update(visible=not x), viral_input, themes_input)with gr.Row():min_dur_input = gr.Number(label=i18n("Min Duration (s)"), value=15)max_dur_input = gr.Number(label=i18n("Max Duration (s)"), value=90)with gr.Column(scale=1):with gr.Row():ai_backend_input = gr.Dropdown(choices=[(i18n("Gemini"), "gemini"), (i18n("G4F"), "g4f"), (i18n("Local (GGUF)"), "local"), (i18n("Manual"), "manual")], label=i18n("AI Backend"), value="gemini", scale=2)api_key_input = gr.Textbox(label=i18n("Gemini API Key"), type="password", scale=3)# New Dynamic Inputswith gr.Row():ai_model_input = gr.Dropdown(choices=GEMINI_MODELS, label=i18n("AI Model"), value=GEMINI_MODELS[1], allow_custom_value=True, visible=True, scale=5)refresh_models_btn = gr.Button("🔄", size="sm", visible=False, scale=0, min_width=50) # Only localchunk_size_input = gr.Number(label=i18n("Chunk Size"), value=20000, precision=0, scale=2)# Update listeners with logic to hide/show API keydef update_ai_ui(backend):show_api = (backend == "gemini")show_refresh = (backend == "local")# Definições padrão para evitar que fiquem vaziosnew_choices = []new_val = ""new_chunk = 20000if backend == "gemini":new_choices = GEMINI_MODELSnew_val = GEMINI_MODELS[1]new_chunk = 20000elif backend == "g4f":new_choices = G4F_MODELSnew_val = G4F_MODELS[0]new_chunk = 3000elif backend == "local":models = get_local_models()new_choices = models if models else [i18n("No models found")]new_val = new_choices[0]new_chunk = 15000else: # Manualpassreturn (gr.update(visible=show_api), # API Key Visibility (Fixes hole 1)gr.update(choices=new_choices, value=new_val, visible=(backend != "manual")), # Model Dropdowngr.update(visible=show_refresh), # Refresh Buttongr.update(value=new_chunk) # Chunk Size)def refresh_local_models():models = get_local_models()val = models[0] if models else i18n("No models found")return gr.update(choices=models, value=val)refresh_models_btn.click(refresh_local_models, outputs=ai_model_input)ai_backend_input.change(update_ai_ui, inputs=ai_backend_input, outputs=[api_key_input, ai_model_input, refresh_models_btn, chunk_size_input])model_input = gr.Dropdown(["tiny", "small", "medium", "large", "large-v1", "large-v2", "large-v3", "turbo", "large-v3-turbo", "distil-large-v2", "distil-medium.en", "distil-small.en", "distil-large-v3"], label=i18n("Whisper Model"), value="large-v3-turbo")with gr.Row():workflow_input = gr.Dropdown(choices=[(i18n("Full"), "Full"), (i18n("Cut Only"), "Cut Only"), (i18n("Subtitles Only"), "Subtitles Only")], label=i18n("Workflow"), value="Full")face_model_input = gr.Dropdown(["insightface", "mediapipe"], label=i18n("Face Model"), value="insightface")with gr.Row():face_mode_input = gr.Dropdown(choices=[(i18n("Auto"), "auto"), ("1", "1"), ("2", "2")], label=i18n("Face Mode"), value="auto")face_detect_interval_input = gr.Textbox(label=i18n("Face Det. Interval"), value="0.17,1.0")# Update listeners now that all components are definedinput_source.change(on_source_change, inputs=input_source, outputs=[url_input, project_selector, video_upload, workflow_input])with gr.Accordion(i18n("Advanced Face Settings"), open=False):face_preset_input = gr.Dropdown(choices=[(i18n(k), k) for k in FACE_PRESETS.keys()], label=i18n("Configuration Presets"), value="Default (Balanced)", interactive=True)with gr.Row():face_filter_thresh_input = gr.Slider(label=i18n("Ignore Small Faces (0.0 - 1.0)"), minimum=0.0, maximum=1.0, value=0.35, step=0.05, info=i18n("Relative size to ignore background."))face_two_thresh_input = gr.Slider(label=i18n("Threshold for 2 Faces (0.0 - 1.0)"), minimum=0.0, maximum=1.0, value=0.60, step=0.05, info=i18n("Size of 2nd face to activate split mode."))face_conf_thresh_input = gr.Slider(label=i18n("Minimum Confidence (0.0 - 1.0)"), minimum=0.0, maximum=1.0, value=0.40, step=0.05, info=i18n("Ignore detections with low confidence."))face_dead_zone_input = gr.Slider(label=i18n("Dead Zone (Stabilization)"), minimum=0, maximum=200, value=150, step=5, info=i18n("Movement pixels to ignore."))face_preset_input.change(apply_face_preset, inputs=face_preset_input, outputs=[face_filter_thresh_input, face_two_thresh_input, face_conf_thresh_input, face_dead_zone_input])with gr.Accordion(i18n("Experimental: Active Speaker & Motion"), open=False):experimental_preset_input = gr.Dropdown(choices=[(i18n(k), k) for k in EXPERIMENTAL_PRESETS.keys()], label=i18n("Configuration Presets"), value="Default (Off)", interactive=True)focus_active_speaker_input = gr.Checkbox(label=i18n("Experimental: Focus on Speaker"), value=False, info=i18n("Tries to focus only on the speaking person instead of split screen."))with gr.Row():active_speaker_mar_input = gr.Slider(label=i18n("MAR Threshold (Mouth Open)"), minimum=0.01, maximum=0.20, value=0.03, step=0.005, info=i18n("Mouth open sensitivity."))active_speaker_score_diff_input = gr.Slider(label=i18n("Score Difference"), minimum=0.5, maximum=10.0, value=1.5, step=0.5, info=i18n("Minimum difference to focus on 1 face."))with gr.Row():include_motion_input = gr.Checkbox(label=i18n("Consider Motion"), value=False, info=i18n("Increases score with motion (gestures)."))with gr.Row():active_speaker_motion_threshold_input = gr.Slider(label=i18n("Motion Dead Zone"), minimum=0.0, maximum=20.0, value=3.0, step=0.5, info=i18n("Pixels ignored."))active_speaker_motion_sensitivity_input = gr.Slider(label=i18n("Motion Sensitivity"), minimum=0.01, maximum=0.5, value=0.05, step=0.01, info=i18n("Points per pixel."))active_speaker_decay_input = gr.Slider(label=i18n("Switch Speed"), minimum=0.5, maximum=5.0, value=2.0, step=0.5, info=i18n("Speed to lose focus."))experimental_preset_input.change(apply_experimental_preset, inputs=experimental_preset_input, outputs=[focus_active_speaker_input, active_speaker_mar_input, active_speaker_score_diff_input, include_motion_input, active_speaker_motion_threshold_input, active_speaker_motion_sensitivity_input, active_speaker_decay_input])with gr.Accordion(i18n("Subtitle Settings (alpha)"), open=False):preset_input = gr.Dropdown(choices=[(i18n("Manual"), "Manual")] + [(i18n(k), k) for k in subs.SUBTITLE_PRESETS.keys()], label=i18n("Quick Presets"), value="Hormozi (Classic)")use_custom_subs = gr.Checkbox(label=i18n("Enable Subtitle Customization (Includes Preset)"), value=True)# Previews (Always Visible)preview_html = gr.HTML(value=f"<div style='text-align:center; padding:10px; color:#666;'>{i18n('Select options or preset to preview')}</div>")with gr.Row():preview_vid_btn = gr.Button(i18n("🎬 Render Animated Preview (Slow)"), size="sm")preview_vid = gr.Video(label=i18n("Animated Preview"), height=300, autoplay=True, interactive=False)with gr.Accordion(i18n("Advanced Settings"), open=False):gr.Markdown(f"### {i18n('Appearance')}")with gr.Row():font_name_input = gr.Textbox(label=i18n("Font Name"), value="Montserrat-Regular")font_size_input = gr.Slider(label=i18n("Font Size (Base)"), minimum=8, maximum=80, value=12)highlight_size_input = gr.Slider(label=i18n("Highlight Size"), minimum=8, maximum=80, value=14)with gr.Row():font_color_input = gr.ColorPicker(label=i18n("Base Color"), value="#FFFFFF")highlight_color_input = gr.ColorPicker(label=i18n("Highlight Color"), value="#00FF00")outline_color_input = gr.ColorPicker(label=i18n("Outline Color"), value="#000000")shadow_color_input = gr.ColorPicker(label=i18n("Shadow Color"), value="#000000")gr.Markdown(f"### {i18n('Styling & Effects')}")with gr.Row():outline_thickness_input = gr.Slider(label=i18n("Outline Thickness"), minimum=0, maximum=10, value=1.5)shadow_size_input = gr.Slider(label=i18n("Shadow Size"), minimum=0, maximum=10, value=2)border_style_input = gr.Dropdown(choices=[(i18n("Outline"), 1), (i18n("Opaque Box"), 3)], label=i18n("Border Style"), value=1)with gr.Row():bold_input = gr.Checkbox(label=i18n("Bold"))italic_input = gr.Checkbox(label=i18n("Italic"))uppercase_input = gr.Checkbox(label=i18n("Uppercase"))remove_punc_input = gr.Checkbox(label=i18n("Remove Punctuation"), value=True)underline_input = gr.Checkbox(label=i18n("Underline"))strikeout_input = gr.Checkbox(label=i18n("Strikeout"))gr.Markdown(f"### {i18n('Positioning & Layout')}")with gr.Row():vertical_pos_input = gr.Slider(label=i18n("V-Pos (Margin V)"), minimum=0, maximum=500, value=210)alignment_input = gr.Dropdown(choices=[(i18n("Left"), 1), (i18n("Center"), 2), (i18n("Right"), 3)], label=i18n("Alignment"), value=2)gap_limit_input = gr.Slider(label=i18n("Gap Limit"), minimum=0.0, maximum=5.0, value=0.5, step=0.1)mode_input = gr.Dropdown(choices=[(i18n("Highlight"), "highlight"), (i18n("Word by Word"), "word_by_word"), (i18n("No Highlight"), "no_highlight")], label=i18n("Mode"), value="highlight")words_per_block_input = gr.Slider(label=i18n("Words per Block"), minimum=1, maximum=20, value=3, step=1)manual_inputs = [font_name_input, font_size_input, font_color_input, highlight_color_input,outline_color_input, outline_thickness_input, shadow_color_input, shadow_size_input,bold_input, italic_input, uppercase_input,highlight_size_input, words_per_block_input, gap_limit_input, mode_input,underline_input, strikeout_input, border_style_input,vertical_pos_input, alignment_input,remove_punc_input]# Update manual inputs when preset changespreset_input.change(subs.apply_preset, inputs=[preset_input], outputs=manual_inputs)# Auto-update PREVIEW HTML on any changefor inp in manual_inputs:inp.change(subs.generate_preview_html, inputs=manual_inputs, outputs=preview_html)# Render video buttonpreview_vid_btn.click(subs.render_preview_video,inputs=manual_inputs,outputs=preview_vid)# Initial loaddemo.load(subs.generate_preview_html, inputs=manual_inputs, outputs=preview_html)demo.load(subs.apply_preset, inputs=[preset_input], outputs=manual_inputs) # Apply default preset on loadwith gr.Row():start_btn = gr.Button(i18n("Start Processing"), variant="primary")stop_btn = gr.Button(i18n("Stop"), variant="stop", visible=False)stop_btn.click(kill_process, outputs=[])logs_output = gr.Textbox(label=i18n("Logs"), lines=10, autoscroll=True, elem_id="logs_output")# Force scroll to bottom via JSlogs_output.change(fn=None, inputs=[], outputs=[], js="""function() {var ta = document.querySelector('#logs_output textarea');if(ta) {ta.scrollTop = ta.scrollHeight;}}""")results_html = gr.HTML(label=i18n("Results"))# MUST pass all all new inputs to the run functionstart_btn.click(run_viral_cutter, inputs=[input_source, project_selector, url_input, video_upload, segments_input, viral_input, themes_input, min_dur_input, max_dur_input,model_input, ai_backend_input, api_key_input, ai_model_input, chunk_size_input,workflow_input, face_model_input, face_mode_input, face_detect_interval_input,face_filter_thresh_input, face_two_thresh_input, face_conf_thresh_input, face_dead_zone_input, focus_active_speaker_input,active_speaker_mar_input, active_speaker_score_diff_input, include_motion_input, active_speaker_motion_threshold_input, active_speaker_motion_sensitivity_input, active_speaker_decay_input,use_custom_subs,# Expanded Manual Inputs mappingfont_name_input, font_size_input, font_color_input, highlight_color_input,outline_color_input, outline_thickness_input, shadow_color_input, shadow_size_input,bold_input, italic_input, uppercase_input, vertical_pos_input, alignment_input,# New Inputshighlight_size_input, words_per_block_input, gap_limit_input, mode_input,underline_input, strikeout_input, border_style_input, remove_punc_input,video_quality_input, use_youtube_subs_input, translate_input], outputs=[logs_output, start_btn, stop_btn, results_html])with gr.Tab(i18n("Subtitle Editor")):gr.Markdown(f"### {i18n('Edit Subtitles (Smart Mode)')}")with gr.Group():editor_project_dropdown = gr.Dropdown(choices=library.get_existing_projects(), label=i18n("Select Project"), value=None)editor_refresh_btn = gr.Button(i18n("Refresh"), size="sm")with gr.Group():editor_file_dropdown = gr.Dropdown(choices=[], label=i18n("Select Subtitle File"), interactive=True)editor_load_btn = gr.Button(i18n("Load Subtitles"), variant="secondary")# Hidden state to store full path of currently loaded JSONcurrent_json_path = gr.State()# The Dataframe Editor# Headers: Start, End, Textsubtitle_dataframe = gr.Dataframe(headers=["Start", "End", "Text"],datatype=["str", "str", "str"],col_count=(3, "fixed"),interactive=True,label=i18n("Subtitle Segments"),wrap=True)with gr.Row():editor_save_btn = gr.Button(i18n("💾 Save Changes"), variant="primary")editor_render_single_btn = gr.Button(i18n("⚡ Render This Segment (Very-Fast)"), variant="secondary")editor_render_all_btn = gr.Button(i18n("🎬 Render All (Fast)"), variant="stop")editor_status = gr.Textbox(label=i18n("Status"), interactive=False)# --- Callbacks for Editor ---editor_refresh_btn.click(library.refresh_projects, outputs=editor_project_dropdown)def update_file_list(proj_name):if not proj_name: return gr.update(choices=[])proj_path = os.path.join(VIRALS_DIR, proj_name)files = editor.list_editable_files(proj_path)return gr.update(choices=files, value=files[0] if files else None)editor_project_dropdown.change(update_file_list, inputs=editor_project_dropdown, outputs=editor_file_dropdown)def load_subs(proj_name, file_name):if not proj_name or not file_name:return [], None, i18n("Please select project and file.")full_path = os.path.join(VIRALS_DIR, proj_name, 'subs', file_name)data = editor.load_transcription_for_editor(full_path)return data, full_path, i18n("Loaded {} segments.").format(len(data))editor_load_btn.click(load_subs, inputs=[editor_project_dropdown, editor_file_dropdown], outputs=[subtitle_dataframe, current_json_path, editor_status])def save_subs(json_path, df):if not json_path: return i18n("No file loaded.")data_list = df.values.tolist() if hasattr(df, 'values') else dfmsg = editor.save_editor_changes(json_path, data_list)return msgeditor_save_btn.click(save_subs, inputs=[current_json_path, subtitle_dataframe], outputs=editor_status)def render_single(json_path, use_custom, font_name, font_size, font_color, highlight_color,outline_color, outline_thickness, shadow_color, shadow_size,is_bold, is_italic, is_uppercase,h_size, w_block, gap, mode, under, strike, border_s,vertical_pos, alignment, remove_punc):if not json_path: return i18n("No file loaded.")subtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")# Save config if custom subs enabledif use_custom:subtitle_config = {"font": font_name, "base_size": int(font_size),"base_color": convert_color_to_ass(font_color),"highlight_color": convert_color_to_ass(highlight_color),"outline_color": convert_color_to_ass(outline_color),"outline_thickness": outline_thickness,"shadow_color": convert_color_to_ass(shadow_color),"shadow_size": shadow_size, "vertical_position": vertical_pos,"alignment": alignment, "bold": 1 if is_bold else 0,"italic": 1 if is_italic else 0,"underline": 1 if under else 0, "strikeout": 1 if strike else 0,"border_style": border_s, "words_per_block": int(w_block),"gap_limit": gap, "mode": mode, "highlight_size": int(h_size),"uppercase": 1 if is_uppercase else 0,"remove_punctuation": remove_punc}try:with open(subtitle_config_path, "w", encoding="utf-8") as f:json.dump(subtitle_config, f, indent=4)except Exception: passelse:# Remove temp config if it exists to ensure defaults are usedtry:if os.path.exists(subtitle_config_path):os.remove(subtitle_config_path)except Exception: pass# We expect user to SAVE first, but we could auto-save.# For now assume saved.msg = editor.render_specific_video(json_path)return msgeditor_render_single_btn.click(render_single,inputs=[current_json_path, use_custom_subs] + manual_inputs,outputs=editor_status)def render_all(proj_name, use_custom, font_name, font_size, font_color, highlight_color,outline_color, outline_thickness, shadow_color, shadow_size,is_bold, is_italic, is_uppercase,h_size, w_block, gap, mode, under, strike, border_s,vertical_pos, alignment, remove_punc):if not proj_name: return i18n("No project selected.")# Save configif use_custom:subtitle_config = {"font": font_name, "base_size": int(font_size),"base_color": convert_color_to_ass(font_color),"highlight_color": convert_color_to_ass(highlight_color),"outline_color": convert_color_to_ass(outline_color),"outline_thickness": outline_thickness,"shadow_color": convert_color_to_ass(shadow_color),"shadow_size": shadow_size, "vertical_position": vertical_pos,"alignment": alignment, "bold": 1 if is_bold else 0,"italic": 1 if is_italic else 0,"underline": 1 if under else 0, "strikeout": 1 if strike else 0,"border_style": border_s, "words_per_block": int(w_block),"gap_limit": gap, "mode": mode, "highlight_size": int(h_size),"uppercase": 1 if is_uppercase else 0,"remove_punctuation": remove_punc}subtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")try:with open(subtitle_config_path, "w", encoding="utf-8") as f:json.dump(subtitle_config, f, indent=4)except Exception: passproj_path = os.path.join(VIRALS_DIR, proj_name)# IMPORTANT: Pass the config file path to the commandsubtitle_config_path = os.path.join(WORKING_DIR, "temp_subtitle_config.json")cmd = [sys.executable, MAIN_SCRIPT_PATH, "--project-path", proj_path, "--workflow", "3", "--skip-prompts"]if use_custom and os.path.exists(subtitle_config_path):cmd.extend(["--subtitle-config", subtitle_config_path])try:subprocess.Popen(cmd, cwd=WORKING_DIR)return i18n("Render All started in background... Check terminal/logs.")except Exception as e:return i18n("Error starting render: {}").format(e)editor_render_all_btn.click(render_all,inputs=[editor_project_dropdown, use_custom_subs] + manual_inputs,outputs=editor_status)with gr.Tab(i18n("Library")):gr.Markdown(f"### {i18n('Existing Projects')}")with gr.Row():project_dropdown = gr.Dropdown(choices=library.get_existing_projects(), label=i18n("Select Project"), value=None)refresh_btn = gr.Button(i18n("Refresh List"))project_gallery_html = gr.HTML()refresh_btn.click(library.refresh_projects, outputs=project_dropdown)def on_select_project(proj_name): return library.generate_project_gallery(proj_name)project_dropdown.change(on_select_project, project_dropdown, project_gallery_html)gr.Markdown(f"""<hr><div style='text-align: center; font-size: 0.9em; color: #777;'><p><strong>{i18n('Desenvolvido por Rafael Godoy')}</strong><br>{i18n('Apoie o projeto, qualquer valor é bem-vindo:')}<a href='https://nubank.com.br/pagar/1ls6a4/0QpSSbWBSq' target='_blank'><strong>{i18n('Apoiar via PIX')}</strong></a><br>{i18n('100% local • open source • sem assinatura')}</p></div>""")if __name__ == "__main__":import webbrowserimport threadingimport timeimport argparseparser = argparse.ArgumentParser()parser.add_argument("--colab", action="store_true", help="Run in Google Colab mode")args = parser.parse_args()if args.colab:print("Running in Colab mode. Generating public link with Static Mounts...")library.set_url_mode("fastapi")# Broaden allowed paths for Colaballowed_dirs = [VIRALS_DIR, WORKING_DIR, os.getcwd(), "."]# Explicitly set static pathstry:gr.set_static_paths(paths=allowed_dirs)print(f"DEBUG: Registered static paths: {allowed_dirs}")except AttributeError:print("DEBUG: gr.set_static_paths not available")print(f"DEBUG: Allowed paths for Gradio: {allowed_dirs}")# Launch with prevent_thread_lock to allow mountingapp, local_url, share_url = demo.queue().launch(share=True,allowed_paths=allowed_dirs,prevent_thread_lock=True)# Mount the VIRALS directory explicitlyapp.mount("/virals", StaticFiles(directory=VIRALS_DIR), name="virals")print(f"Mounted /virals to {VIRALS_DIR}")demo.block_thread()else:# Check environmentis_windows = (os.name == 'nt')library.set_url_mode("fastapi")allowed_dirs = [VIRALS_DIR, WORKING_DIR, os.getcwd(), "."]try:gr.set_static_paths(paths=allowed_dirs)except AttributeError: passfrom fastapi.responses import FileResponsefrom fastapi import BackgroundTasks# Helper to attach routes to any FastAPI app (whether created by Gradio or us)def attach_extra_routes(fastapi_app):fastapi_app.mount("/virals", StaticFiles(directory=VIRALS_DIR), name="virals")@fastapi_app.get("/export_xml_api")def export_xml_api(project: str, segment: int, background_tasks: BackgroundTasks, format: str = "premiere"):try:project_path = os.path.join(VIRALS_DIR, project)script_path = os.path.join(WORKING_DIR, "scripts", "export_xml.py")cmd = [sys.executable, script_path, "--project", project_path, "--segment", str(segment), "--format", format]subprocess.run(cmd, check=True)proj_name = os.path.basename(project_path)zip_filename = f"export_{proj_name}_seg{segment}.zip"file_path = os.path.join(project_path, zip_filename)if os.path.exists(file_path):return FileResponse(file_path, filename=zip_filename, media_type='application/zip')else:return {"error": f"File generation failed. Expected: {file_path}"}except Exception as e:return {"error": str(e)}print(f"Mounted /virals to {VIRALS_DIR}")if is_windows:print("Running in Windows environment (using Gradio launch for convenience).")# Windows: Use demo.launch() for convenience (auto-browser, etc)app, local_url, share_url = demo.queue().launch(share=False,allowed_paths=allowed_dirs,inbrowser=True,server_name="0.0.0.0",server_port=7860,prevent_thread_lock=True)attach_extra_routes(app)demo.block_thread()else:print("Running in Linux/Container environment (using Uvicorn for stability).")# Linux/HF: Use Uvicorn for explicit loop controlapp = FastAPI()attach_extra_routes(app)# Disable SSR to prevent Node proxying issues on HF Spacesapp = gr.mount_gradio_app(app, demo.queue(), path="/", allowed_paths=allowed_dirs, ssr_mode=False)uvicorn.run(app, host="0.0.0.0", port=7860)
200 unmodified lines201202203204205206207208209210211212213214215200 unmodified linesreturn f'<a href="{src}" target="_blank" style="color: #aaa; display: flex; align-items: center; justify-content: center; padding: 5px; border-radius: 50%; transition: color 0.2s;" title="{label}" onmouseover="this.style.color=\'{color_hover}\'" onmouseout="this.style.color=\'#aaa\'"><svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">{svg_path}</svg></a>'# Premiere (Pr)export_pr = make_export_btn("premiere", "Export Premiere XML", "#d064ff", '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><path d="M9 15h6"></path><path d="M12 12v6"></path>')# Resolve (Dv)export_dv = make_export_btn("resolve", "Export DaVinci Resolve XML", "#ff6464", '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><circle cx="12" cy="14" r="3"></circle>')# Final Cut (Fc)export_fc = make_export_btn("final-cut-pro", "Export FCP XML", "#64d0ff", '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><path d="M10 12l4 2l-4 2z"></path>')export_link = f"{export_pr}{export_dv}{export_fc}"else:video_tag = f'<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; background: #222; color: #666;"><span>⚠️</span><br>{i18n("External Video")}</div>'200 unmodified lines201202203204205206207208209210211212213214215200 unmodified linesreturn f'<a href="{src}" target="_blank" style="color: #aaa; display: flex; align-items: center; justify-content: center; padding: 5px; border-radius: 50%; transition: color 0.2s;" title="{label}" onmouseover="this.style.color=\'{color_hover}\'" onmouseout="this.style.color=\'#aaa\'"><svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">{svg_path}</svg></a>'# Premiere (Pr)export_pr = make_export_btn("premiere", "Export Premiere XML (Split Screen – known bug)", "#d064ff", '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><path d="M9 15h6"></path><path d="M12 12v6"></path>')# Resolve (Dv)# export_dv = make_export_btn("resolve", "Export DaVinci Resolve XML", "#ff6464", '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><circle cx="12" cy="14" r="3"></circle>')# Final Cut (Fc)# export_fc = make_export_btn("final-cut-pro", "Export FCP XML", "#64d0ff", '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><path d="M10 12l4 2l-4 2z"></path>')export_link = f"{export_pr}" #{export_dv}{export_fc}"else:video_tag = f'<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; background: #222; color: #666;"><span>⚠️</span><br>{i18n("External Video")}</div>'
webui/app.py+841-816
webui/library.py+4-4