File size: 1,210 Bytes
48c30b0
 
 
82510b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests

def lookup_word(word):
    if not word.strip():
        return "Please enter a word."
    url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word.strip()}"
    try:
        response = requests.get(url, timeout=10)
        if response.status_code == 404:
            return f"**{word}** not found in the dictionary."
        data = response.json()[0]
    except Exception:
        return "Something went wrong. Please try again."

    output = f"# {data['word']}\n\n"

    for meaning in data.get("meanings", []):
        part = meaning.get("partOfSpeech", "")
        output += f"### {part}\n\n"
        for defn in meaning.get("definitions", [])[:3]:
            output += f"- {defn['definition']}\n"
            if defn.get("example"):
                output += f"  *Example: {defn['example']}*\n"
        output += "\n"

    return output

demo = gr.Interface(
    fn=lookup_word,
    inputs=gr.Textbox(label="Enter a word", placeholder="e.g. serendipity"),
    outputs=gr.Markdown(label="Definition"),
    title="Dictionary Lookup",
    description="Look up any English word. "
                "Uses the free Dictionary API — no API key needed."
)
demo.launch()