Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line23456789101112131 unmodified line15161718192021140 unmodified lines16216316416516616716816917017117217317417517657 unmodified lines234235236237238239240441 unmodified lines6826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587591 unmodified lineimport argparse# Argument parser to handle optional parametersparser = argparse.ArgumentParser(description="Launch the Gradio app with optional share parameter.")parser.add_argument("--share", type=bool, default=False, help="Set to True to enable Gradio share link.")args = parser.parse_args()import osimport shutilimport subprocess1 unmodified linefrom pydub import AudioSegmentimport tempfilefrom pydub import AudioSegmentimport osimport nltkfrom nltk.tokenize import sent_tokenizeimport sys140 unmodified linesexcept Exception as e:print(f"Error extracting eBook metadata or cover: {e}")return None# Combine WAV files into a single filedef combine_wav_files(chapter_files, output_path):# Initialize an empty audio segmentcombined_audio = AudioSegment.empty()# Sequentially append each file to the combined_audiofor chapter_file in chapter_files:audio_segment = AudioSegment.from_wav(chapter_file)combined_audio += audio_segment# Export the combined audio to the output file pathcombined_audio.export(output_path, format='wav')print(f"Combined audio saved to {output_path}")57 unmodified lines#this code right here isnt the book grabbing thing but its before to refrence in ordero to create the sepecial chapter labeled book thing with calibre idk some systems cant seem to get it so just in case but the next bit of code after this is the book grabbing code with booknlpimport osimport subprocessimport ebooklib441 unmodified linesreturn list_audiobook_files(audiobook_output_path)language_options = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko"]theme = gr.themes.Soft(primary_hue="blue",secondary_hue="blue",neutral_hue="blue",text_size=gr.themes.sizes.text_md,)# Gradio UI setupwith gr.Blocks(theme=theme) as demo:gr.Markdown("""# eBook to Audiobook ConverterTransform your eBooks into immersive audiobooks with optional custom TTS models.This interface is based on [Ebook2AudioBookXTTS](https://github.com/DrewThomasson/ebook2audiobookXTTS).""")with gr.Row():with gr.Column(scale=3):ebook_file = gr.File(label="eBook File")target_voice_file = gr.File(label="Target Voice File (Optional)")language = gr.Dropdown(label="Language", choices=language_options, value="en")with gr.Column(scale=3):use_custom_model = gr.Checkbox(label="Use Custom Model")custom_model_file = gr.File(label="Custom Model File (Optional)", visible=False)custom_config_file = gr.File(label="Custom Config File (Optional)", visible=False)custom_vocab_file = gr.File(label="Custom Vocab File (Optional)", visible=False)custom_model_url = gr.Textbox(label="Custom Model Zip URL (Optional)", visible=False)convert_btn = gr.Button("Convert to Audiobook", variant="primary")output = gr.Textbox(label="Conversion Status")audio_player = gr.Audio(label="Audiobook Player", type="filepath")download_btn = gr.Button("Download Audiobook Files")download_files = gr.File(label="Download Files", interactive=False)convert_btn.click(convert_ebook_to_audio,inputs=[ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url],outputs=[output, audio_player])use_custom_model.change(lambda x: [gr.update(visible=x)] * 4,inputs=[use_custom_model],outputs=[custom_model_file, custom_config_file, custom_vocab_file, custom_model_url])download_btn.click(download_audiobooks,outputs=[download_files])#demo.launch(share=True)#demo.launch() # Removing share = True for Gradio Interface# Get the correct local IP or localhosthostname = socket.gethostname()local_ip = socket.gethostbyname(hostname)# Ensure Gradio runs and prints the correct local IPprint(f"Running on local URL: http://{local_ip}:7860")print(f"Running on local URL: http://localhost:7860")# Your Gradio launch commanddemo.launch(server_name="0.0.0.0", server_port=7860, share=args.share)1 unmodified line234567891011121314151617181920212223242526272829303132333435363738391 unmodified line414243444546140 unmodified lines18718818919019119219319419519619719819920020120220320420520620720820921057 unmodified lines268269270271272273274441 unmodified lines7167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138141 unmodified lineimport argparselanguage_options = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko"]# Convert the list of languages to a string to display in the help textlanguage_options_str = ", ".join(language_options)# Argument parser to handle optional parameters with descriptionsparser = argparse.ArgumentParser(description="Convert eBooks to Audiobooks using a Text-to-Speech model. You can either launch the Gradio interface or run the script in headless mode for direct conversion.",epilog="Example: python script.py --headless --ebook path_to_ebook --voice path_to_voice --language en --use_custom_model True --custom_model model.pth --custom_config config.json --custom_vocab vocab.json")parser.add_argument("--share", type=bool, default=False, help="Set to True to enable a public shareable Gradio link. Defaults to False.")parser.add_argument("--headless", type=bool, default=False, help="Set to True to run in headless mode without the Gradio interface. Defaults to False.")parser.add_argument("--ebook", type=str, help="Path to the ebook file for conversion. Required in headless mode.")parser.add_argument("--voice", type=str, help="Path to the target voice file for TTS. Optional, uses a default voice if not provided.")parser.add_argument("--language", type=str, default="en",help=f"Language for the audiobook conversion. Options: {language_options_str}. Defaults to English (en).")parser.add_argument("--use_custom_model", type=bool, default=False,help="Set to True to use a custom TTS model. Defaults to False. Must be True to use custom models, otherwise you'll get an error.")parser.add_argument("--custom_model", type=str, help="Path to the custom model file (.pth). Required if using a custom model.")parser.add_argument("--custom_config", type=str, help="Path to the custom config file (config.json). Required if using a custom model.")parser.add_argument("--custom_vocab", type=str, help="Path to the custom vocab file (vocab.json). Required if using a custom model.")parser.add_argument("--custom_model_url", type=str,help=("URL to download the custom model as a zip file. Optional, but will be used if provided. ""Examples include David Attenborough's model: ""'https://huggingface.co/drewThomasson/xtts_David_Attenborough_fine_tune/resolve/main/Finished_model_files.zip?download=true'. ""More XTTS fine-tunes can be found on my Hugging Face at 'https://huggingface.co/drewThomasson'."))args = parser.parse_args()import osimport shutilimport subprocess1 unmodified linefrom pydub import AudioSegmentimport tempfilefrom pydub import AudioSegmentimport nltkfrom nltk.tokenize import sent_tokenizeimport sys140 unmodified linesexcept Exception as e:print(f"Error extracting eBook metadata or cover: {e}")return None# Combine WAV files into a single filedef combine_wav_files(chapter_files, output_path, batch_size=256):# Initialize an empty audio segmentcombined_audio = AudioSegment.empty()# Process the chapter files in batchesfor i in range(0, len(chapter_files), batch_size):batch_files = chapter_files[i:i + batch_size]batch_audio = AudioSegment.empty() # Initialize an empty AudioSegment for the batch# Sequentially append each file in the current batch to the batch_audiofor chapter_file in batch_files:audio_segment = AudioSegment.from_wav(chapter_file)batch_audio += audio_segment# Combine the batch audio with the overall combined_audiocombined_audio += batch_audio# Export the combined audio to the output file pathcombined_audio.export(output_path, format='wav')print(f"Combined audio saved to {output_path}")57 unmodified lines#this code right here isnt the book grabbing thing but its before to refrence in order to create the sepecial chapter labeled book thing with calibre idk some systems cant seem to get it so just in case but the next bit of code after this is the book grabbing code with booknlpimport osimport subprocessimport ebooklib441 unmodified linesreturn list_audiobook_files(audiobook_output_path)# Gradio UI setupdef run_gradio_interface():language_options = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko"]theme = gr.themes.Soft(primary_hue="blue",secondary_hue="blue",neutral_hue="blue",text_size=gr.themes.sizes.text_md,)with gr.Blocks(theme=theme) as demo:gr.Markdown("""# eBook to Audiobook ConverterTransform your eBooks into immersive audiobooks with optional custom TTS models.This interface is based on [Ebook2AudioBookXTTS](https://github.com/DrewThomasson/ebook2audiobookXTTS).""")with gr.Row():with gr.Column(scale=3):ebook_file = gr.File(label="eBook File")target_voice_file = gr.File(label="Target Voice File (Optional)")language = gr.Dropdown(label="Language", choices=language_options, value="en")with gr.Column(scale=3):use_custom_model = gr.Checkbox(label="Use Custom Model")custom_model_file = gr.File(label="Custom Model File (Optional)", visible=False)custom_config_file = gr.File(label="Custom Config File (Optional)", visible=False)custom_vocab_file = gr.File(label="Custom Vocab File (Optional)", visible=False)custom_model_url = gr.Textbox(label="Custom Model Zip URL (Optional)", visible=False)convert_btn = gr.Button("Convert to Audiobook", variant="primary")output = gr.Textbox(label="Conversion Status")audio_player = gr.Audio(label="Audiobook Player", type="filepath")download_btn = gr.Button("Download Audiobook Files")download_files = gr.File(label="Download Files", interactive=False)convert_btn.click(convert_ebook_to_audio,inputs=[ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url],outputs=[output, audio_player])use_custom_model.change(lambda x: [gr.update(visible=x)] * 4,inputs=[use_custom_model],outputs=[custom_model_file, custom_config_file, custom_vocab_file, custom_model_url])download_btn.click(download_audiobooks,outputs=[download_files])# Get the correct local IP or localhosthostname = socket.gethostname()local_ip = socket.gethostbyname(hostname)# Ensure Gradio runs and prints the correct local IPprint(f"Running on local URL: http://{local_ip}:7860")print(f"Running on local URL: http://localhost:7860")# Launch Gradio appdemo.launch(server_name="0.0.0.0", server_port=7860, share=args.share)# Check if running in headless modeif args.headless:if not args.ebook:print("Error: In headless mode, you must specify an ebook file using --ebook.")exit(1)ebook_file_path = args.ebooktarget_voice = args.voice if args.voice else Nonecustom_model = Noneif args.use_custom_model:custom_model = {'model': args.custom_model,'config': args.custom_config,'vocab': args.custom_vocab}# Example headless executionconvert_ebook_to_audio(ebook_file_path, target_voice, args.language, args.use_custom_model, args.custom_model, args.custom_config, args.custom_vocab)else:# Launch Gradio UIrun_gradio_interface()
app.py+136-81