spagestic commited on
Commit
c2ba9da
Β·
1 Parent(s): 57d77b7

feat: Add explanation generation and audio output features to PDF extractor

Browse files
Files changed (1) hide show
  1. _app.py +122 -12
_app.py CHANGED
@@ -7,6 +7,7 @@ load_dotenv()
7
 
8
  def main():
9
  """Main function to create and launch the interface."""
 
10
  def process_pdf(pdf_file):
11
  """Process PDF and extract text automatically"""
12
  if pdf_file is None:
@@ -24,8 +25,69 @@ def main():
24
  except Exception as e:
25
  return "", f"Error processing PDF: {str(e)}"
26
 
27
- # Create the interface with side-by-side layout
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  with gr.Blocks(title="πŸ” PDF Text Extractor", theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  gr.Markdown("# πŸ” PDF Text Extractor")
30
  gr.Markdown("Upload a PDF on the left to automatically extract and view text on the right.")
31
 
@@ -45,24 +107,72 @@ def main():
45
  placeholder="Upload a PDF to see status...",
46
  interactive=False
47
  )
48
-
49
- # Right column - Extracted Text
50
  with gr.Column(scale=1):
51
- gr.Markdown("### πŸ“ Extracted Text")
52
- text_output = gr.Textbox(
53
- label="Extracted Text",
54
- lines=25,
55
- placeholder="Upload a PDF to automatically extract text...",
56
- show_copy_button=True,
57
- interactive=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  )
59
-
60
- # Set up automatic processing on PDF upload
61
  pdf_input.upload(
62
  fn=process_pdf,
63
  inputs=[pdf_input],
64
  outputs=[text_output, status_output]
65
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  return demo
68
 
 
7
 
8
  def main():
9
  """Main function to create and launch the interface."""
10
+
11
  def process_pdf(pdf_file):
12
  """Process PDF and extract text automatically"""
13
  if pdf_file is None:
 
25
  except Exception as e:
26
  return "", f"Error processing PDF: {str(e)}"
27
 
28
+ def generate_explanations(extracted_text):
29
+ """Generate explanations for extracted text"""
30
+ if not extracted_text or extracted_text.strip() == "":
31
+ return "No text available to explain. Please extract text from a PDF first."
32
+
33
+ try:
34
+ # Initialize extractor
35
+ extractor = PDFTextExtractor()
36
+
37
+ # Generate explanations
38
+ explanations = extractor.generate_explanations(extracted_text)
39
+ return explanations
40
+
41
+ except Exception as e:
42
+ return f"Error generating explanations: {str(e)}"
43
+
44
+ def generate_audio(explanation_text):
45
+ """Generate TTS audio for explanations"""
46
+ if not explanation_text or explanation_text.strip() == "":
47
+ raise gr.Error("No explanations available to convert to audio. Please generate explanations first.")
48
+
49
+ try:
50
+ # Import the TTS function
51
+ from ui.chatterbox.generate_tts_audio import generate_tts_audio
52
+
53
+ # Clean up the text for better TTS
54
+ clean_text = explanation_text.strip()
55
+
56
+ # Limit text length for TTS (assuming 1000 character limit)
57
+ if len(clean_text) > 1000:
58
+ # Truncate at sentence boundary if possible
59
+ sentences = clean_text[:950].split('.')
60
+ if len(sentences) > 1:
61
+ clean_text = '.'.join(sentences[:-1]) + '.'
62
+ else:
63
+ clean_text = clean_text[:950]
64
+ clean_text += " [Text has been truncated for audio generation]"
65
+
66
+ # Generate audio and make it visible
67
+ audio_result = generate_tts_audio(clean_text, None)
68
+ return audio_result, gr.update(visible=True)
69
+
70
+ except Exception as e:
71
+ raise gr.Error(f"Error generating audio: {str(e)}")
72
+ # Create the interface with side-by-side layout
73
  with gr.Blocks(title="πŸ” PDF Text Extractor", theme=gr.themes.Soft()) as demo:
74
+ # Inject fullscreen CSS
75
+ gr.HTML("""
76
+ <style>
77
+ html, body, #root, .gradio-container {
78
+ height: 100% !important;
79
+ width: 100% !important;
80
+ margin: 0 !important;
81
+ padding: 0 !important;
82
+ }
83
+ .gradio-container {
84
+ max-width: 100vw !important;
85
+ min-height: 100vh !important;
86
+ box-sizing: border-box;
87
+ }
88
+ </style>
89
+ """)
90
+
91
  gr.Markdown("# πŸ” PDF Text Extractor")
92
  gr.Markdown("Upload a PDF on the left to automatically extract and view text on the right.")
93
 
 
107
  placeholder="Upload a PDF to see status...",
108
  interactive=False
109
  )
110
+ # Right column - Extracted Content with Tabs
 
111
  with gr.Column(scale=1):
112
+ gr.Markdown("### πŸ“ Extracted Content")
113
+
114
+ with gr.Tabs():
115
+ with gr.TabItem("Extracted Text"):
116
+ text_output = gr.Textbox(
117
+ label="Extracted Text",
118
+ lines=20,
119
+ placeholder="Upload a PDF to automatically extract text...",
120
+ show_copy_button=True,
121
+ interactive=False
122
+ )
123
+
124
+ with gr.TabItem("Explanation Script"):
125
+ with gr.Row():
126
+ generate_explanation_btn = gr.Button(
127
+ "πŸ€– Generate Explanation Script",
128
+ variant="primary",
129
+ size="lg"
130
+ )
131
+
132
+ explanation_output = gr.Textbox(
133
+ label="Generated Explanation Script",
134
+ lines=15,
135
+ placeholder="Click 'Generate Explanation Script' after extracting text to get explanations...",
136
+ show_copy_button=True,
137
+ interactive=False
138
+ )
139
+
140
+ # Audio generation section (below tabs)
141
+ gr.Markdown("### πŸ”Š Audio Generation")
142
+ with gr.Row():
143
+ generate_audio_btn = gr.Button(
144
+ "🎡 Generate Explanation Audio",
145
+ variant="secondary",
146
+ size="lg"
147
+ )
148
+
149
+ audio_output = gr.Audio(
150
+ label="Generated Explanation Audio",
151
+ interactive=False,
152
+ visible=False
153
  )
154
+ # Set up automatic processing on PDF upload
 
155
  pdf_input.upload(
156
  fn=process_pdf,
157
  inputs=[pdf_input],
158
  outputs=[text_output, status_output]
159
  )
160
+
161
+ # Set up explanation generation button
162
+ generate_explanation_btn.click(
163
+ fn=generate_explanations,
164
+ inputs=[text_output],
165
+ outputs=[explanation_output],
166
+ show_progress=True
167
+ )
168
+
169
+ # Set up audio generation button
170
+ generate_audio_btn.click(
171
+ fn=generate_audio,
172
+ inputs=[explanation_output],
173
+ outputs=[audio_output, audio_output],
174
+ show_progress=True
175
+ )
176
 
177
  return demo
178