File size: 5,164 Bytes
c769319 577cd8e c769319 577cd8e 0789588 c769319 b977317 0789588 b977317 0789588 b977317 08b913e b977317 148cf98 d040eb4 577cd8e b977317 d040eb4 bd3311e d040eb4 bd3311e 577cd8e bd3311e 0789588 c769319 b977317 c769319 b977317 c769319 39de50d b977317 39de50d b977317 c769319 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
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()
|