Spaces:
Build error
Build error
| import streamlit as st | |
| # Dictionary dataset (for demo purposes, you can replace this with an API or a larger dataset) | |
| urdu_dictionary = { | |
| "happy": { | |
| "urdu_meaning": "خوش", | |
| "synonyms": ["joyful", "cheerful", "content"] | |
| }, | |
| "sad": { | |
| "urdu_meaning": "اداس", | |
| "synonyms": ["unhappy", "sorrowful", "melancholy"] | |
| }, | |
| "beautiful": { | |
| "urdu_meaning": "خوبصورت", | |
| "synonyms": ["attractive", "lovely", "gorgeous"] | |
| }, | |
| "strong": { | |
| "urdu_meaning": "مضبوط", | |
| "synonyms": ["powerful", "sturdy", "resilient"] | |
| } | |
| } | |
| # Function to fetch word details | |
| def get_word_info(word): | |
| word = word.lower() | |
| return urdu_dictionary.get(word, None) | |
| # Streamlit UI | |
| def main(): | |
| st.title("📖 English to Urdu Dictionary") | |
| st.write("Enter an English word to get its Urdu meaning and synonyms.") | |
| # Sidebar for additional options | |
| with st.sidebar: | |
| st.header("Options") | |
| show_history = st.checkbox("Show search history", value=True) | |
| # User input: Dropdown or text input | |
| word_list = list(urdu_dictionary.keys()) | |
| selected_word = st.selectbox("Or select a word from the list:", word_list) | |
| word = st.text_input("Enter an English word:", "") | |
| # Use the selected word if no text input is provided | |
| if not word: | |
| word = selected_word | |
| # Initialize session state for search history | |
| if "search_history" not in st.session_state: | |
| st.session_state.search_history = [] | |
| if word: | |
| word_info = get_word_info(word) | |
| if word_info: | |
| st.success(f"**Urdu Meaning:** word_info('urdu_meaning')") | |
| st.write(f"**Synonyms:** ', '.join(word_info('synonyms')") | |
| # Update search history | |
| if word not in st.session_state.search_history: | |
| st.session_state.search_history.append(word) | |
| if len(st.session_state.search_history) > 5: | |
| st.session_state.search_history.pop(0) | |
| else: | |
| st.error("Word not found in the dictionary. Try another word!") | |
| # Display search history | |
| if show_history and st.session_state.search_history: | |
| st.subheader("Search History") | |
| for i, searched_word in enumerate(st.session_state.search_history[::-1], 1): | |
| st.write(f"{i}. {searched_word}") | |
| if __name__ == "__main__": | |
| main() |