Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,102 @@
|
|
1 |
import gradio as gr
|
2 |
-
import classla
|
3 |
-
classla.download('hr')
|
4 |
-
nlp = classla.Pipeline('hr')
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
return doc.to_conll()
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
iface.launch()
|
13 |
|
14 |
|
|
|
1 |
import gradio as gr
|
2 |
+
# import classla
|
3 |
+
# classla.download('hr')
|
4 |
+
# nlp = classla.Pipeline('hr')
|
5 |
+
import random
|
6 |
+
import nltk
|
7 |
+
from nltk.corpus import wordnet
|
8 |
+
from bs4 import BeautifulSoup
|
9 |
+
import requests
|
10 |
|
11 |
+
# Download the Croatian WordNet data
|
12 |
+
nltk.download("wordnet")
|
13 |
+
nltk.download("omw")
|
|
|
14 |
|
15 |
+
|
16 |
+
|
17 |
+
# Function to get the English WordNet synset ID for a Croatian word
|
18 |
+
def get_english_synset_id(croatian_word):
|
19 |
+
# Convert the Croatian word to English WordNet synset IDs
|
20 |
+
synsets = wordnet.synsets(croatian_word, lang="hrv")
|
21 |
+
|
22 |
+
# Check if any synsets were found
|
23 |
+
if synsets:
|
24 |
+
# Take the first synset (you can modify this to handle multiple synsets if needed)
|
25 |
+
english_synset_id = synsets[0].name()
|
26 |
+
return english_synset_id
|
27 |
+
else:
|
28 |
+
return None
|
29 |
+
|
30 |
+
# Function to get definitions and images from web
|
31 |
+
def get_definition_and_image(word):
|
32 |
+
# try:
|
33 |
+
# # You may need to adjust this URL based on the source of definitions and images
|
34 |
+
# url = f"https://your-dictionary-website.com/{word}"
|
35 |
+
# response = requests.get(url)
|
36 |
+
# soup = BeautifulSoup(response.text, "html.parser")
|
37 |
+
|
38 |
+
# # Extract definition and image (assuming they are in specific HTML elements)
|
39 |
+
# definition = soup.find("div", {"class": "definition"}).text
|
40 |
+
# image_url = soup.find("img", {"class": "word-image"})["src"]
|
41 |
+
|
42 |
+
# return definition, image_url
|
43 |
+
# except Exception as e:
|
44 |
+
# return None, None
|
45 |
+
|
46 |
+
|
47 |
+
# Define the WordNet synset ID for the concept you're interested in
|
48 |
+
synset_id = get_english_synset_id(word) #"n07753592" # Replace with the synset ID you want
|
49 |
+
|
50 |
+
# URL for the ImageNet API
|
51 |
+
base_url = "http://www.image-net.org/api/text/imagenet.synset.geturls?wnid="
|
52 |
+
|
53 |
+
# Combine the base URL and the synset ID to get a list of image URLs
|
54 |
+
url = base_url + synset_id
|
55 |
+
|
56 |
+
# Send a GET request to the URL
|
57 |
+
response = requests.get(url)
|
58 |
+
|
59 |
+
if response.status_code == 200:
|
60 |
+
# Parse the response to get a list of image URLs
|
61 |
+
image_urls = response.text.splitlines()
|
62 |
+
|
63 |
+
# Print the first 10 image URLs
|
64 |
+
for i, url in enumerate(image_urls[:10], start=1):
|
65 |
+
print(f"Image {i}: {url}")
|
66 |
+
else:
|
67 |
+
print(f"Failed to retrieve image URLs. Status code: {response.status_code}")
|
68 |
+
|
69 |
+
# Function to randomly sample a noun or verb
|
70 |
+
def sample_word_and_display_info():
|
71 |
+
is_noun = random.choice([True, False]) # Randomly choose noun or verb
|
72 |
+
if is_noun:
|
73 |
+
pos = "n"
|
74 |
+
else:
|
75 |
+
pos = "v"
|
76 |
+
|
77 |
+
# Get a random word of the chosen part of speech from the Croatian WordNet
|
78 |
+
word = random.choice(wordnet.all_lemma_names(pos, lang="hrv"))
|
79 |
+
|
80 |
+
# Get the definition and image (change the function for your source)
|
81 |
+
definition, image_url = get_definition_and_image(word)
|
82 |
+
|
83 |
+
if definition and image_url:
|
84 |
+
print(f"Word: {word}")
|
85 |
+
print(f"Part of speech: {'Noun' if is_noun else 'Verb'}")
|
86 |
+
print(f"Definition: {definition}")
|
87 |
+
print(f"Image URL: {image_url}")
|
88 |
+
return image_url ,definition
|
89 |
+
else:
|
90 |
+
print(f"No information found for '{word}'")
|
91 |
+
return none,none
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
def sample():
|
96 |
+
return sample_word_and_display_info()
|
97 |
+
|
98 |
+
|
99 |
+
iface = gr.Interface(fn=sample, inputs="text", outputs="image, text")
|
100 |
iface.launch()
|
101 |
|
102 |
|