File size: 3,275 Bytes
c769319
577cd8e
4a7d9b6
c769319
577cd8e
40f31ac
 
0789588
c769319
765c790
 
bfe61b1
a53b35a
 
40f31ac
4a7d9b6
 
 
 
 
 
 
 
 
 
 
 
0789588
40f31ac
b977317
40f31ac
b977317
40f31ac
577cd8e
b977317
40f31ac
b977317
 
 
 
d040eb4
 
 
 
 
 
 
 
40f31ac
d040eb4
40f31ac
577cd8e
40f31ac
0789588
 
c769319
 
 
 
b977317
 
 
 
 
c769319
 
 
 
 
 
 
 
 
b977317
 
 
 
c769319
 
 
 
 
 
 
39de50d
b977317
4a7d9b6
b977317
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import gradio as gr
import os
import shutil
import subprocess

# Declare the global variable
gradio_input_file = None

def process_ebook(ebook_file):
    import nltk
    nltk.download('averaged_perceptron_tagger_eng')
    nltk.download('punkt_tab')
    #download the en_core_web_sm spacy model
    subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
    global gradio_input_file  # Use the global variable to store the ebook file path

    # Create input_files directory if it doesn't exist
    input_dir = "input_files"
    if not os.path.exists(input_dir):
        os.mkdir(input_dir)

    # Copy the uploaded file to input_files folder
    input_file_path = os.path.join(input_dir, os.path.basename(ebook_file))
    shutil.copy(ebook_file, input_file_path)

    # Set the file path to the global variable
    gradio_input_file = input_file_path
    
    # Print the name of the uploaded file
    ebook_file_name = os.path.basename(ebook_file)
    print(f"Uploaded file: {ebook_file_name}")

    # Call the Auto_VoxNovel.py script and pass the file path as an argument
    try:
        process = subprocess.Popen(
            ["python3", "Auto_VoxNovel.py", gradio_input_file],  # Pass gradio_input_file as an argument
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True
        )

        # Print output in real-time
        for line in process.stdout:
            print(line, end='')

        process.wait()

        if process.returncode == 0:
            return f"Audiobook is ready! You can now download your files."
        else:
            return "Error occurred during audiobook processing."
    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 = [
            os.path.join(output_dir, f)
            for f in os.listdir(output_dir)
            if os.path.isfile(os.path.join(output_dir, f))
        ]
        return files
    return []

# 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.)",
                type='filepath'  # Specify that we want the file path
            )
            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.Files(
                label="Generated Audiobook Files",
                type='filepath'  # Use 'filepath' type for gr.Files component
            )

            # Update the file_output component with the list of output files
            def update_output_files():
                files = list_output_files()
                return files

            download_button.click(fn=update_output_files, inputs=[], outputs=file_output)

gui.launch()