This commit is contained in:
unknown
2025-07-01 07:56:02 -07:00
parent b308c3c12a
commit ac0387386f
2 changed files with 19 additions and 14 deletions

12
app.py
View File

@@ -114,8 +114,6 @@ def is_port_in_use(port):
return s.connect_ex(('0.0.0.0', port)) == 0
def main():
global is_gui_process
# Argument parser to handle optional parameters with descriptions
parser = argparse.ArgumentParser(
description='Convert eBooks to Audiobooks using a Text-to-Speech model. You can either launch the Gradio interface or run the script in headless mode for direct conversion.',
@@ -233,8 +231,8 @@ Tip: to add of silence (1.4 seconds) into your text just use "###" or "[pause]".
print(error)
sys.exit(1)
from lib.functions import web_interface, convert_ebook_batch, convert_ebook
from lib.functions import SessionContext, convert_ebook_batch, convert_ebook, web_interface
ctx = SessionContext()
# Conditions based on the --headless flag
if args['headless']:
args['is_gui_process'] = False
@@ -268,7 +266,7 @@ Tip: to add of silence (1.4 seconds) into your text just use "###" or "[pause]".
if any(file.endswith(ext) for ext in ebook_formats):
full_path = os.path.abspath(os.path.join(args['ebooks_dir'], file))
args['ebook_list'].append(full_path)
progress_status, passed = convert_ebook_batch(args)
progress_status, passed = convert_ebook_batch(args, ctx)
if passed is False:
error = f'Conversion failed: {progress_status}'
print(error)
@@ -279,7 +277,7 @@ Tip: to add of silence (1.4 seconds) into your text just use "###" or "[pause]".
error = f'Error: The provided --ebook "{args["ebook"]}" does not exist.'
print(error)
sys.exit(1)
progress_status, passed = convert_ebook(args)
progress_status, passed = convert_ebook(args, ctx)
if passed is False:
error = f'Conversion failed: {progress_status}'
print(error)
@@ -294,7 +292,7 @@ Tip: to add of silence (1.4 seconds) into your text just use "###" or "[pause]".
allowed_arguments = {'--share', '--script_mode'}
passed_args_set = {arg for arg in passed_arguments if arg.startswith('--')}
if passed_args_set.issubset(allowed_arguments):
web_interface(args)
web_interface(args, ctx)
else:
error = 'Error: In non-headless mode, no option or only --share can be passed'
print(error)

View File

@@ -68,6 +68,10 @@ from lib.classes.tts_manager import TTSManager
#from lib.classes.redirect_console import RedirectConsole
#from lib.classes.argos_translator import ArgosTranslator
context = None
lock = threading.Lock()
is_gui_process = False
class DependencyError(Exception):
def __init__(self, message=None):
super().__init__(message)
@@ -178,10 +182,6 @@ class SessionContext:
}, manager=self.manager)
return self.sessions[id]
lock = threading.Lock()
context = SessionContext()
is_gui_process = False
def prepare_dirs(src, session):
try:
resume = False
@@ -1463,7 +1463,9 @@ def get_compatible_tts_engines(language):
]
return compatible_engines
def convert_ebook_batch(args):
def convert_ebook_batch(args, ctx):
global context
context = ctx
if isinstance(args['ebook_list'], list):
ebook_list = args['ebook_list'][:]
for file in ebook_list: # Use a shallow copy
@@ -1481,7 +1483,7 @@ def convert_ebook_batch(args):
print(f'the ebooks source is not a list!')
sys.exit(1)
def convert_ebook(args):
def convert_ebook(args, ctx=None):
try:
global is_gui_process, context
error = None
@@ -1517,6 +1519,8 @@ def convert_ebook(args):
print(error)
return error, false
if ctx is not None:
context = ctx
is_gui_process = args['is_gui_process']
id = args['session'] if args['session'] is not None else str(uuid.uuid4())
session = context.get_session(id)
@@ -1784,7 +1788,8 @@ def show_alert(state):
elif state['type'] == 'success':
gr.Success(state['msg'])
def web_interface(args):
def web_interface(args, ctx):
global context
script_mode = args['script_mode']
is_gui_process = args['is_gui_process']
is_gui_shared = args['share']
@@ -1816,6 +1821,8 @@ def web_interface(args):
# Event to signal when the process should stop
thread = None
stop_event = threading.Event()
context = ctx
theme = gr.themes.Origin(
primary_hue='green',