import gradio as gr | |
import os | |
import shutil | |
import subprocess | |
def process_ebook(ebook_file): | |
# Ensure input_files folder exists | |
input_dir = "input_files" | |
os.makedirs(input_dir, exist_ok=True) | |
# Check if input_files folder is empty | |
if not os.listdir(input_dir): | |
raise FileNotFoundError("The folder 'input_files' is empty. Please upload a file.") | |
# Get the base name and extension of the uploaded file | |
ebook_file_name = os.path.basename(ebook_file) | |
file_name, file_extension = os.path.splitext(ebook_file_name) | |
# Define the new file name with "_Audiobook" | |
new_file_name = f"{file_name}_Audiobook{file_extension}" | |
new_file_path = os.path.join(input_dir, new_file_name) | |
# Copy the uploaded file to the input_files folder with the new name | |
shutil.copy(ebook_file, new_file_path) | |
# print the name of the new file | |
print (f"File duplicated as: {new_file_name} in 'input_files' folder") | |
# Print the list of input files in the terminal | |
print(f"Files in input_files folder:\n{input_files_str}") | |
if not os.listdir("input_files"): | |
raise FileNotFoundError("The folder 'input_files' is empty.") | |
# Call the Auto_VoxNovel.py script and stream its output live | |
try: | |
process = subprocess.Popen( | |
["python3", "Auto_VoxNovel.py"], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
text=True | |
) | |
# Print output in real-time | |
for line in process.stdout: | |
print(line, end='') | |
process.wait() | |
if process.returncode == 0: | |
return f"Audiobook is ready! You can now download your files below.\n\nInput files:\n{input_files_str}" | |
else: | |
return f"Error occurred during audiobook processing.\n\nInput files:\n{input_files_str}" | |
except Exception as e: | |
return f"Failed to run audiobook script: {str(e)}\n\nInput files:\n{input_files_str}" | |
#def process_ebook(ebook_file): | |
# # Ensure input_files folder exists | |
# input_dir = "input_files" | |
# os.makedirs(input_dir, exist_ok=True) | |
# | |
# # Get the base name of the uploaded file | |
# ebook_file_name = os.path.basename(ebook_file) | |
# ebook_file_path = os.path.join(input_dir, ebook_file_name) | |
# | |
# # Copy the file to the input_files directory if it doesn't already exist there | |
# if not os.path.exists(ebook_file_path): | |
# shutil.copy(ebook_file, ebook_file_path) | |
# | |
# # List the contents of the input_files folder | |
# input_files_list = os.listdir(input_dir) | |
# input_files_str = "\n".join(input_files_list) | |
# | |
# # Print the list of input files in the terminal | |
# print(f"Files in input_files folder:\n{input_files_str}") | |
# if not os.listdir("input_files"): | |
# raise FileNotFoundError("The folder 'input_files' is empty.") | |
# | |
# # Call the Auto_VoxNovel.py script and stream its output live | |
# try: | |
# process = subprocess.Popen( | |
# ["python3", "Auto_VoxNovel.py"], | |
# stdout=subprocess.PIPE, | |
# stderr=subprocess.STDOUT, | |
# text=True | |
# ) | |
# | |
# # Print output in real-time | |
# for line in process.stdout: | |
# print(line, end='') | |
# | |
# process.wait() | |
# | |
# if process.returncode == 0: | |
# return f"Audiobook is ready! You can now download your files below.\n\nInput files:\n{input_files_str}" | |
# else: | |
# return f"Error occurred during audiobook processing.\n\nInput files:\n{input_files_str}" | |
# except Exception as e: | |
# return f"Failed to run audiobook script: {str(e)}\n\nInput files:\n{input_files_str}" | |
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") | |
process_button.click(process_ebook, inputs=ebook_input, 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='file' # Use 'file' 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() | |