Spaces:
Running
Running
| 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() |