Xuratron commited on
Commit
6616d67
·
1 Parent(s): c7deed6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #https://huggingface.co/spaces/Xuratron/abstract-speech-summarizer
2
+
3
+ # Here are the imports
4
+ import PyPDF2
5
+ import re
6
+ import torch
7
+ from transformers import pipeline
8
+ import soundfile as sf
9
+ from fairseq.checkpoint_utils import load_model_ensemble_and_task_from_hf_hub
10
+ from fairseq.models.text_to_speech.hub_interface import TTSHubInterface
11
+ import gradio as gr
12
+
13
+
14
+ # Here is the code
15
+
16
+ def extract_and_clean_abstract(uploaded_file):
17
+ """
18
+ Extracts and cleans the abstract from the uploaded PDF file.
19
+ """
20
+ reader = PyPDF2.PdfReader(uploaded_file.file)
21
+ text = ""
22
+ for page in reader.pages:
23
+ text += page.extract_text() or ""
24
+
25
+ # Regular expression pattern to find the abstract
26
+ pattern = r"(Abstract|ABSTRACT|abstract)(.*?)(Introduction|INTRODUCTION|introduction|1|Keywords|KEYWORDS|keywords)"
27
+ match = re.search(pattern, text, re.DOTALL)
28
+
29
+ if match:
30
+ abstract = match.group(2).strip()
31
+ else:
32
+ abstract = "Abstract not found."
33
+
34
+ # Clean the abstract text
35
+ cleaned_abstract = abstract.replace('\n', ' ').replace('- ', '')
36
+
37
+ return cleaned_abstract
38
+
39
+ def summarize_text(hf_model_name, text):
40
+ """
41
+ Summarizes the given text using a Hugging Face model.
42
+ """
43
+ summarizer = pipeline("summarization", model=hf_model_name)
44
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
45
+ return summary
46
+
47
+ def text_to_speech(text):
48
+ """
49
+ Converts text to speech using a Hugging Face model.
50
+ """
51
+ models, cfg, task = load_model_ensemble_and_task_from_hf_hub(
52
+ "facebook/fastspeech2-en-ljspeech",
53
+ arg_overrides={"vocoder": "hifigan", "fp16": False}
54
+ )
55
+ model = models[0]
56
+ TTSHubInterface.update_cfg_with_data_cfg(cfg, task.data_cfg)
57
+ generator = task.build_generator([model], cfg)
58
+ sample = TTSHubInterface.get_model_input(task, text)
59
+ wav, rate = TTSHubInterface.get_prediction(task, model, generator, sample)
60
+
61
+ return wav, rate
62
+
63
+ def process_pdf(uploaded_file, hf_model_name):
64
+ """
65
+ Processes the uploaded PDF file to extract, summarize the abstract, and convert it to speech.
66
+ """
67
+ abstract = extract_and_clean_abstract(uploaded_file)
68
+ summary = summarize_text(hf_model_name, abstract)
69
+ wav, rate = text_to_speech(summary)
70
+ sf.write('/tmp/speech_output.wav', wav, rate)
71
+ return '/tmp/speech_output.wav'
72
+
73
+ iface = gr.Interface(
74
+ fn=process_pdf,
75
+ inputs=[
76
+ gr.inputs.File(label="Upload PDF", type="pdf"),
77
+ gr.inputs.Textbox(label="Hugging Face Model Name for Summarization")
78
+ ],
79
+ outputs=gr.outputs.Audio(label="Audio Summary"),
80
+ title="PDF Abstract to Speech",
81
+ description="Extracts and summarizes the abstract from a PDF file and converts it to speech."
82
+ )
83
+
84
+ if __name__ == "__main__":
85
+ iface.launch()
86
+