LeafCat79 commited on
Commit
33d1ea8
·
verified ·
1 Parent(s): c2151fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def lookup_word(word):
2
+ if not word.strip():
3
+ return "Please enter a word."
4
+ url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word.strip()}"
5
+ try:
6
+ response = requests.get(url, timeout=10)
7
+ if response.status_code == 404:
8
+ return f"**{word}** not found in the dictionary."
9
+ data = response.json()[0]
10
+ except Exception:
11
+ return "Something went wrong. Please try again."
12
+
13
+ output = f"# {data['word']}\n\n"
14
+
15
+ for meaning in data.get("meanings", []):
16
+ part = meaning.get("partOfSpeech", "")
17
+ output += f"### {part}\n\n"
18
+ for defn in meaning.get("definitions", [])[:3]:
19
+ output += f"- {defn['definition']}\n"
20
+ if defn.get("example"):
21
+ output += f" *Example: {defn['example']}*\n"
22
+ output += "\n"
23
+
24
+ return output
25
+
26
+ demo = gr.Interface(
27
+ fn=lookup_word,
28
+ inputs=gr.Textbox(label="Enter a word", placeholder="e.g. serendipity"),
29
+ outputs=gr.Markdown(label="Definition"),
30
+ title="Dictionary Lookup",
31
+ description="Look up any English word. "
32
+ "Uses the free Dictionary API — no API key needed."
33
+ )
34
+ demo.launch()