import gradio as gr import os import shutil import subprocess import pexpect import threading # Declare the global variable gradio_input_file = None process = None def process_ebook(ebook_file): # Pre-download the xtts TOS agreed file import download_tos_agreed_file import nltk nltk.download('averaged_perceptron_tagger_eng') nltk.download('punkt_tab') # Download the en_core_web_sm spacy model subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"]) global gradio_input_file # Use the global variable to store the ebook file path # Create input_files directory if it doesn't exist input_dir = "input_files" if not os.path.exists(input_dir): os.mkdir(input_dir) # Copy the uploaded file to input_files folder input_file_path = os.path.join(input_dir, os.path.basename(ebook_file)) shutil.copy(ebook_file, input_file_path) # Set the file path to the global variable gradio_input_file = input_file_path # Print the name of the uploaded file ebook_file_name = os.path.basename(ebook_file) print(f"Uploaded file: {ebook_file_name}") # Initialize pexpect to run the subprocess and capture output return run_subprocess() def run_subprocess(): global process output = [] # Use pexpect to spawn the process and interact with it process = pexpect.spawn(f"python3 Auto_VoxNovel.py {gradio_input_file}", encoding='utf-8', timeout=None) def read_output(): while True: try: line = process.readline().strip() # Read output from the subprocess if line: # If there's output, append it to the list output.append(line) yield "\n".join(output) # Return current output to display except pexpect.EOF: break # End of file means the process has finished return read_output def send_input(user_input): global process if process and process.isalive(): process.sendline(user_input) return "Input sent!" else: return "Process is not running or already finished." def list_output_files(): # List all files in the output directory for downloading output_dir = "output_audiobooks" if os.path.exists(output_dir): files = [ os.path.join(output_dir, f) for f in os.listdir(output_dir) if os.path.isfile(os.path.join(output_dir, f)) ] return files return [] # Gradio Interface 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' # Specify that we want the file path ) process_button = gr.Button("Start Processing") status_output = gr.Textbox(label="Status") # Function to update status with subprocess output def update_status(ebook_input): output_generator = process_ebook(ebook_input) # Pass the file to process_ebook for output in output_generator: status_output.value = output # Start processing and update status in real-time process_button.click(update_status, inputs=ebook_input, outputs=status_output) with gr.Column(): gr.Markdown("### Subprocess Input") # Textbox to allow user to send input to subprocess 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, outputs=status_output) with gr.Column(): gr.Markdown("### Download Generated Audiobook Files") download_button = gr.Button("Reload Files") file_output = gr.Files( label="Generated Audiobook Files", type='filepath' # Use 'filepath' type for gr.Files component ) # Update the file_output component with the list of output files def update_output_files(): files = list_output_files() return files download_button.click(fn=update_output_files, inputs=[], outputs=file_output) gui.launch()