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 = [["""What type of language is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics developed by Guido van Rossum. It was originally released in 1991. Designed to be easy as well as fun, the name "Python" is a nod to the British comedy group Monty Python. Python has a reputation as a beginner-friendly language, replacing Java as the most widely used introductory language because it handles much of the complexity for the user, allowing beginners to focus on fully grasping programming concepts rather than minute details. Python is used for server-side web development, software development, mathematics, and system scripting, and is popular for Rapid Application Development and as a scripting or glue language to tie existing components because of its high-level, built-in data structures, dynamic typing, and dynamic binding. Program maintenance costs are reduced with Python due to the easily learned syntax and emphasis on readability. Additionally, Python's support of modules and packages facilitates modular programs and reuse of code. Python is an open source community language, so numerous independent programmers are continually building libraries and functionality for it. """],["""Machine learning is an application of artificial intelligence that uses statistical techniques to enable computers to learn and make decisions without being explicitly programmed. It is predicated on the notion that computers can learn from data, spot patterns, and make judgments with little assistance from humans. It is a subset of Artificial Intelligence. It is the study of making machines more human-like in their behavior and decisions by giving them the ability to learn and develop their own programs. This is done with minimum human intervention, i.e., no explicit programming. The learning process is automated and improved based on the experiences of the machines throughout the process.Good quality data is fed to the machines, and different algorithms are used to build ML models to train the machines on this data. The choice of algorithm depends on the type of data at hand and the type of activity that needs to be automated. """],["""Natural language processing (NLP) refers to the branch of computer science—and more specifically, the branch of artificial intelligence or AI—concerned with giving computers the ability to understand text and spoken words in much the same way human beings can. NLP combines computational linguistics—rule-based modeling of human language—with statistical, machine learning, and deep learning models. Together, these technologies enable computers to process human language in the form of text or voice data and to ‘understand’ its full meaning, complete with the speaker or writer’s intent and sentiment. NLP drives computer programs that translate text from one language to another, respond to spoken commands, and summarize large volumes of text rapidly—even in real time. There’s a good chance you’ve interacted with NLP in the form of voice-operated GPS systems, digital assistants, speech-to-text dictation software, customer service chatbots, and other consumer conveniences. But NLP also plays a growing role in enterprise solutions that help streamline business operations, increase employee productivity, and simplify mission-critical business processes."""]] DESCRIPTION = """This Hugging Face's Document Summarization app utilizes a pre-trained model trained on web text to quickly and effectively condense lengthy documents into concise one to two-paragraph summaries. This is invaluable for organizations handling vast volumes of text, such as law firms, where the need for efficient and accurate document summarization is critical.""" outputs = [ gr.Textbox(lines =5,label = "Summarization of Document"), gr.Number(label="Word Count of given Document"), gr.Number(label="Word Count of Summarized Document") ] demo_app = gr.Interface( fn=prediction, inputs=gr.Textbox(lines =10,label = " Enter the Text", max_lines = 20), outputs= outputs, title = "Document 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)