File size: 2,664 Bytes
187adbd
 
 
 
 
 
 
 
 
 
 
 
 
ae3af40
 
 
 
 
 
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
ae3af40
187adbd
 
ae3af40
187adbd
ae3af40
187adbd
 
ae3af40
187adbd
 
ae3af40
187adbd
 
 
4334328
187adbd
 
 
 
6dd70de
187adbd
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import gradio as gr

API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
headers = {"Authorization": "Bearer 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"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()
    


def summarize(input_text):
    lister= input_text.split(" ")
    word_lenght = len(lister)
    if word_lenght < 50:
        text= "Oops! It looks like your sentence is too short to summarize."
    else:
        max_chunk = 500

        input_text = input_text.replace('.', '.<eos>')

        input_text = input_text.replace('?', '?<eos>')

        input_text = input_text.replace('!', '!<eos>')

        sentences = input_text.split('<eos>')

        current_chunk = 0

        chunks = []

        for sentence in sentences:

            if len(chunks) == current_chunk + 1:

                if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:

                    chunks[current_chunk].extend(sentence.split(' '))

                else:

                    current_chunk += 1

                    chunks.append(sentence.split(' '))

            else:

                print(current_chunk)

                chunks.append(sentence.split(' '))


        for chunk_id in range(len(chunks)):

            chunks[chunk_id] = ' '.join(chunks[chunk_id])


        res = query({"inputs":chunks})


        text = ' '.join([summ['summary_text'] for summ in res])

    return text

#gradio function 
demo = gr.Interface(
    fn=summarize,
    inputs=[gr.Textbox(lines=8, placeholder="Paragraph Here...")],
    outputs="text",title="Text summarisation app",
    description="This is a summarisation app, it can prove useful when you want to summarize a text. All you need to do is copy and paste the text you want to summarize below",
    examples=[
        ["The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct."
],
    ],
)
demo.launch()