epub-to-text / app.py
dwarkesh's picture
Update app.py
1b65e7d verified
raw
history blame
2.11 kB
import gradio as gr
import ebooklib
from ebooklib import epub
from bs4 import BeautifulSoup
import re
def extract_text_from_html(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
return soup.get_text()
def get_chapters(epub_file):
book = epub.read_epub(epub_file.name)
chapters = []
for item in book.get_items():
if item.get_type() == ebooklib.ITEM_DOCUMENT:
content = item.get_content().decode('utf-8')
title = BeautifulSoup(content, 'html.parser').find('title')
title = title.string if title else f"Chapter {len(chapters) + 1}"
text = extract_text_from_html(content)
chapters.append((title, len(text), text))
return chapters
def display_chapters(epub_file):
chapters = get_chapters(epub_file)
chapter_list = [f"{title} ({length} characters)" for title, length, _ in chapters]
return chapter_list, gr.Dropdown.update(choices=chapter_list)
def get_chapter_content(epub_file, selected_chapter):
chapters = get_chapters(epub_file)
for title, _, content in chapters:
if f"{title} (" in selected_chapter: # Match the title part of the dropdown option
return content
return "Chapter not found"
def create_interface():
with gr.Blocks() as interface:
gr.Markdown("# EPUB Chapter Extractor")
with gr.Row():
epub_input = gr.File(label="Upload EPUB File")
chapters_output = gr.Textbox(label="Available Chapters", interactive=False)
chapter_dropdown = gr.Dropdown(label="Select a chapter", choices=[], interactive=True)
epub_input.upload(display_chapters, epub_input, [chapters_output, chapter_dropdown])
read_button = gr.Button("Read Chapter")
chapter_content = gr.Textbox(label="Chapter Content", interactive=False)
read_button.click(get_chapter_content, inputs=[epub_input, chapter_dropdown], outputs=chapter_content)
return interface
if __name__ == "__main__":
app = create_interface()
app.launch()