dwarkesh commited on
Commit
1b65e7d
1 Parent(s): f724589

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -24,12 +24,13 @@ def get_chapters(epub_file):
24
 
25
  def display_chapters(epub_file):
26
  chapters = get_chapters(epub_file)
27
- return "\n".join([f"{title} ({length} characters)" for title, length, _ in chapters])
 
28
 
29
  def get_chapter_content(epub_file, selected_chapter):
30
  chapters = get_chapters(epub_file)
31
  for title, _, content in chapters:
32
- if title in selected_chapter:
33
  return content
34
  return "Chapter not found"
35
 
@@ -40,15 +41,15 @@ def create_interface():
40
  epub_input = gr.File(label="Upload EPUB File")
41
  chapters_output = gr.Textbox(label="Available Chapters", interactive=False)
42
 
43
- epub_input.upload(display_chapters, epub_input, chapters_output)
44
 
45
- with gr.Row():
46
- chapter_select = gr.Textbox(label="Enter the chapter title you want to read")
47
- read_button = gr.Button("Read Chapter")
48
 
49
  chapter_content = gr.Textbox(label="Chapter Content", interactive=False)
50
 
51
- read_button.click(get_chapter_content, inputs=[epub_input, chapter_select], outputs=chapter_content)
52
 
53
  return interface
54
 
 
24
 
25
  def display_chapters(epub_file):
26
  chapters = get_chapters(epub_file)
27
+ chapter_list = [f"{title} ({length} characters)" for title, length, _ in chapters]
28
+ return chapter_list, gr.Dropdown.update(choices=chapter_list)
29
 
30
  def get_chapter_content(epub_file, selected_chapter):
31
  chapters = get_chapters(epub_file)
32
  for title, _, content in chapters:
33
+ if f"{title} (" in selected_chapter: # Match the title part of the dropdown option
34
  return content
35
  return "Chapter not found"
36
 
 
41
  epub_input = gr.File(label="Upload EPUB File")
42
  chapters_output = gr.Textbox(label="Available Chapters", interactive=False)
43
 
44
+ chapter_dropdown = gr.Dropdown(label="Select a chapter", choices=[], interactive=True)
45
 
46
+ epub_input.upload(display_chapters, epub_input, [chapters_output, chapter_dropdown])
47
+
48
+ read_button = gr.Button("Read Chapter")
49
 
50
  chapter_content = gr.Textbox(label="Chapter Content", interactive=False)
51
 
52
+ read_button.click(get_chapter_content, inputs=[epub_input, chapter_dropdown], outputs=chapter_content)
53
 
54
  return interface
55