Spaces:
Paused
Paused
| import gradio as gr | |
| import subprocess | |
| import os | |
| # Supported input and output formats for Calibre | |
| input_formats = ["azw", "azw3", "azw4", "cbz", "cbr", "cb7", "cbc", "chm", "djvu", "docx", "epub", "fb2", "fbz", "html", "htmlz", "lit", "lrf", "mobi", "odt", "pdf", "prc", "pdb", "pml", "rb", "rtf", "snb", "tcr", "txt", "txtz"] | |
| output_formats = ["AZW3", "EPUB", "DOCX", "FB2", "HTMLZ", "OEB", "LIT", "LRF", "MOBI", "PDB", "PMLZ", "RB", "PDF", "RTF", "SNB", "TCR", "TXT", "TXTZ", "ZIP"] | |
| def convert_ebook(input_file, output_format): | |
| output_file = os.path.splitext(input_file.name)[0] + "." + output_format.lower() | |
| # Execute Calibre ebook-convert command | |
| command = ['ebook-convert', input_file.name, output_file] | |
| result = subprocess.run(command, capture_output=True, text=True) | |
| if result.returncode != 0: | |
| return None, f"Error: {result.stderr}" | |
| # Return the converted file | |
| return output_file, "Conversion successful! You can download your file below." | |
| with gr.Blocks() as gui: | |
| gr.Markdown("## Ebook Converter\nThis interface uses Calibre to convert ebooks. Supported input formats: AZW, AZW3, AZW4, CBZ, CBR, CB7, CBC, CHM, DJVU, DOCX, EPUB, FB2, FBZ, HTML, HTMLZ, LIT, LRF, MOBI, ODT, PDF, PRC, PDB, PML, RB, RTF, SNB, TCR, TXT, TXTZ.\n\n[Calibre GitHub](https://github.com/kovidgoyal/calibre) | [Calibre Website](https://calibre-ebook.com)") | |
| # File upload (only accepts valid input formats) | |
| input_file = gr.File(label="Select Ebook File", file_types=input_formats) | |
| # Dropdown for output format | |
| output_format = gr.Dropdown(choices=output_formats, label="Choose Output Format") | |
| # Convert button | |
| convert_button = gr.Button("Convert") | |
| # Output for status messages | |
| status_message = gr.Textbox(label="Status", interactive=False) | |
| # Output file download | |
| output_file = gr.File(label="Converted Ebook", interactive=False) | |
| # Convert and display file for download or show success message | |
| convert_button.click(fn=convert_ebook, inputs=[input_file, output_format], outputs=[output_file, status_message]) | |
| # Launch the Gradio GUI | |
| gui.launch() | |