Multilingual Dictionary App Using Python
Here, for creating the Multilingual Dictionary App Using Python follow the below steps
Create the Virtual Environment
using the below command create virutal environment
python -m venv env
.\env\Scripts\activate.ps1
Install Nesseaccary Libraries
pip install openai
pip install streamlit
These commands will install the necessary dependencies for utilizing OpenAI and Streamlit in your Python environment
Import Necessary libraries
First, import the OpenAI, Streamlit, and languages library in the following manner to create the GUI, use the API, and handle language conversion
import openai
import streamlit as st
from languages import *
Code Implementation
app.py : Here, The Python code defines a Streamlit app for a Multilingual Dictionary. It uses the OpenAI API to retrieve the meaning of a user-entered text in a specified language. The app has a State class to manage the application state, including the entered text, selected language, and retrieved definition. The get_meaning()
method sends a request to the OpenAI API with a prompt based on the user’s input and stores the obtained definition.
The main()
function sets up the Streamlit app interface with a text area for input, a dropdown for language selection, and a button to trigger the meaning retrieval. The retrieved definition is displayed below the horizontal line. The app is run when the script is executed, utilizing the Streamlit framework for creating the graphical user interface.
Note: To get the key follow the link
Python
import openai as OpenAI import streamlit as st from languages import * # Set OpenAI API key OpenAI.api_key = "OpenAI API Key" class State: """This is the app state.""" text = "" language = "" define = "" @staticmethod def get_meaning(): """Get the meaning of the entered text in the specified language.""" # Check if text is entered if not State.text: st.warning( "Please enter some text." ) return # Check if a language is selected if not State.language: st.warning( "Please select a language." ) return # Generate prompt for OpenAI API prompt = f "get meaning of '{State.text}' in {State.language}" try : # Make a request to OpenAI API response = OpenAI.Completion.create( model = "text-davinci-002" , prompt = prompt, temperature = 1 , max_tokens = 256 , top_p = 1 , frequency_penalty = 0 , presence_penalty = 0 , ) # Extract and store the definition from the API response State.define = response.choices[ 0 ].text.strip() except Exception as e: # Handle errors from OpenAI API st.error(f "Error with OpenAI API: {e}" ) @staticmethod def set_text(text): """Set the entered text in the app state.""" State.text = text @staticmethod def set_language(language): """Set the selected language in the app state.""" State.language = language def main(): # Set Streamlit page configuration st.set_page_config( page_title = "Streamlit: Multilingual Dictionary App" , page_icon = ":globe_with_meridians:" , layout = "wide" , ) # Main title of the app st.title( "Multilingual Dictionary App" ) # Text area for user input State.text = st.text_area( "Enter text" ) # Dropdown for selecting a language State.language = st.selectbox( "Select a language" , languages) # Button to trigger the meaning retrieval if st.button( "Get Meaning" , key = "meaning_button" ): State.get_meaning() # Horizontal line for visual separation st.markdown( "---" ) # Display the retrieved definition if State.define: st.subheader( "Definition:" ) st.write(State.define) # Run the app when the script is executed if __name__ = = "__main__" : main() # This code is contributed by sourabh_jain |
languages.py : The code defines a list of various languages .
Python3
# List of languages languages = [ "Afrikaans" , "Akan" , "Albanian" , "Amharic" , "Arabic" , "Armenian" , "Assamese" , "Aymara" , "Azerbaijani" , "Bambara" , "Bangla" , "Basque" , "Belarusian" , "Bhojpuri" , "Bosnian" , "Bulgarian" , "Burmese" , "Catalan" , "Cebuano" , "Central Kurdish" , "Chinese (Simplified)" , "Chinese (Traditional)" , "Corsican" , "Croatian" , "Czech" , "Danish" , "Divehi" , "Dogri" , "Dutch" , "English" , "Esperanto" , "Estonian" , "Ewe" , "Filipino" , "Finnish" , "French" , "Galician" , "Ganda" , "Georgian" , "German" , "Goan Konkani" , "Greek" , "Guarani" , "Gujarati" , "Haitian Creole" , "Hausa" , "Hawaiian" , "Hebrew" , "Hindi" , "Hmong" , "Hungarian" , "Icelandic" , "Igbo" , "Iloko" , "Indonesian" , "Irish" , "Italian" , "Japanese" , "Javanese" , "Kannada" , "Kazakh" , "Khmer" , "Kinyarwanda" , "Korean" , "Krio" , "Kurdish" , "Kyrgyz" , "Lao" , "Latin" , "Latvian" , "Lingala" , "Lithuanian" , "Luxembourgish" , "Macedonian" , "Maithili" , "Malagasy" , "Malay" , "Malayalam" , "Maltese" , "Manipuri (Meitei Mayek)" , "Māori" , "Marathi" , "Mizo" , "Mongolian" , "Nepali" , "Northern Sotho" , "Norwegian" , "Nyanja" , "Odia" , "Oromo" , "Pashto" , "Persian" , "Polish" , "Portuguese" , "Punjabi" , "Quechua" , "Romanian" , "Russian" , "Samoan" , "Sanskrit" , "Scottish Gaelic" , "Serbian" , "Shona" , "Sindhi" , "Sinhala" , "Slovak" , "Slovenian" , "Somali" , "Southern Sotho" , "Spanish" , "Sundanese" , "Swahili" , "Swedish" , "Tajik" , "Tamil" , "Tatar" , "Telugu" , "Thai" , "Tigrinya" , "Tsonga" , "Turkish" , "Turkmen" , "Ukrainian" , "Urdu" , "Uyghur" , "Uzbek" , "Vietnamese" , "Welsh" , "Western Frisian" , "Xhosa" , "Yiddish" , "Yoruba" , "Zulu" ] |
Run the Server
For run the server use the below commands:
streamlit run "app.py_file_path"
Output
Video Demonstration
Conclusion
In conclusion, the Multilingual Dictionary App created using Python leverages the power of OpenAI’s language model and the Streamlit library to provide a user-friendly interface for translating words into multiple languages. The app allows users to input a word, select a target language from an extensive list, and seamlessly retrieve the translated result with the click of a button. The integration of OpenAI’s API enhances the app’s functionality, making it a versatile tool for linguistic exploration and communication across different languages. The simplicity of the user interface, combined with the robust translation capabilities, makes this Multilingual Dictionary App a valuable asset for users seeking quick and efficient language translation solutions.
Multilingual Dictionary App Using Python
In this article, we will guide you through the process of creating a Multilingual Dictionary app using Python. The core functionality of this application involves translating words using the OpenAI API. To provide a user-friendly interface, we will leverage the Streamlit library.
With this setup, translating words becomes a seamless process – a simple click of a button. Users only need to input the word they want to translate and select the desired languages, and the API will automatically generate translations in the specified languages. This combination of Python, OpenAI API, and Streamlit makes it easy for users to effortlessly translate words across different languages.