-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (47 loc) · 2.03 KB
/
app.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import streamlit as st
import requests
# Oxford Dictionary API credentials
rapidapi_key = '97e3139a3cmsh644ef8adadf9762p16097fjsnf5403961d00c'
rapidapi_host = 'joughtred-oxford-dictionaries-v1.p.rapidapi.com'
# Function to get word definition from Oxford Dictionary API via RapidAPI
def get_definition(word):
url = f"https://{rapidapi_host}/entries/en-us/{word}/examples"
headers = {
"x-rapidapi-key": rapidapi_key,
"x-rapidapi-host": rapidapi_host
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
definitions = []
if 'results' in data:
for entry in data['results']:
if 'lexicalEntries' in entry:
for lexicalEntry in entry['lexicalEntries']:
if 'entries' in lexicalEntry:
for sense in lexicalEntry['entries']:
if 'senses' in sense:
for definition in sense['senses']:
if 'definitions' in definition:
definitions.append(definition['definitions'][0])
return definitions if definitions else ["No definition found."]
else:
return ["Error fetching definition from Oxford Dictionary API."]
# Read HTML and CSS files
def read_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
# Streamlit UI
html_content = read_file('templates/index.html')
css_content = read_file('static/style.css')
# Inject CSS and HTML into Streamlit
st.markdown(f'<style>{css_content}</style>', unsafe_allow_html=True)
st.markdown(html_content, unsafe_allow_html=True)
# User input for word definition
user_input = st.text_input("Enter a word:")
if user_input:
with st.spinner("Looking up the word..."):
definitions = get_definition(user_input)
st.write("Definitions:")
for definition in definitions:
st.write(f"- {definition}")