itsmariamaraki commited on
Commit
6ef620e
β€’
1 Parent(s): f095f5c

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -77
app.py DELETED
@@ -1,77 +0,0 @@
1
- # https://huggingface.co/spaces/itsmariamaraki/AAI-Assessment3
2
-
3
- # Here are the imports
4
-
5
- import gradio as gr
6
- import PyPDF2
7
- from PyPDF2 import PdfReader
8
- from pdfminer.high_level import extract_pages, extract_text
9
- from transformers import pipeline, AutoProcessor, AutoModel, AutoTokenizer
10
- import torch
11
- import soundfile as sf
12
- from IPython.display import Audio
13
- from datasets import load_dataset
14
- from io import BytesIO
15
- import os
16
-
17
- # Here is the code
18
-
19
- def abstract(pdf_file):
20
- pdf_bytes = BytesIO(pdf_file)
21
- pdf_reader = PyPDF2.PdfReader(pdf_bytes)
22
-
23
- abstract = ''
24
-
25
- for page_number in range(len(pdf_reader.pages)):
26
- text = pdf_reader.pages[page_number].extract_text()
27
-
28
- if 'abstract' in text.lower(): #in order to read only the abstract, i set as a start the abstract point & as an end the introduction point
29
- start_index = text.lower().find('abstract')
30
- end_index = text.lower().find('introduction')
31
- abstract = text[start_index:end_index]
32
- break
33
-
34
- return abstract
35
-
36
-
37
-
38
- summarization = pipeline('summarization', model = 'pszemraj/long-t5-tglobal-base-16384-book-summary') #best summarization model i tested regarding this assessment
39
- audiospeech = pipeline('text-to-speech', model = 'suno/bark-small') #the voice is a bit distorted but gives a good output & takes less time
40
-
41
-
42
-
43
- def summarization_n_audiospeech(pdf_file):
44
- abstract_text = abstract(pdf_file)
45
-
46
- summary = summarization(abstract_text, max_length=50, min_length=10)[0]['summary_text'] #didn't know exactly what would give one sentence, so i checked multiple times the min & max lengths regarding the 11th article. for a dif article, those parameters would probably have to be different as well
47
-
48
- fin_summary = summary.split('.')[0] + '.' #extract and print only the first sentence of the summary
49
-
50
- #converting the summarization into an audio output
51
- tts_output = audiospeech(fin_summary)
52
- audio_data = tts_output['audio'][0]
53
-
54
- with BytesIO() as buffer:
55
- sf.write(buffer, audio_data, 16000, format = 'wav')
56
- audio_bytes = buffer.getvalue()
57
-
58
- return fin_summary, audio_bytes
59
-
60
-
61
-
62
- iface = gr.Interface(
63
- fn = summarization_n_audiospeech,
64
- inputs = gr.File(label='upload PDF', type='binary'), #if i didn't set a type, the gradio output was an error - searched it online for the solution
65
- outputs = [
66
- gr.Textbox(label='Summarization of the Abstract:'),
67
- gr.Audio(label="Audio Speech of the Abstract's Summary:")
68
- ],
69
- title = "PDF's Abstract Summarization & Audio Speech Processor",
70
- description = "App that generates a one-line summary of the abstract & a speech audio of this summarization -- requirements: app only accepts PDFs which include an ABSTRACT section",
71
- examples = [os.path.join(os.path.dirname(__file__), 'Hidden_Technical_Debt.pdf'),
72
- os.path.join(os.path.dirname(__file__), 'Semiconductors.pdf'),
73
- os.path.join(os.path.dirname(__file__), 'Efficient_Estimation_of_Word_Representations.pdf')
74
- ]
75
- )
76
-
77
- iface.launch()