import os
from dotenv import load_dotenv
import streamlit as st

from langchain.chains import LLMChain
from langchain import PromptTemplate

from genai.credentials import Credentials
from genai.extensions.langchain import LangChainInterface
from genai.schemas import GenerateParams

load_dotenv()
api_key = os.getenv("GENAI_KEY", None)
api_endpoint = os.getenv("GENAI_API", None)

creds = Credentials(api_key,api_endpoint)

params = GenerateParams(
    decoding_method="sample",
    max_new_tokens=200,
    min_new_tokens=1,
    stream=False,
    temperature=0.7,
    top_k=50,
    top_p=1
).dict()

with st.sidebar:
    st.title("Translation Assistant")

st.title("Translation Assistant")

text_input = st.text_area('Enter text')
    
# Create a selectbox for the user to select the target language
target_language = st.selectbox('Select language', [ 'English', 'Spanish', 'French', 'German','Chinese','Korean','Japanese','Hindi'])

# Create a button that the user can click to initiate the translation process
translate_button = st.button('Translate')

# Create a placeholder where the translated text will be displayed
translated_text = st.empty()

# Handle the translation process when the user clicks the translate button
if translate_button:
    translated_text.text('Translating...')
    llm = LangChainInterface(model="bigscience/mt0-xxl",credentials=creds, params=params)
    prompt = PromptTemplate(template=f"Translate '{text_input}' to {target_language}",
                            input_variables=[])
    chain = LLMChain(llm=llm,prompt=prompt)
    response_text = chain.predict()
    translated_text.text(response_text)

langchain.chains.LLMChain langchain.PromptTemplate
[((283, 296), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (294, 296), False, 'from dotenv import load_dotenv\n'), ((307, 335), 'os.getenv', 'os.getenv', (['"""GENAI_KEY"""', 'None'], {}), "('GENAI_KEY', None)\n", (316, 335), False, 'import os\n'), ((351, 379), 'os.getenv', 'os.getenv', (['"""GENAI_API"""', 'None'], {}), "('GENAI_API', None)\n", (360, 379), False, 'import os\n'), ((389, 423), 'genai.credentials.Credentials', 'Credentials', (['api_key', 'api_endpoint'], {}), '(api_key, api_endpoint)\n', (400, 423), False, 'from genai.credentials import Credentials\n'), ((656, 689), 'streamlit.title', 'st.title', (['"""Translation Assistant"""'], {}), "('Translation Assistant')\n", (664, 689), True, 'import streamlit as st\n'), ((704, 730), 'streamlit.text_area', 'st.text_area', (['"""Enter text"""'], {}), "('Enter text')\n", (716, 730), True, 'import streamlit as st\n'), ((818, 939), 'streamlit.selectbox', 'st.selectbox', (['"""Select language"""', "['English', 'Spanish', 'French', 'German', 'Chinese', 'Korean', 'Japanese',\n    'Hindi']"], {}), "('Select language', ['English', 'Spanish', 'French', 'German',\n    'Chinese', 'Korean', 'Japanese', 'Hindi'])\n", (830, 939), True, 'import streamlit as st\n'), ((1031, 1053), 'streamlit.button', 'st.button', (['"""Translate"""'], {}), "('Translate')\n", (1040, 1053), True, 'import streamlit as st\n'), ((1140, 1150), 'streamlit.empty', 'st.empty', ([], {}), '()\n', (1148, 1150), True, 'import streamlit as st\n'), ((621, 654), 'streamlit.title', 'st.title', (['"""Translation Assistant"""'], {}), "('Translation Assistant')\n", (629, 654), True, 'import streamlit as st\n'), ((1301, 1386), 'genai.extensions.langchain.LangChainInterface', 'LangChainInterface', ([], {'model': '"""bigscience/mt0-xxl"""', 'credentials': 'creds', 'params': 'params'}), "(model='bigscience/mt0-xxl', credentials=creds, params=params\n    )\n", (1319, 1386), False, 'from genai.extensions.langchain import LangChainInterface\n'), ((1394, 1491), 'langchain.PromptTemplate', 'PromptTemplate', ([], {'template': 'f"""Translate \'{text_input}\' to {target_language}"""', 'input_variables': '[]'}), '(template=f"Translate \'{text_input}\' to {target_language}",\n    input_variables=[])\n', (1408, 1491), False, 'from langchain import PromptTemplate\n'), ((1528, 1560), 'langchain.chains.LLMChain', 'LLMChain', ([], {'llm': 'llm', 'prompt': 'prompt'}), '(llm=llm, prompt=prompt)\n', (1536, 1560), False, 'from langchain.chains import LLMChain\n'), ((433, 566), 'genai.schemas.GenerateParams', 'GenerateParams', ([], {'decoding_method': '"""sample"""', 'max_new_tokens': '(200)', 'min_new_tokens': '(1)', 'stream': '(False)', 'temperature': '(0.7)', 'top_k': '(50)', 'top_p': '(1)'}), "(decoding_method='sample', max_new_tokens=200, min_new_tokens\n    =1, stream=False, temperature=0.7, top_k=50, top_p=1)\n", (447, 566), False, 'from genai.schemas import GenerateParams\n')]