# app.py import gradio as gr import spacy import subprocess import json # Download the spaCy model if it is not already downloaded subprocess.run(["python", "-m", "spacy", "download", "en_core_web_md"]) # Load the spaCy model for POS tagging and similarity nlp = spacy.load("en_core_web_md") def identify_nouns_verbs(text): # Process the text with spaCy doc = nlp(text) # Extract nouns and verbs nouns = [token.text for token in doc if token.pos_ == "NOUN"] verbs = [token.text for token in doc if token.pos_ == "VERB"] return {"Nouns": nouns, "Verbs": verbs} def calculate_similarity(nouns_verbs, input_list): similarities = {"Nouns": {}, "Verbs": {}} for noun in nouns_verbs["Nouns"]: noun_token = nlp(noun) for word in input_list["Nouns"]: word_token = nlp(word) similarity = noun_token.similarity(word_token) if similarity > 0.7: # Adjust threshold as needed if noun not in similarities["Nouns"]: similarities["Nouns"][noun] = [] similarities["Nouns"][noun].append((word, similarity)) for verb in nouns_verbs["Verbs"]: verb_token = nlp(verb) for word in input_list["Verbs"]: word_token = nlp(word) similarity = verb_token.similarity(word_token) if similarity > 0.7: # Adjust threshold as needed if verb not in similarities["Verbs"]: similarities["Verbs"][verb] = [] similarities["Verbs"][verb].append((word, similarity)) return similarities def process_inputs(text, json_file): # Read the content of the uploaded file with open(json_file.name, 'r') as f: input_list = json.load(f) nouns_verbs = identify_nouns_verbs(text) similarities = calculate_similarity(nouns_verbs, input_list) return {"Nouns and Verbs": nouns_verbs, "Similarities": similarities} # Create the Gradio interface iface = gr.Interface( fn=process_inputs, inputs=[ gr.Textbox(lines=10, placeholder="Enter your text here..."), gr.File(label="Upload JSON File") ], outputs=gr.JSON(), title="Noun and Verb Identifier with Similarity Check", description="Enter a document or text to identify the nouns and verbs, and check for similarities with a given list of words." ) if __name__ == "__main__": iface.launch(share=True)