Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,46 +10,48 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
|
| 12 |
def extract_first_sentence(text):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def summarize_pdf_abstract(pdf_file):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
interface = gr.Interface(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
)
|
| 54 |
|
| 55 |
interface.launch(share=True)
|
|
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
|
| 12 |
def extract_first_sentence(text):
|
| 13 |
+
"""
|
| 14 |
+
Extracts the first sentence from the given text.
|
| 15 |
+
"""
|
| 16 |
+
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
|
| 17 |
+
if sentences:
|
| 18 |
+
return sentences[0]
|
| 19 |
+
else:
|
| 20 |
+
return text
|
| 21 |
|
| 22 |
def summarize_pdf_abstract(pdf_file):
|
| 23 |
+
"""
|
| 24 |
+
Reads a PDF file, extracts the abstract, summarizes it as the first sentence, and generates audio.
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
reader = PdfReader(pdf_file)
|
| 28 |
+
abstract_text = ""
|
| 29 |
+
for page in reader.pages:
|
| 30 |
+
if "Abstract" in page.extract_text() or "Introduction" in page.extract_text():
|
| 31 |
+
abstract_text = page.extract_text()
|
| 32 |
+
break
|
| 33 |
+
|
| 34 |
+
inputs = tokenizer(abstract_text, return_tensors="pt")
|
| 35 |
+
outputs = model.generate(**inputs)
|
| 36 |
+
summary = tokenizer.decode(outputs[0])
|
| 37 |
+
|
| 38 |
+
# Extract only the first sentence
|
| 39 |
+
summary_sentence = extract_first_sentence(summary)
|
| 40 |
+
|
| 41 |
+
# Generate audio
|
| 42 |
+
speech = gTTS(text=summary_sentence, lang="en")
|
| 43 |
+
speech_bytes = BytesIO()
|
| 44 |
+
speech.write_to_fp(speech_bytes)
|
| 45 |
+
|
| 46 |
+
return {"summary": summary_sentence, "audio": speech_bytes}
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
raise Exception(str(e))
|
| 50 |
|
| 51 |
interface = gr.Interface(
|
| 52 |
+
fn=summarize_pdf_abstract,
|
| 53 |
+
inputs=[gr.File(label="Upload PDF")],
|
| 54 |
+
outputs=[gr.Textbox(label="Summary"), gr.Audio()],
|
| 55 |
)
|
| 56 |
|
| 57 |
interface.launch(share=True)
|