drewThomasson's picture
Update app.py
c769319 verified
raw
history blame
2.21 kB
import gradio as gr
import os
import shutil
import subprocess
def process_ebook(ebook_file):
# Ensure input_files folder exists
input_dir = "input_files"
output_dir = "output_audiobooks"
os.makedirs(input_dir, exist_ok=True)
# Save the uploaded file to the input_files folder
ebook_file_path = os.path.join(input_dir, ebook_file.name)
shutil.move(ebook_file.name, ebook_file_path)
# Call your audiobook.py script and wait for it to finish
try:
result = subprocess.run(["python3", "audiobook.py"], capture_output=True, text=True)
if result.returncode == 0:
# Success message when the audiobook is ready
return "Audiobook is ready! You can now download your files below."
else:
# Error message if something went wrong
return f"Error: {result.stderr}"
except Exception as e:
return f"Failed to run audiobook script: {str(e)}"
def list_output_files():
# List all files in the output directory for downloading
output_dir = "output_audiobooks"
if os.path.exists(output_dir):
files = [str(f) for f in os.listdir(output_dir) if os.path.isfile(os.path.join(output_dir, f))]
return files
return []
def download_output_files():
files = list_output_files()
file_links = {file: os.path.join("output_audiobooks", file) for file in files}
return file_links
# 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.)")
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.File(label="Generated Audiobook Files", interactive=False)
download_button.click(fn=lambda: download_output_files(), inputs=[], outputs=file_output)
gui.launch()