from heapq import nlargest import spacy from spacy.lang.en.stop_words import STOP_WORDS from string import punctuation import gradio as gr # Stopwords stopwords = list(STOP_WORDS) nlp = spacy.load('en_core_web_sm') punctuation = punctuation + '\n' import spacy from spacy.lang.en.stop_words import STOP_WORDS from string import punctuation # Prediction def prediction(text): doc = nlp(text) len1 = len(text) tokens = [token.text for token in doc] word_frequencies = {} for word in doc: if word.text.lower() not in stopwords: if word.text.lower() not in punctuation: if word.text not in word_frequencies.keys(): word_frequencies[word.text] = 1 else: word_frequencies[word.text] += 1 max_frequency = max(word_frequencies.values()) for word in word_frequencies.keys(): word_frequencies[word] = word_frequencies[word]/max_frequency sentence_tokens = [sent for sent in doc.sents] sentence_scores = {} for sent in sentence_tokens: for word in sent: if word.text.lower() in word_frequencies.keys(): if sent not in sentence_scores.keys(): sentence_scores[sent] = word_frequencies[word.text.lower()] else: sentence_scores[sent] += word_frequencies[word.text.lower()] select_length = int(len(sentence_tokens)*0.3) summary = nlargest(select_length, sentence_scores, key = sentence_scores.get) org_len = len(text.split(' ')) summary = (str(summary[0])) sum_len = len(summary.split(' ')) return summary,org_len,sum_len EXAMPLES = [[""" Maria Sharapova has basically no friends as tennis players on the WTA Tour. The Russian player has no problems in openly speaking about it and in a recent interview she said: 'I don't really hide any feelings too much. I think everyone knows this is my job here. When I'm on the courts or when I'm on the court playing, I'm a competitor and I want to beat every single person whether they're in the locker room or across the net. So I'm not the one to strike up a conversation about the weather and know that in the next few minutes I have to go and try to win a tennis match. I'm a pretty competitive girl. I say my hellos, but I'm not sending any players flowers as well. Uhm, I'm not really friendly or close to many players. I have not a lot of friends away from the courts.' When she said she is not really close to a lot of players, is that something strategic that she is doing? Is it different on the men's tour than the women's tour? 'No, not at all. I think just because you're in the same sport doesn't mean that you have to be friends with everyone just because you're categorized, you're a tennis player, so you're going to get along with tennis players. I think every person has different interests. I have friends that have completely different jobs and interests, and I've met them in very different parts of my life. I think everyone just thinks because we're tennis players we should be the greatest of friends. But ultimately tennis is just a very small part of what we do. There are so many other things that we're interested in, that we do.' """],["""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."""]] DESCRIPTION = """We are bombarded with lakhs of characters of text and information and not so much time. Text summarization reads the whole documents, based on frequency of words and sentences it understands the important sentences and gives us the summary of text. We have used a pre-trained model which is a small English pipeline trained on written web text like news, comments for this demo. This can be used in organizations that deal with lots of text documents like a law firm where the documents will be summarized in one to two paragraph as per our needs.""" outputs = [ gr.Textbox(lines =5,label = "Summarization of text"), gr.Number(label="Word Count of given Text"), gr.Number(label="Word Count of Summarized Text") ] demo_app = gr.Interface( fn=prediction, inputs=gr.Textbox(lines =10,label = " Enter the Text", max_lines = 20), outputs= outputs, title = "Text Summarization", examples = EXAMPLES, description = DESCRIPTION, #cache_example = True, #live = True, theme = 'huggingface' ) #if __name__ == "__main__": demo_app.launch() #demo_app.launch(debug=True, enable_queue = True)