drewThomasson commited on
Commit
0789588
·
verified ·
1 Parent(s): 39de50d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -10
app.py CHANGED
@@ -3,22 +3,30 @@ import os
3
  import shutil
4
  import subprocess
5
 
 
 
6
  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
- # 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}")
@@ -48,6 +56,53 @@ def process_ebook(ebook_file):
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"
 
3
  import shutil
4
  import subprocess
5
 
6
+
7
+
8
  def process_ebook(ebook_file):
9
  # Ensure input_files folder exists
10
  input_dir = "input_files"
11
  os.makedirs(input_dir, exist_ok=True)
12
 
13
+ # Check if input_files folder is empty
14
+ if not os.listdir(input_dir):
15
+ raise FileNotFoundError("The folder 'input_files' is empty. Please upload a file.")
16
+
17
+ # Get the base name and extension of the uploaded file
18
  ebook_file_name = os.path.basename(ebook_file)
19
+ file_name, file_extension = os.path.splitext(ebook_file_name)
20
+
21
+ # Define the new file name with "_Audiobook"
22
+ new_file_name = f"{file_name}_Audiobook{file_extension}"
23
+ new_file_path = os.path.join(input_dir, new_file_name)
24
+
25
+ # Copy the uploaded file to the input_files folder with the new name
26
+ shutil.copy(ebook_file, new_file_path)
27
+
28
+ # print the name of the new file
29
+ print (f"File duplicated as: {new_file_name} in 'input_files' folder")
30
 
31
  # Print the list of input files in the terminal
32
  print(f"Files in input_files folder:\n{input_files_str}")
 
56
  except Exception as e:
57
  return f"Failed to run audiobook script: {str(e)}\n\nInput files:\n{input_files_str}"
58
 
59
+
60
+
61
+
62
+ #def process_ebook(ebook_file):
63
+ # # Ensure input_files folder exists
64
+ # input_dir = "input_files"
65
+ # os.makedirs(input_dir, exist_ok=True)
66
+ #
67
+ # # Get the base name of the uploaded file
68
+ # ebook_file_name = os.path.basename(ebook_file)
69
+ # ebook_file_path = os.path.join(input_dir, ebook_file_name)
70
+ #
71
+ # # Copy the file to the input_files directory if it doesn't already exist there
72
+ # if not os.path.exists(ebook_file_path):
73
+ # shutil.copy(ebook_file, ebook_file_path)
74
+ #
75
+ # # List the contents of the input_files folder
76
+ # input_files_list = os.listdir(input_dir)
77
+ # input_files_str = "\n".join(input_files_list)
78
+ #
79
+ # # Print the list of input files in the terminal
80
+ # print(f"Files in input_files folder:\n{input_files_str}")
81
+ # if not os.listdir("input_files"):
82
+ # raise FileNotFoundError("The folder 'input_files' is empty.")
83
+ #
84
+ # # Call the Auto_VoxNovel.py script and stream its output live
85
+ # try:
86
+ # process = subprocess.Popen(
87
+ # ["python3", "Auto_VoxNovel.py"],
88
+ # stdout=subprocess.PIPE,
89
+ # stderr=subprocess.STDOUT,
90
+ # text=True
91
+ # )
92
+ #
93
+ # # Print output in real-time
94
+ # for line in process.stdout:
95
+ # print(line, end='')
96
+ #
97
+ # process.wait()
98
+ #
99
+ # if process.returncode == 0:
100
+ # return f"Audiobook is ready! You can now download your files below.\n\nInput files:\n{input_files_str}"
101
+ # else:
102
+ # return f"Error occurred during audiobook processing.\n\nInput files:\n{input_files_str}"
103
+ # except Exception as e:
104
+ # return f"Failed to run audiobook script: {str(e)}\n\nInput files:\n{input_files_str}"
105
+
106
  def list_output_files():
107
  # List all files in the output directory for downloading
108
  output_dir = "output_audiobooks"