Spaces:
Runtime error
Runtime error
import gradio as gr | |
import random | |
import nltk | |
from nltk.corpus import wordnet | |
from PIL import Image | |
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): | |
# try: | |
# # You may need to adjust this URL based on the source of definitions and images | |
# url = f"https://your-dictionary-website.com/{word}" | |
# response = requests.get(url) | |
# soup = BeautifulSoup(response.text, "html.parser") | |
# # Extract definition and image (assuming they are in specific HTML elements) | |
# definition = soup.find("div", {"class": "definition"}).text | |
# image_url = soup.find("img", {"class": "word-image"})["src"] | |
# return definition, image_url | |
# except Exception as e: | |
# return None, None | |
# 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 | |
# URL for the ImageNet API | |
base_url = "http://www.image-net.org/api/text/imagenet.synset.geturls?wnid=" | |
if not synset_id: return None, None | |
# Cofrom PIL import Image mbine the base URL and the synset ID to get a list of image URLs | |
url = base_url + str(synset_id.offset()) | |
# Send a GET request to the URL | |
response = requests.get(url) | |
if response.status_code == 200: | |
# Parse the response to get a list of image URLs | |
image_urls = response.text.splitlines() | |
# Print the first 10 image URLs | |
for i, url in enumerate(image_urls[:10], start=1): | |
print(f"Image {i}: {url}") | |
return word + ":" + synset_id.definition() or "" , image_urls[0] | |
else: | |
print(f"Failed to retrieve image URLs. Status code: {response.status_code}") | |
return None, None | |
# 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, image_url = get_definition_and_image(word) | |
if definition and image_url: | |
print(f"Word: {word}") | |
print(f"Part of speech: {'Noun' if is_noun else 'Verb'}") | |
print(f"Definition: {definition}") | |
print(f"Image URL: {image_url}") | |
return image_url ,definition | |
else: | |
print(f"No information found for '{word}'") | |
img = Image.new('RGB', (64,64) ) | |
return img, f"No information found for '{word}'" | |
with gr.Blocks() as demo: | |
image, text = sample_word_and_display_info() | |
print(image, text) | |
with gr.Row(): | |
img_1= gr.Image(label="image") | |
inp = gr.Textbox(placeholder="What is your name?") | |
text_1 = gr.Markdown(label="text") | |
button = gr.Button(label="Update Text") | |
button.click(sample_word_and_display_info,inputs=inp, outputs=text_1) | |
demo.launch() | |