amendolajine commited on
Commit
3abff09
·
1 Parent(s): 6c26e5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -31
app.py CHANGED
@@ -1,56 +1,64 @@
1
- #Required imports
2
  import gradio as gr
3
  import fitz # PyMuPDF
4
  from transformers import BartTokenizer, BartForConditionalGeneration, pipeline
5
  import scipy.io.wavfile
6
  import numpy as np
7
 
 
 
 
8
  # Initialize tokenizers and models
9
  tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
10
  model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
11
  synthesiser = pipeline("text-to-speech", "suno/bark")
12
 
13
- # Function to extract abstract from PDF
14
  def extract_abstract(pdf_bytes):
15
- # Open PDF with fitz
16
- doc = fitz.open(stream=pdf_bytes, filetype="pdf")
17
- first_page = doc[0].get_text()
18
- start_idx = first_page.lower().find("abstract")
19
- end_idx = first_page.lower().find("introduction")
20
- if start_idx != -1 and end_idx != -1:
21
- return first_page[start_idx:end_idx].strip()
22
- else:
23
- return "Abstract not found or 'Introduction' not found in the first page."
24
-
25
- # Function to process text (summarize and convert to speech)
 
 
26
  def process_text(uploaded_file):
27
- # Attempt to extract byte content from NamedString object
28
  try:
29
  pdf_bytes = uploaded_file.file.read()
30
- except AttributeError:
31
- print("Error reading file content from uploaded_file")
 
32
  return "File content could not be retrieved", None
33
 
34
- # Extract abstract from PDF
35
- abstract_text = extract_abstract(pdf_bytes)
 
 
 
 
36
 
37
- # Generate summary
38
- inputs = tokenizer([abstract_text], max_length=1024, return_tensors='pt', truncation=True)
39
- summary_ids = model.generate(inputs['input_ids'], num_beams=4, max_length=40, min_length=10, length_penalty=2.0, early_stopping=True, no_repeat_ngram_size=2)
40
- summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
41
 
42
- # Convert summary to speech
43
- speech = synthesiser(summary, forward_params={"do_sample": True})
44
- audio_data = speech["audio"].squeeze()
45
- normalized_audio_data = np.int16(audio_data / np.max(np.abs(audio_data)) * 32767)
46
 
47
- # Save audio to temporary file
48
- output_file = "temp_output.wav"
49
- scipy.io.wavfile.write(output_file, rate=speech["sampling_rate"], data=normalized_audio_data)
50
 
51
- return summary, output_file
 
 
 
52
 
53
- # Gradio Interface
54
  iface = gr.Interface(
55
  fn=process_text,
56
  inputs=gr.components.File(label="Upload PDF"),
 
1
+ import logging
2
  import gradio as gr
3
  import fitz # PyMuPDF
4
  from transformers import BartTokenizer, BartForConditionalGeneration, pipeline
5
  import scipy.io.wavfile
6
  import numpy as np
7
 
8
+ # Initialize logging
9
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
10
+
11
  # Initialize tokenizers and models
12
  tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
13
  model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
14
  synthesiser = pipeline("text-to-speech", "suno/bark")
15
 
 
16
  def extract_abstract(pdf_bytes):
17
+ try:
18
+ doc = fitz.open(stream=pdf_bytes, filetype="pdf")
19
+ first_page = doc[0].get_text()
20
+ start_idx = first_page.lower().find("abstract")
21
+ end_idx = first_page.lower().find("introduction")
22
+ if start_idx != -1 and end_idx != -1:
23
+ return first_page[start_idx:end_idx].strip()
24
+ else:
25
+ return "Abstract not found or 'Introduction' not found in the first page."
26
+ except Exception as e:
27
+ logging.error(f"Error extracting abstract: {e}")
28
+ return "Error in abstract extraction"
29
+
30
  def process_text(uploaded_file):
 
31
  try:
32
  pdf_bytes = uploaded_file.file.read()
33
+ logging.info("PDF file read successfully")
34
+ except AttributeError as e:
35
+ logging.error(f"Error reading file content: {e}")
36
  return "File content could not be retrieved", None
37
 
38
+ try:
39
+ abstract_text = extract_abstract(pdf_bytes)
40
+ logging.info(f"Extracted abstract: {abstract_text[:100]}...") # Log first 100 chars of abstract
41
+ except Exception as e:
42
+ logging.error(f"Error in abstract extraction: {e}")
43
+ return "Error in processing PDF", None
44
 
45
+ try:
46
+ inputs = tokenizer([abstract_text], max_length=1024, return_tensors='pt', truncation=True)
47
+ summary_ids = model.generate(inputs['input_ids'], num_beams=4, max_length=40, min_length=10, length_penalty=2.0, early_stopping=True, no_repeat_ngram_size=2)
48
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
49
 
50
+ speech = synthesiser(summary, forward_params={"do_sample": True})
51
+ audio_data = speech["audio"].squeeze()
52
+ normalized_audio_data = np.int16(audio_data / np.max(np.abs(audio_data)) * 32767)
 
53
 
54
+ output_file = "temp_output.wav"
55
+ scipy.io.wavfile.write(output_file, rate=speech["sampling_rate"], data=normalized_audio_data)
 
56
 
57
+ return summary, output_file
58
+ except Exception as e:
59
+ logging.error(f"Error in summary generation or TTS conversion: {e}")
60
+ return "Error in summary or speech generation", None
61
 
 
62
  iface = gr.Interface(
63
  fn=process_text,
64
  inputs=gr.components.File(label="Upload PDF"),