|
|
|
character_accent = None |
|
use_narrator_voice_y_n = None |
|
|
|
|
|
languages = ['en', 'es', 'fr', 'de', 'it', 'pt', 'pl', 'tr', 'ru', 'nl', 'cs', 'ar', 'zh-cn', 'hu', 'ko', 'ja', 'hi'] |
|
|
|
|
|
def process_ebook(ebook_file, character_accent, use_narrator_voice_y_n, state): |
|
if not ebook_file: |
|
return "No file uploaded.", state |
|
|
|
|
|
import download_tos_agreed_file |
|
|
|
import nltk |
|
nltk.download('averaged_perceptron_tagger_eng') |
|
nltk.download('punkt_tab') |
|
|
|
|
|
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"]) |
|
|
|
|
|
input_dir = "input_files" |
|
if not os.path.exists(input_dir): |
|
os.mkdir(input_dir) |
|
|
|
|
|
input_file_path = os.path.join(input_dir, os.path.basename(ebook_file)) |
|
shutil.copy(ebook_file, input_file_path) |
|
|
|
|
|
ebook_file_name = os.path.basename(ebook_file) |
|
initial_output = f"Uploaded file: {ebook_file_name}\n" |
|
|
|
|
|
state = { |
|
"process": None, |
|
"output": initial_output, |
|
"lock": threading.Lock() |
|
} |
|
|
|
|
|
thread = threading.Thread(target=run_subprocess, args=(input_file_path, character_accent, use_narrator_voice_y_n, state), daemon=True) |
|
thread.start() |
|
|
|
return initial_output, state |
|
|
|
|
|
def run_subprocess(input_file_path, character_accent, use_narrator_voice_y_n, state): |
|
try: |
|
|
|
process = subprocess.Popen( |
|
["python3", "Headless_VoxNovel.py", input_file_path, character_accent, use_narrator_voice_y_n], |
|
stdin=subprocess.PIPE, |
|
stdout=subprocess.PIPE, |
|
stderr=subprocess.STDOUT, |
|
text=True, |
|
bufsize=1 |
|
) |
|
|
|
with state["lock"]: |
|
state["process"] = process |
|
|
|
|
|
for line in iter(process.stdout.readline, ''): |
|
with state["lock"]: |
|
state["output"] += line |
|
process.stdout.close() |
|
process.wait() |
|
with state["lock"]: |
|
state["output"] += "\nProcess finished." |
|
except Exception as e: |
|
with state["lock"]: |
|
state["output"] += f"\nError: {str(e)}" |
|
|
|
|
|
with gr.Blocks() as gui: |
|
gr.Markdown("### Ebook to Audiobook Converter") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
ebook_input = gr.File( |
|
label="Upload your ebook file (epub, pdf, etc.)", |
|
type='filepath' |
|
) |
|
|
|
accent_dropdown = gr.Dropdown( |
|
label="Character Accent", |
|
choices=languages, |
|
value="en" |
|
) |
|
|
|
|
|
narrator_checkbox = gr.Checkbox( |
|
label="Use Narrator Voice for All", |
|
value=False |
|
) |
|
|
|
process_button = gr.Button("Start Processing") |
|
status_output = gr.Textbox(label="Status", lines=20) |
|
|
|
|
|
state = gr.State() |
|
|
|
|
|
process_button.click( |
|
process_ebook, |
|
inputs=[ebook_input, accent_dropdown, narrator_checkbox, state], |
|
outputs=[status_output, state] |
|
) |
|
|
|
with gr.Column(): |
|
gr.Markdown("### Subprocess Input") |
|
|
|
|
|
user_input_box = gr.Textbox(label="Send Input to Subprocess") |
|
submit_input_button = gr.Button("Submit Input") |
|
submit_input_button.click( |
|
send_input, |
|
inputs=[user_input_box, state], |
|
outputs=status_output |
|
) |
|
|
|
with gr.Column(): |
|
gr.Markdown("### Download Generated Audiobook Files") |
|
download_button = gr.Button("Reload Files") |
|
file_output = gr.File( |
|
label="Generated Audiobook Files", |
|
file_count="multiple", |
|
type='filepath' |
|
) |
|
|
|
|
|
download_button.click(fn=list_output_files, inputs=[], outputs=file_output) |
|
|
|
|
|
def periodic_update(state): |
|
return get_output(state) |
|
|
|
|
|
gui.load( |
|
periodic_update, |
|
inputs=state, |
|
outputs=status_output, |
|
every=1 |
|
) |
|
|
|
gui.launch() |
|
|