|
import gradio as gr |
|
import os |
|
import shutil |
|
import subprocess |
|
|
|
def process_ebook(ebook_file): |
|
|
|
input_dir = "input_files" |
|
output_dir = "output_audiobooks" |
|
os.makedirs(input_dir, exist_ok=True) |
|
|
|
|
|
ebook_file_path = os.path.join(input_dir, ebook_file.name) |
|
shutil.move(ebook_file.name, ebook_file_path) |
|
|
|
|
|
try: |
|
result = subprocess.run(["python3", "audiobook.py"], capture_output=True, text=True) |
|
if result.returncode == 0: |
|
|
|
return "Audiobook is ready! You can now download your files below." |
|
else: |
|
|
|
return f"Error: {result.stderr}" |
|
except Exception as e: |
|
return f"Failed to run audiobook script: {str(e)}" |
|
|
|
def list_output_files(): |
|
|
|
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 |
|
|
|
|
|
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() |
|
|