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 #predicted_label, score = occ_predict("img1.jpg") #inputs = gr.inputs.text(label) #label = gr.outputs.Label(num_top_classes=2) #EXAMPLES = ["img1.png","img2.png","img3.png","img10.png","img8.png","img9.png"] #DESCRIPTION = "Occlusion means the act of closing, blocking or shutting something or the state of being closed or blocked" #summary = prediction(text) #print(summary) 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", #description = DESCRIPTION, #cache_example = True, #live = True, theme = 'huggingface' ) #if __name__ == "__main__": demo_app.launch() #demo_app.launch(debug=True, enable_queue = True)