yilmazmusa commited on
Commit
854fcbb
1 Parent(s): 92162ef

Add application file

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://huggingface.co/spaces/yilmazmusa_ml/abstract_summarizer
2
+
3
+ # Here are the imports
4
+ import warnings
5
+ import pdfplumber
6
+ import torch
7
+ from transformers import pipeline, AutoProcessor, AutoModel
8
+ import numpy as np
9
+ import gradio as gr
10
+ from io import BytesIO
11
+ from scipy.io.wavfile import write as write_wav
12
+ warnings.filterwarnings("ignore")
13
+
14
+
15
+ # Here is the code
16
+ def extract_abstract(uploaded_file):
17
+ pdf_bytes = BytesIO(uploaded_file)
18
+ with pdfplumber.open(pdf_bytes) as pdf:
19
+ abstract = ""
20
+ # Iterate through each page
21
+ for page in pdf.pages:
22
+ text = page.extract_text(x_tolerance = 1, y_tolerance = 1) # these parameters are set 1 in order to get spaces between words and lines
23
+ # Search for the "Abstract" keyword
24
+ if "abstract" in text.lower():
25
+ # Found the "Abstract" keyword
26
+ start_index = text.lower().find("abstract") # find the "abstract" title as starter index
27
+ end_index = text.lower().find("introduction") # find the "introduction" title as end index
28
+ abstract = text[start_index:end_index]
29
+ break
30
+ print(abstract)
31
+ return abstract
32
+
33
+ def process_summary(summary):
34
+ # Split the summary by the first period
35
+ summary = summary[0]["summary_text"]
36
+ sentences = summary.split('.', 1)
37
+ if len(sentences) > 0:
38
+ # Retrieve the first part before the period
39
+ processed_summary = sentences[0].strip() + "."
40
+ # Replace "-" with an empty string
41
+ processed_summary = processed_summary.replace("-", "")
42
+ return processed_summary
43
+
44
+ # Function for summarization and audio conversion
45
+ def summarize_and_convert_to_audio(pdf_file):
46
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
47
+ print(device)
48
+
49
+ # Move models and related tensors to CUDA device if available
50
+ processor = AutoProcessor.from_pretrained("suno/bark")
51
+ model = AutoModel.from_pretrained("suno/bark").to(device)
52
+
53
+ # Extract abstract
54
+ abstract_text = extract_abstract(pdf_file)
55
+
56
+ if not abstract_text:
57
+ return "No 'abstract' section found in the uploaded PDF. Please upload a different PDF."
58
+
59
+ # Summarize the abstract
60
+ summarization_pipeline = pipeline(task='summarization', model='knkarthick/MEETING_SUMMARY', min_length=15, max_length=30)
61
+ summarized_text = summarization_pipeline(abstract_text)
62
+ one_sentence_summary = process_summary(summarized_text)
63
+
64
+ # Text-to-audio conversion
65
+ inputs = processor(
66
+ text=[one_sentence_summary],
67
+ return_tensors="pt",
68
+ )
69
+ inputs = inputs.to(device)
70
+
71
+ speech_values = model.generate(**inputs, do_sample=True)
72
+ sampling_rate = model.generation_config.sample_rate
73
+
74
+ # Convert speech values to audio data
75
+ audio_data = speech_values.cpu().numpy().squeeze()
76
+
77
+ # Convert audio data to bytes
78
+ with BytesIO() as buffer:
79
+ write_wav(buffer, sampling_rate, audio_data.astype(np.float32))
80
+ audio_bytes = buffer.getvalue()
81
+
82
+ return audio_bytes # Return audio as bytes
83
+
84
+
85
+ # Create a Gradio interface
86
+ iface = gr.Interface(
87
+ fn=summarize_and_convert_to_audio,
88
+ inputs=gr.UploadButton(label="Upload PDF", type="bytes", file_types=["pdf"]), # Set to accept only PDF files
89
+ outputs=gr.Audio(label="Audio"),
90
+ title="PDF Abstract Summarizer",
91
+ description="Upload a PDF with an abstract to generate a summarized audio."
92
+ )
93
+
94
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.24.1
2
+ gradio==3.50.2
3
+ huggingface-hub==0.17.3
4
+ numpy==1.24.1
5
+ pdfplumber==0.10.3
6
+ torch==2.2.0
7
+ torchaudio==2.2.0
8
+ torchvision==0.17.0
9
+ transformers==4.35.0