Spaces:
Runtime error
Runtime error
File size: 1,965 Bytes
07c0793 d5f6764 07c0793 1b65e7d d5f6764 07c0793 1b65e7d 07c0793 1b65e7d 07c0793 d5f6764 1b65e7d 07c0793 1b65e7d 07c0793 |
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 |
import gradio as gr
import ebooklib
from ebooklib import epub
from bs4 import BeautifulSoup
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 update_dropdown(epub_file):
chapters = get_chapters(epub_file)
chapter_list = [f"{title} ({length} characters)" for title, length, _ in chapters]
return gr.Dropdown(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")
chapter_dropdown = gr.Dropdown(label="Select a chapter", choices=[], interactive=True)
epub_input.upload(update_dropdown, epub_input, 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() |