drewThomasson
commited on
Commit
•
40c8999
1
Parent(s):
40b2c6c
Create new_app.py
Browse files- new_app.py +149 -0
new_app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Global variables for dropdown and checkbox
|
2 |
+
character_accent = None
|
3 |
+
use_narrator_voice_y_n = None
|
4 |
+
|
5 |
+
# List of languages for character accents
|
6 |
+
languages = ['en', 'es', 'fr', 'de', 'it', 'pt', 'pl', 'tr', 'ru', 'nl', 'cs', 'ar', 'zh-cn', 'hu', 'ko', 'ja', 'hi']
|
7 |
+
|
8 |
+
# Function to process the ebook and start the subprocess
|
9 |
+
def process_ebook(ebook_file, character_accent, use_narrator_voice_y_n, state):
|
10 |
+
if not ebook_file:
|
11 |
+
return "No file uploaded.", state
|
12 |
+
|
13 |
+
# Pre-download the xtts TOS agreed file
|
14 |
+
import download_tos_agreed_file
|
15 |
+
|
16 |
+
import nltk
|
17 |
+
nltk.download('averaged_perceptron_tagger_eng')
|
18 |
+
nltk.download('punkt_tab')
|
19 |
+
|
20 |
+
# Download the en_core_web_sm spacy model
|
21 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
22 |
+
|
23 |
+
# Create input_files directory if it doesn't exist
|
24 |
+
input_dir = "input_files"
|
25 |
+
if not os.path.exists(input_dir):
|
26 |
+
os.mkdir(input_dir)
|
27 |
+
|
28 |
+
# Copy the uploaded file to input_files folder
|
29 |
+
input_file_path = os.path.join(input_dir, os.path.basename(ebook_file))
|
30 |
+
shutil.copy(ebook_file, input_file_path)
|
31 |
+
|
32 |
+
# Print the name of the uploaded file
|
33 |
+
ebook_file_name = os.path.basename(ebook_file)
|
34 |
+
initial_output = f"Uploaded file: {ebook_file_name}\n"
|
35 |
+
|
36 |
+
# Initialize state with subprocess info
|
37 |
+
state = {
|
38 |
+
"process": None,
|
39 |
+
"output": initial_output,
|
40 |
+
"lock": threading.Lock()
|
41 |
+
}
|
42 |
+
|
43 |
+
# Start the subprocess in a separate thread
|
44 |
+
thread = threading.Thread(target=run_subprocess, args=(input_file_path, character_accent, use_narrator_voice_y_n, state), daemon=True)
|
45 |
+
thread.start()
|
46 |
+
|
47 |
+
return initial_output, state
|
48 |
+
|
49 |
+
# Function to run the subprocess and capture its output
|
50 |
+
def run_subprocess(input_file_path, character_accent, use_narrator_voice_y_n, state):
|
51 |
+
try:
|
52 |
+
# Start the subprocess with additional arguments
|
53 |
+
process = subprocess.Popen(
|
54 |
+
["python3", "Headless_VoxNovel.py", input_file_path, character_accent, use_narrator_voice_y_n],
|
55 |
+
stdin=subprocess.PIPE,
|
56 |
+
stdout=subprocess.PIPE,
|
57 |
+
stderr=subprocess.STDOUT,
|
58 |
+
text=True,
|
59 |
+
bufsize=1 # Line-buffered
|
60 |
+
)
|
61 |
+
|
62 |
+
with state["lock"]:
|
63 |
+
state["process"] = process
|
64 |
+
|
65 |
+
# Continuously read the subprocess output
|
66 |
+
for line in iter(process.stdout.readline, ''):
|
67 |
+
with state["lock"]:
|
68 |
+
state["output"] += line
|
69 |
+
process.stdout.close()
|
70 |
+
process.wait()
|
71 |
+
with state["lock"]:
|
72 |
+
state["output"] += "\nProcess finished."
|
73 |
+
except Exception as e:
|
74 |
+
with state["lock"]:
|
75 |
+
state["output"] += f"\nError: {str(e)}"
|
76 |
+
|
77 |
+
# Gradio Interface
|
78 |
+
with gr.Blocks() as gui:
|
79 |
+
gr.Markdown("### Ebook to Audiobook Converter")
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column():
|
83 |
+
ebook_input = gr.File(
|
84 |
+
label="Upload your ebook file (epub, pdf, etc.)",
|
85 |
+
type='filepath' # Specify that we want the file path
|
86 |
+
)
|
87 |
+
# Add dropdown for character accent
|
88 |
+
accent_dropdown = gr.Dropdown(
|
89 |
+
label="Character Accent",
|
90 |
+
choices=languages,
|
91 |
+
value="en" # Default to English
|
92 |
+
)
|
93 |
+
|
94 |
+
# Add checkbox for narrator voice
|
95 |
+
narrator_checkbox = gr.Checkbox(
|
96 |
+
label="Use Narrator Voice for All",
|
97 |
+
value=False # Default is 'n' (false)
|
98 |
+
)
|
99 |
+
|
100 |
+
process_button = gr.Button("Start Processing")
|
101 |
+
status_output = gr.Textbox(label="Status", lines=20)
|
102 |
+
|
103 |
+
# Initialize state
|
104 |
+
state = gr.State()
|
105 |
+
|
106 |
+
# Start processing and update status in real-time
|
107 |
+
process_button.click(
|
108 |
+
process_ebook,
|
109 |
+
inputs=[ebook_input, accent_dropdown, narrator_checkbox, state],
|
110 |
+
outputs=[status_output, state]
|
111 |
+
)
|
112 |
+
|
113 |
+
with gr.Column():
|
114 |
+
gr.Markdown("### Subprocess Input")
|
115 |
+
|
116 |
+
# Textbox to allow user to send input to subprocess
|
117 |
+
user_input_box = gr.Textbox(label="Send Input to Subprocess")
|
118 |
+
submit_input_button = gr.Button("Submit Input")
|
119 |
+
submit_input_button.click(
|
120 |
+
send_input,
|
121 |
+
inputs=[user_input_box, state],
|
122 |
+
outputs=status_output # Corrected to pass the actual Gradio component
|
123 |
+
)
|
124 |
+
|
125 |
+
with gr.Column():
|
126 |
+
gr.Markdown("### Download Generated Audiobook Files")
|
127 |
+
download_button = gr.Button("Reload Files")
|
128 |
+
file_output = gr.File(
|
129 |
+
label="Generated Audiobook Files",
|
130 |
+
file_count="multiple",
|
131 |
+
type='filepath' # Corrected the type to 'filepath'
|
132 |
+
)
|
133 |
+
|
134 |
+
# Update the file_output component with the list of output files
|
135 |
+
download_button.click(fn=list_output_files, inputs=[], outputs=file_output)
|
136 |
+
|
137 |
+
# Periodically update the status_output textbox
|
138 |
+
def periodic_update(state):
|
139 |
+
return get_output(state)
|
140 |
+
|
141 |
+
# Use Gradio's `every` method to update every second
|
142 |
+
gui.load(
|
143 |
+
periodic_update,
|
144 |
+
inputs=state,
|
145 |
+
outputs=status_output,
|
146 |
+
every=1 # Update every second
|
147 |
+
)
|
148 |
+
|
149 |
+
gui.launch()
|