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('.', '.') input_text = input_text.replace('?', '?') input_text = input_text.replace('!', '!') sentences = input_text.split('') 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()