Spaces:
Runtime error
Runtime error
File size: 2,318 Bytes
ae4ea47 053844b e52febb 053844b 3e994a1 053844b 7099fbc ae4ea47 053844b 6bc5afd 053844b 47f01ff e95dd79 053844b e52febb 053844b 9162278 e52febb 053844b b911d1f e52febb 053844b f3091e4 053844b f0176cc 6bc5afd 053844b e52febb 053844b e52febb 053844b e52febb 053844b 49c7bae e52febb 053844b 8e72703 e52febb 4c5e037 b911d1f a8fa02d d3b4381 b911d1f 8e72703 81136e1 3e994a1 81136e1 6bf846d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import gradio as gr
import random
import nltk
from nltk.corpus import wordnet
import requests
# Download the Croatian WordNet data
nltk.download("wordnet")
nltk.download("omw")
nltk.download("omw-1.4")
# Function to get the English WordNet synset ID for a Croatian word
def get_english_synset_id(croatian_word):
# Convert the Croatian word to English WordNet synset IDs
synsets = wordnet.synsets(croatian_word, lang="hrv")
print(f"synsets: {synsets}")
# Check if any synsets were found
if synsets:
# Take the first synset (you can modify this to handle multiple synsets if needed)
hrv_synset = synsets[0]
english_synset_id = wordnet.synset_from_pos_and_offset("n", hrv_synset.offset())
return english_synset_id
else:
return None
# Function to get definitions and images from web
def get_definition_and_image(word):
# Define the WordNet synset ID for the concept you're interested in
synset_id = get_english_synset_id(word) #"n07753592" # Replace with the synset ID you want
if not synset_id: return None
return word + ":\n" + synset_id.definition() or ""
# Function to randomly sample a noun or verb
def sample_word_and_display_info(dummy="dummy"):
is_noun = random.choice([True, False]) # Randomly choose noun or verb
if is_noun:
pos = "n"
else:
pos = "v"
# Get a random word of the chosen part of speech from the Croatian WordNet
word = random.choice(list(wordnet.all_lemma_names(pos, lang="hrv")))
print(f"Word: {word}")
# Get the definition and image (change the function for your source)
definition = get_definition_and_image(word)
if definition:
print(f"Word: {word}")
print(f"Part of speech: {'Noun' if is_noun else 'Verb'}")
print(f"Definition: {definition}")
return definition
else:
print(f"No information found for '{word}'")
return f"No information found for '{word}'"
with gr.Blocks() as demo:
text = sample_word_and_display_info()
print(text)
with gr.Row():
text_1 = gr.Textbox(label="text")
button = gr.Button(label="Update Text")
button.click(sample_word_and_display_info,inputs=text_1, outputs=text_1)
demo.launch()
|