dictionary / app.py
EKEnglish's picture
Create app.py
5cb0fed verified
import gradio as gr
from PIL import Image
import requests
from io import BytesIO
# Enhanced dictionary data with images
dictionary_data = {
"devoted": {
"definition": "Very loving or loyal.",
"synonyms": ["loyal", "faithful", "dedicated"],
"example": "She was devoted to her family.",
"image": "https://kids.wordsmyth.net/media/wedt/image/devoted.jpg"
},
"ships": {
"definition": "Large boats that travel on the sea, especially large passenger or cargo ships.",
"synonyms": ["vessel", "boat"],
"example": "The ships sailed across the ocean.",
"image": "https://www.cunard.com/content/dam/cunard/marketing-assets/ships/Cunard_Fleet_Four_Ship_Day_mobile.jpg.1708444329365.image.750.563.low.jpg"
},
"lighthouse": {
"definition": "A tower or other structure containing a beacon light to warn or guide ships at sea.",
"synonyms": ["beacon", "light tower"],
"example": "The lighthouse shone brightly in the dark.",
"image": "https://cdn.britannica.com/13/189513-050-D73988F5/John-Smeaton-Eddystone-Lighthouse-Plymouth-Hoe-England.jpg"
},
"seaside": {
"definition": "A place by the sea, especially a beach area or vacation resort.",
"synonyms": ["beach", "coast"],
"example": "We spent our holiday at the seaside.",
"image": "https://cdn.britannica.com/13/189513-050-D73988F5/John-Smeaton-Eddystone-Lighthouse-Plymouth-Hoe-England.jpg"
},
"town": {
"definition": "A populated area that is smaller than a city and larger than a village.",
"synonyms": ["municipality", "settlement"],
"example": "They live in a small town.",
"image": "https://en.wikipedia.org/wiki/Town#/media/File:St_Mary's_Church,_Castle_Street_1.jpg"
},
"council": {
"definition": "A group of people elected or chosen to make decisions or give advice on a particular subject.",
"synonyms": ["committee", "board"],
"example": "The town council met to discuss the new project.",
"image": "https://d3i6fh83elv35t.cloudfront.net/static/2022/03/2022-03-23T224553Z_1063185917_RC2L8T9JT4FJ_RTRMADP_3_UKRAINE-CRISIS-UN-1024x683.jpg"
},
"replace": {
"definition": "To take the place of something, or to put something or someone in the place of something or someone else.",
"synonyms": ["substitute", "exchange"],
"example": "She had to replace the old batteries.",
"image": "https://adamtheautomator.com/wp-content/uploads/2021/03/How-to-Replace-Text-with-PowerShell-Examples.jpg"
},
"electronic": {
"definition": "Relating to devices or systems that operate using electric power or signals.",
"synonyms": ["digital", "automated"],
"example": "He bought a new electronic device.",
"image": "https://www.monash.edu.my/__data/assets/image/0006/3439176/electrocic-engineer-fixes-electronic-equipment.jpg"
},
"navigation": {
"definition": "The process or activity of accurately ascertaining one's position and planning and following a route.",
"synonyms": ["direction finding", "steering"],
"example": "Navigation skills are important for pilots.",
"image": "https://keyassets.timeincuk.net/inspirewp/live/wp-content/uploads/sites/11/2014/11/map-and-compass-e1510065441447.jpg"
},
"storm": {
"definition": "A violent disturbance of the atmosphere with strong winds and usually rain, thunder, lightning, or snow.",
"synonyms": ["tempest", "squall"],
"example": "The storm caused widespread damage.",
"image": "https://t3.ftcdn.net/jpg/04/09/38/88/360_F_409388878_YWjMLSwp6YJm833AclWTqQIF0ZtHbWlz.jpg"
},
"grandson": {
"definition": "A son of one's son or daughter.",
"synonyms": ["descendant", "heir"],
"example": "She is very proud of her grandson.",
"image": "https://www.shutterstock.com/image-photo/grandfather-piggyback-riding-grandson-260nw-13861099.jpg"
},
"lantern": {
"definition": "A lamp with a transparent case protecting the flame or electric bulb, and typically having a handle by which it can be carried or hung.",
"synonyms": ["lamp", "light"],
"example": "He carried a lantern to light his way.",
"image": "https://as1.ftcdn.net/v2/jpg/03/17/91/44/1000_F_317914468_9mfoHwDbSyuqY0fkt7Ax4zZHzypsbEnb.jpg"
},
"lighthouse keeper": {
"definition": "A person who is employed to tend and take care of a lighthouse.",
"synonyms": ["lightkeeper", "watchman"],
"example": "The lighthouse keeper ensured the light was always shining.",
"image": "https://as2.ftcdn.net/v2/jpg/05/79/27/37/1000_F_579273772_uI8IaYFb724GyMUqtRmATvhTIibWPhEN.jpg"
},
"determination": {
"definition": "Firmness of purpose; resoluteness.",
"synonyms": ["resolve", "perseverance"],
"example": "She showed great determination in achieving her goals.",
"image": "https://ccsc.nsw.edu.au/wp-content/uploads/2015/05/Unknown-4.jpeg"
}
}
def resize_image(image_url, size=(300, 300)):
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
img = img.resize(size, Image.ANTIALIAS)
return img
def lookup_word(word):
word = word.lower()
if word in dictionary_data:
entry = dictionary_data[word]
response_text = f"**Definition**: {entry['definition']}\n\n"
response_text += f"**Synonyms**: {', '.join(entry['synonyms'])}\n\n"
response_text += f"**Example**: {entry['example']}"
response_image = resize_image(entry['image'])
return response_text, response_image
else:
return "Word not found in the dictionary.", None
# Gradio interface
iface = gr.Interface(
fn=lookup_word,
inputs="text",
outputs=["markdown", gr.Image(type="pil")],
title="Interactive Online Dictionary",
description="Type a word to get its definition, synonyms, an example sentence, and an image.",
)
# Launch the application
iface.launch()