Spaces:
Build error
Build error
| import streamlit as st | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| import os | |
| st.title("Translation Using OpenAI") | |
| # Language options for the dropdown menus | |
| languages = ["English", "Hindi", "Telugu", "Malayalam", "Tamil", "Punjabi"] | |
| # Dropdown menu for selecting input language | |
| input_language = st.selectbox("Select your input language", languages) | |
| # Text area for input text | |
| input_text = st.text_area(f"Write your text in {input_language}") | |
| # Dropdown menu for selecting output language | |
| output_language = st.selectbox("Select your output language", languages) | |
| # Set the environment variable for the API key | |
| os.environ['GOOGLE_API_KEY'] = os.getenv("geminiapi_key") | |
| # Initialize the OpenAI language model | |
| llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0, max_tokens=80) | |
| # Create the prompt template | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", "You are a good assistant for translating from {il} to {ol}."), | |
| ("human", "{i}") | |
| ]) | |
| # Define the translation chain | |
| chain = prompt | llm | |
| # Function to perform the translation | |
| def translate(input_text, input_language, output_language): | |
| response = chain.invoke({"il": input_language, "ol": output_language, "i": input_text}) | |
| return response.content | |
| # Submit button | |
| if st.button("Submit"): | |
| if input_text: | |
| translation = translate(input_text, input_language, output_language) | |
| st.write("Translated Text:", translation) | |
| else: | |
| st.write("Please enter text to translate.") | |