import nltk from collections import Counter import gradio as gr # Download the necessary NLTK resources nltk.download('punkt') nltk.download('averaged_perceptron_tagger') # Define the function for extracting the top 2 nouns def extract_top_nouns(text): # Tokenize the text into individual words words = nltk.word_tokenize(text) # Tag each word with its part of speech tagged_words = nltk.pos_tag(words) # Extract only the nouns nouns = [word for word, tag in tagged_words if tag in ['NN', 'NNS', 'NNP', 'NNPS']] # Count the frequency of each noun noun_counts = Counter(nouns) # Get the top 2 most common nouns top_nouns = noun_counts.most_common(2) # Return the top 2 nouns as a string return ', '.join([noun[0] for noun in top_nouns]) # Define the Gradio interface iface = gr.Interface(fn=extract_top_nouns, inputs=gr.Textbox(lines=2, placeholder='Enter text here...'), outputs='text') # Launch the Gradio app iface.launch(share=True)