amendolajine commited on
Commit
1842832
·
1 Parent(s): 8b2d29b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -28,17 +28,16 @@ def extract_abstract(pdf_bytes):
28
  return "Error in abstract extraction"
29
 
30
  def process_text(uploaded_file):
31
- # Debugging: Print the type and path of the uploaded_file
32
  print(f"Uploaded file type: {type(uploaded_file)}")
33
- print(f"Uploaded file path: {uploaded_file}")
34
 
35
- # Read PDF file from the path
36
- try:
37
- with open(uploaded_file, "rb") as file:
38
- pdf_bytes = file.read()
39
- except Exception as e:
40
- print(f"Error reading file from path: {e}")
41
- return "Error reading PDF file", None
42
 
43
  try:
44
  abstract_text = extract_abstract(pdf_bytes)
@@ -48,14 +47,29 @@ def process_text(uploaded_file):
48
  return "Error in processing PDF", None
49
 
50
  try:
51
- inputs = tokenizer([abstract_text], max_length=1024, return_tensors='pt', truncation=True)
52
- 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)
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
54
 
 
55
  speech = synthesiser(summary, forward_params={"do_sample": True})
56
  audio_data = speech["audio"].squeeze()
57
  normalized_audio_data = np.int16(audio_data / np.max(np.abs(audio_data)) * 32767)
58
 
 
59
  output_file = "temp_output.wav"
60
  scipy.io.wavfile.write(output_file, rate=speech["sampling_rate"], data=normalized_audio_data)
61
 
 
28
  return "Error in abstract extraction"
29
 
30
  def process_text(uploaded_file):
31
+ # Debugging: Print the type and contents of the uploaded_file
32
  print(f"Uploaded file type: {type(uploaded_file)}")
33
+ print(f"Uploaded file content: {uploaded_file}")
34
 
35
+ # Check if uploaded_file is a dictionary with 'data' key
36
+ if isinstance(uploaded_file, dict) and 'data' in uploaded_file:
37
+ pdf_bytes = uploaded_file['data']
38
+ else:
39
+ print("Uploaded file is not in the expected format")
40
+ return "File content could not be retrieved", None
 
41
 
42
  try:
43
  abstract_text = extract_abstract(pdf_bytes)
 
47
  return "Error in processing PDF", None
48
 
49
  try:
50
+ # Prepare inputs for the model
51
+ inputs = tokenizer([abstract_text], max_length=1024, return_tensors='pt', truncation=True, padding="max_length")
52
+
53
+ # Generate summary
54
+ summary_ids = model.generate(
55
+ input_ids=inputs['input_ids'],
56
+ attention_mask=inputs['attention_mask'], # Include attention mask
57
+ pad_token_id=model.config.pad_token_id, # Include pad token id
58
+ num_beams=4,
59
+ max_length=40,
60
+ min_length=10,
61
+ length_penalty=2.0,
62
+ early_stopping=True,
63
+ no_repeat_ngram_size=2
64
+ )
65
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
66
 
67
+ # Convert summary to speech
68
  speech = synthesiser(summary, forward_params={"do_sample": True})
69
  audio_data = speech["audio"].squeeze()
70
  normalized_audio_data = np.int16(audio_data / np.max(np.abs(audio_data)) * 32767)
71
 
72
+ # Save audio to temporary file
73
  output_file = "temp_output.wav"
74
  scipy.io.wavfile.write(output_file, rate=speech["sampling_rate"], data=normalized_audio_data)
75