File size: 2,205 Bytes
c769319
577cd8e
 
c769319
577cd8e
c769319
 
 
 
 
577cd8e
c769319
 
 
577cd8e
c769319
577cd8e
c769319
 
 
 
577cd8e
c769319
 
577cd8e
c769319
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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()