drewThomasson
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,28 +7,33 @@ def process_ebook(ebook_file):
|
|
7 |
# Ensure input_files folder exists
|
8 |
input_dir = "input_files"
|
9 |
os.makedirs(input_dir, exist_ok=True)
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
# List the contents of the input_files folder
|
18 |
input_files_list = os.listdir(input_dir)
|
19 |
input_files_str = "\n".join(input_files_list)
|
20 |
-
|
21 |
# Print the list of input files in the terminal
|
22 |
-
print(f"Files in input_files folder
|
23 |
-
|
24 |
if not os.listdir("input_files"):
|
25 |
raise FileNotFoundError("The folder 'input_files' is empty.")
|
26 |
|
27 |
-
|
28 |
-
|
29 |
# Call the Auto_VoxNovel.py script and stream its output live
|
30 |
try:
|
31 |
-
process = subprocess.Popen(
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Print output in real-time
|
34 |
for line in process.stdout:
|
@@ -43,28 +48,28 @@ def process_ebook(ebook_file):
|
|
43 |
except Exception as e:
|
44 |
return f"Failed to run audiobook script: {str(e)}\n\nInput files:\n{input_files_str}"
|
45 |
|
46 |
-
|
47 |
-
|
48 |
def list_output_files():
|
49 |
# List all files in the output directory for downloading
|
50 |
output_dir = "output_audiobooks"
|
51 |
if os.path.exists(output_dir):
|
52 |
-
files = [
|
|
|
|
|
|
|
|
|
53 |
return files
|
54 |
return []
|
55 |
|
56 |
-
def download_output_files():
|
57 |
-
files = list_output_files()
|
58 |
-
file_links = {file: os.path.join("output_audiobooks", file) for file in files}
|
59 |
-
return file_links
|
60 |
-
|
61 |
# Gradio Interface
|
62 |
with gr.Blocks() as gui:
|
63 |
gr.Markdown("### Ebook to Audiobook Converter")
|
64 |
|
65 |
with gr.Row():
|
66 |
with gr.Column():
|
67 |
-
ebook_input = gr.File(
|
|
|
|
|
|
|
68 |
process_button = gr.Button("Start Processing")
|
69 |
status_output = gr.Textbox(label="Status")
|
70 |
process_button.click(process_ebook, inputs=ebook_input, outputs=status_output)
|
@@ -72,8 +77,17 @@ with gr.Blocks() as gui:
|
|
72 |
with gr.Column():
|
73 |
gr.Markdown("### Download Generated Audiobook Files")
|
74 |
download_button = gr.Button("Reload Files")
|
75 |
-
file_output = gr.File(
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
gui.launch()
|
|
|
7 |
# Ensure input_files folder exists
|
8 |
input_dir = "input_files"
|
9 |
os.makedirs(input_dir, exist_ok=True)
|
10 |
+
|
11 |
+
# Get the base name of the uploaded file
|
12 |
+
ebook_file_name = os.path.basename(ebook_file)
|
13 |
+
ebook_file_path = os.path.join(input_dir, ebook_file_name)
|
14 |
+
|
15 |
+
# Copy the file to the input_files directory if it doesn't already exist there
|
16 |
+
if not os.path.exists(ebook_file_path):
|
17 |
+
shutil.copy(ebook_file, ebook_file_path)
|
18 |
+
|
19 |
# List the contents of the input_files folder
|
20 |
input_files_list = os.listdir(input_dir)
|
21 |
input_files_str = "\n".join(input_files_list)
|
22 |
+
|
23 |
# Print the list of input files in the terminal
|
24 |
+
print(f"Files in input_files folder:\n{input_files_str}")
|
25 |
+
|
26 |
if not os.listdir("input_files"):
|
27 |
raise FileNotFoundError("The folder 'input_files' is empty.")
|
28 |
|
|
|
|
|
29 |
# Call the Auto_VoxNovel.py script and stream its output live
|
30 |
try:
|
31 |
+
process = subprocess.Popen(
|
32 |
+
["python3", "Auto_VoxNovel.py"],
|
33 |
+
stdout=subprocess.PIPE,
|
34 |
+
stderr=subprocess.STDOUT,
|
35 |
+
text=True
|
36 |
+
)
|
37 |
|
38 |
# Print output in real-time
|
39 |
for line in process.stdout:
|
|
|
48 |
except Exception as e:
|
49 |
return f"Failed to run audiobook script: {str(e)}\n\nInput files:\n{input_files_str}"
|
50 |
|
|
|
|
|
51 |
def list_output_files():
|
52 |
# List all files in the output directory for downloading
|
53 |
output_dir = "output_audiobooks"
|
54 |
if os.path.exists(output_dir):
|
55 |
+
files = [
|
56 |
+
os.path.join(output_dir, f)
|
57 |
+
for f in os.listdir(output_dir)
|
58 |
+
if os.path.isfile(os.path.join(output_dir, f))
|
59 |
+
]
|
60 |
return files
|
61 |
return []
|
62 |
|
|
|
|
|
|
|
|
|
|
|
63 |
# Gradio Interface
|
64 |
with gr.Blocks() as gui:
|
65 |
gr.Markdown("### Ebook to Audiobook Converter")
|
66 |
|
67 |
with gr.Row():
|
68 |
with gr.Column():
|
69 |
+
ebook_input = gr.File(
|
70 |
+
label="Upload your ebook file (epub, pdf, etc.)",
|
71 |
+
type='filepath' # Specify that we want the file path
|
72 |
+
)
|
73 |
process_button = gr.Button("Start Processing")
|
74 |
status_output = gr.Textbox(label="Status")
|
75 |
process_button.click(process_ebook, inputs=ebook_input, outputs=status_output)
|
|
|
77 |
with gr.Column():
|
78 |
gr.Markdown("### Download Generated Audiobook Files")
|
79 |
download_button = gr.Button("Reload Files")
|
80 |
+
file_output = gr.File(
|
81 |
+
label="Generated Audiobook Files",
|
82 |
+
interactive=False,
|
83 |
+
type='file' # Ensure the files are served correctly
|
84 |
+
)
|
85 |
+
|
86 |
+
# Update the file_output component with the list of output files
|
87 |
+
def update_output_files():
|
88 |
+
files = list_output_files()
|
89 |
+
return files
|
90 |
+
|
91 |
+
download_button.click(fn=update_output_files, inputs=[], outputs=file_output)
|
92 |
|
93 |
gui.launch()
|