Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -621,28 +621,29 @@ def convert_chapters_to_audio_standard_model(chapters_dir, output_audio_dir, tar
|
|
621 |
print(f"Converted chapter {chapter_num} to audio.")
|
622 |
|
623 |
|
624 |
-
|
625 |
-
# Define the functions to be used in the Gradio interface
|
626 |
def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url=None, progress=gr.Progress()):
|
|
|
627 |
ebook_file_path = ebook_file if isinstance(ebook_file, str) else ebook_file.name
|
628 |
target_voice = target_voice_file if isinstance(target_voice_file, str) else target_voice_file.name if target_voice_file else None
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
|
|
|
633 |
working_files = os.path.join(".", "Working_files", "temp_ebook")
|
634 |
full_folder_working_files = os.path.join(".", "Working_files")
|
635 |
chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
|
636 |
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
|
|
|
|
637 |
remove_folder_with_contents(full_folder_working_files)
|
638 |
remove_folder_with_contents(output_audio_directory)
|
639 |
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
if use_custom_model and custom_model_url:
|
647 |
print(f"Received custom model URL: {custom_model_url}")
|
648 |
download_dir = os.path.join(".", "Working_files", "custom_model")
|
@@ -652,10 +653,16 @@ def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_m
|
|
652 |
if rename_vocab_file_if_exists(download_dir):
|
653 |
print("vocab.json file was found and renamed.")
|
654 |
|
|
|
|
|
|
|
|
|
|
|
|
|
655 |
custom_model = {
|
656 |
-
'model':
|
657 |
-
'config':
|
658 |
-
'vocab':
|
659 |
}
|
660 |
|
661 |
try:
|
@@ -663,15 +670,16 @@ def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_m
|
|
663 |
except Exception as e:
|
664 |
print(f"Error updating progress: {e}")
|
665 |
|
|
|
666 |
if not calibre_installed():
|
667 |
return "Calibre is not installed."
|
668 |
|
669 |
-
|
670 |
try:
|
671 |
progress(0.1, desc="Creating chapter-labeled book")
|
672 |
except Exception as e:
|
673 |
print(f"Error updating progress: {e}")
|
674 |
|
|
|
675 |
create_chapter_labeled_book(ebook_file_path)
|
676 |
audiobook_output_path = os.path.join(".", "Audiobooks")
|
677 |
|
@@ -680,6 +688,7 @@ def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_m
|
|
680 |
except Exception as e:
|
681 |
print(f"Error updating progress: {e}")
|
682 |
|
|
|
683 |
if use_custom_model:
|
684 |
convert_chapters_to_audio_custom_model(chapters_directory, output_audio_directory, target_voice, language, custom_model)
|
685 |
else:
|
@@ -690,6 +699,7 @@ def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_m
|
|
690 |
except Exception as e:
|
691 |
print(f"Error updating progress: {e}")
|
692 |
|
|
|
693 |
create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
|
694 |
|
695 |
# Get the name of the created M4B file
|
@@ -700,10 +710,12 @@ def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_m
|
|
700 |
progress(1.0, desc="Conversion complete")
|
701 |
except Exception as e:
|
702 |
print(f"Error updating progress: {e}")
|
|
|
703 |
print(f"Audiobook created at {m4b_filepath}")
|
704 |
return f"Audiobook created at {m4b_filepath}", m4b_filepath
|
705 |
|
706 |
|
|
|
707 |
def list_audiobook_files(audiobook_folder):
|
708 |
# List all files in the audiobook folder
|
709 |
files = []
|
|
|
621 |
print(f"Converted chapter {chapter_num} to audio.")
|
622 |
|
623 |
|
|
|
|
|
624 |
def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url=None, progress=gr.Progress()):
|
625 |
+
# Handle ebook file and target voice file
|
626 |
ebook_file_path = ebook_file if isinstance(ebook_file, str) else ebook_file.name
|
627 |
target_voice = target_voice_file if isinstance(target_voice_file, str) else target_voice_file.name if target_voice_file else None
|
628 |
+
|
629 |
+
custom_model = None # Initialize custom_model as None
|
|
|
630 |
|
631 |
+
# Setup working directories
|
632 |
working_files = os.path.join(".", "Working_files", "temp_ebook")
|
633 |
full_folder_working_files = os.path.join(".", "Working_files")
|
634 |
chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
|
635 |
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
636 |
+
|
637 |
+
# Clean up previous working files
|
638 |
remove_folder_with_contents(full_folder_working_files)
|
639 |
remove_folder_with_contents(output_audio_directory)
|
640 |
|
641 |
+
# Handle custom model files (if provided)
|
642 |
+
custom_model_path = custom_model_file if isinstance(custom_model_file, str) else custom_model_file.name if custom_model_file else None
|
643 |
+
custom_config_path = custom_config_file if isinstance(custom_config_file, str) else custom_config_file.name if custom_config_file else None
|
644 |
+
custom_vocab_path = custom_vocab_file if isinstance(custom_vocab_file, str) else custom_vocab_file.name if custom_vocab_file else None
|
645 |
+
|
646 |
+
# Handle the download of custom model via URL (if provided)
|
647 |
if use_custom_model and custom_model_url:
|
648 |
print(f"Received custom model URL: {custom_model_url}")
|
649 |
download_dir = os.path.join(".", "Working_files", "custom_model")
|
|
|
653 |
if rename_vocab_file_if_exists(download_dir):
|
654 |
print("vocab.json file was found and renamed.")
|
655 |
|
656 |
+
custom_model_path = os.path.join(download_dir, 'model.pth')
|
657 |
+
custom_config_path = os.path.join(download_dir, 'config.json')
|
658 |
+
custom_vocab_path = os.path.join(download_dir, 'vocab.json_')
|
659 |
+
|
660 |
+
# Set the custom model if all the paths are available
|
661 |
+
if use_custom_model and custom_model_path and custom_config_path and custom_vocab_path:
|
662 |
custom_model = {
|
663 |
+
'model': custom_model_path,
|
664 |
+
'config': custom_config_path,
|
665 |
+
'vocab': custom_vocab_path
|
666 |
}
|
667 |
|
668 |
try:
|
|
|
670 |
except Exception as e:
|
671 |
print(f"Error updating progress: {e}")
|
672 |
|
673 |
+
# Ensure Calibre is installed
|
674 |
if not calibre_installed():
|
675 |
return "Calibre is not installed."
|
676 |
|
|
|
677 |
try:
|
678 |
progress(0.1, desc="Creating chapter-labeled book")
|
679 |
except Exception as e:
|
680 |
print(f"Error updating progress: {e}")
|
681 |
|
682 |
+
# Create a chapter-labeled book
|
683 |
create_chapter_labeled_book(ebook_file_path)
|
684 |
audiobook_output_path = os.path.join(".", "Audiobooks")
|
685 |
|
|
|
688 |
except Exception as e:
|
689 |
print(f"Error updating progress: {e}")
|
690 |
|
691 |
+
# Convert chapters to audio (custom or standard model)
|
692 |
if use_custom_model:
|
693 |
convert_chapters_to_audio_custom_model(chapters_directory, output_audio_directory, target_voice, language, custom_model)
|
694 |
else:
|
|
|
699 |
except Exception as e:
|
700 |
print(f"Error updating progress: {e}")
|
701 |
|
702 |
+
# Create an M4B from the chapters
|
703 |
create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
|
704 |
|
705 |
# Get the name of the created M4B file
|
|
|
710 |
progress(1.0, desc="Conversion complete")
|
711 |
except Exception as e:
|
712 |
print(f"Error updating progress: {e}")
|
713 |
+
|
714 |
print(f"Audiobook created at {m4b_filepath}")
|
715 |
return f"Audiobook created at {m4b_filepath}", m4b_filepath
|
716 |
|
717 |
|
718 |
+
|
719 |
def list_audiobook_files(audiobook_folder):
|
720 |
# List all files in the audiobook folder
|
721 |
files = []
|