spagestic commited on
Commit
37d50b6
·
1 Parent(s): c2ba9da

feat: Enhance PDF processing to automatically extract text, generate explanations, and produce audio

Browse files
Files changed (1) hide show
  1. _app.py +52 -42
_app.py CHANGED
@@ -7,23 +7,65 @@ load_dotenv()
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:
14
- return "", "No PDF uploaded"
 
15
 
16
  try:
17
- # Initialize extractor
18
  extractor = PDFTextExtractor()
19
 
20
- # Extract text from PDF
 
 
21
  extracted_text, status, images_data = extractor.extract_text_from_pdf(pdf_file)
22
 
23
- return extracted_text, status
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
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"""
@@ -120,58 +162,26 @@ def main():
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
 
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, then explanations, then audio, updating UI at each step."""
12
  if pdf_file is None:
13
+ yield "", "No PDF uploaded", "", None, gr.update(visible=False)
14
+ return
15
 
16
  try:
 
17
  extractor = PDFTextExtractor()
18
 
19
+ # Step 1: Extract text
20
+ # Show "Extracting text..." message
21
+ yield "", gr.update(value="Extracting text..."), "", None, gr.update(visible=False)
22
  extracted_text, status, images_data = extractor.extract_text_from_pdf(pdf_file)
23
 
24
+ if not extracted_text or extracted_text.strip() == "":
25
+ yield extracted_text, status, "No text available to explain.", None, gr.update(visible=False)
26
+ return
27
+
28
+ # Show extracted text immediately, explanations/audio loading
29
+ yield extracted_text, status, gr.update(value="Generating explanations..."), None, gr.update(visible=False)
30
+
31
+ # Step 2: Generate explanations
32
+ try:
33
+ explanations = extractor.generate_explanations(extracted_text)
34
+
35
+ # Show explanations immediately, audio loading
36
+ yield extracted_text, status, explanations, gr.update(value="Generating audio..."), gr.update(visible=False)
37
+
38
+ # Step 3: Generate audio
39
+ try:
40
+ from ui.chatterbox.generate_tts_audio import generate_tts_audio
41
+
42
+ # Clean up the text for better TTS
43
+ clean_text = explanations.strip()
44
+
45
+ # Limit text length for TTS (assuming 1000 character limit)
46
+ if len(clean_text) > 1000:
47
+ sentences = clean_text[:950].split('.')
48
+ if len(sentences) > 1:
49
+ clean_text = '.'.join(sentences[:-1]) + '.'
50
+ else:
51
+ clean_text = clean_text[:950]
52
+ clean_text += " [Text has been truncated for audio generation]"
53
+
54
+ audio_result = generate_tts_audio(clean_text, None)
55
+
56
+ # Show everything
57
+ yield extracted_text, status, explanations, audio_result, gr.update(visible=True)
58
+
59
+ except Exception as audio_error:
60
+ # Show explanations, but indicate audio error
61
+ yield extracted_text, status, explanations, f"Error generating audio: {str(audio_error)}", gr.update(visible=False)
62
+
63
+ except Exception as explanation_error:
64
+ # Show extracted text, but indicate explanation error
65
+ yield extracted_text, status, f"Error generating explanations: {str(explanation_error)}", None, gr.update(visible=False)
66
 
67
  except Exception as e:
68
+ yield "", f"Error processing PDF: {str(e)}", "", None, gr.update(visible=False)
69
 
70
  def generate_explanations(extracted_text):
71
  """Generate explanations for extracted text"""
 
162
  show_copy_button=True,
163
  interactive=False
164
  )
 
165
  with gr.TabItem("Explanation Script"):
 
 
 
 
 
 
 
166
  explanation_output = gr.Textbox(
167
  label="Generated Explanation Script",
168
  lines=15,
169
+ placeholder="Explanations will be automatically generated after text extraction...",
170
  show_copy_button=True,
171
  interactive=False
172
  )
173
 
174
  # Audio generation section (below tabs)
175
  gr.Markdown("### 🔊 Audio Generation")
 
 
 
 
 
 
 
176
  audio_output = gr.Audio(
177
  label="Generated Explanation Audio",
178
  interactive=False,
179
  visible=False
180
+ ) # Set up automatic processing on PDF upload (now handles all steps)
 
181
  pdf_input.upload(
182
  fn=process_pdf,
183
  inputs=[pdf_input],
184
+ outputs=[text_output, status_output, explanation_output, audio_output, audio_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  )
186
 
187
  return demo