Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,53 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_openai import ChatOpenAI
|
| 3 |
import os
|
| 4 |
-
from
|
| 5 |
|
|
|
|
| 6 |
os.environ['OPENAI_API_KEY'] = os.getenv('api_key')
|
| 7 |
|
|
|
|
| 8 |
llm = ChatOpenAI(model='gpt-3.5-turbo', temperature=0, max_tokens=150)
|
| 9 |
|
|
|
|
| 10 |
st.header("Translation Application")
|
| 11 |
|
|
|
|
| 12 |
il = st.selectbox(
|
| 13 |
-
"Please select
|
| 14 |
-
("Hindi", "English", "Bengali")
|
| 15 |
-
|
| 16 |
st.write("You selected:", il)
|
| 17 |
|
|
|
|
| 18 |
ol = st.selectbox(
|
| 19 |
-
"Please select
|
| 20 |
-
("Hindi", "English", "Bengali")
|
| 21 |
-
|
| 22 |
-
st.write("You selected:", ol)
|
| 23 |
-
|
| 24 |
-
prompt = ChatPromptTemplate.from_messages(
|
| 25 |
-
[('system', 'you are a good assistant for translation to {il} to {ol}'),
|
| 26 |
-
'user', '{i}']
|
| 27 |
)
|
|
|
|
| 28 |
|
|
|
|
| 29 |
input_text = st.text_area('Input Text', height=300)
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
})
|
| 36 |
|
|
|
|
| 37 |
if st.button('Submit'):
|
| 38 |
if input_text:
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
-
st.write('Please enter
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_openai import ChatOpenAI
|
| 3 |
import os
|
| 4 |
+
from langchain.prompts import ChatPromptTemplate
|
| 5 |
|
| 6 |
+
# Set the OpenAI API key
|
| 7 |
os.environ['OPENAI_API_KEY'] = os.getenv('api_key')
|
| 8 |
|
| 9 |
+
# Initialize the OpenAI model
|
| 10 |
llm = ChatOpenAI(model='gpt-3.5-turbo', temperature=0, max_tokens=150)
|
| 11 |
|
| 12 |
+
# Streamlit app header
|
| 13 |
st.header("Translation Application")
|
| 14 |
|
| 15 |
+
# Input language selection
|
| 16 |
il = st.selectbox(
|
| 17 |
+
"Please select the input language",
|
| 18 |
+
("Hindi", "English", "Bengali")
|
| 19 |
+
)
|
| 20 |
st.write("You selected:", il)
|
| 21 |
|
| 22 |
+
# Output language selection
|
| 23 |
ol = st.selectbox(
|
| 24 |
+
"Please select the output language",
|
| 25 |
+
("Hindi", "English", "Bengali")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
+
st.write("You selected:", ol)
|
| 28 |
|
| 29 |
+
# Input text area
|
| 30 |
input_text = st.text_area('Input Text', height=300)
|
| 31 |
|
| 32 |
+
# Define the translation prompt
|
| 33 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 34 |
+
[('system', 'You are a good assistant for translation from {il} to {ol}'),
|
| 35 |
+
('user', '{i}')]
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
# Create the chain
|
| 39 |
+
chain = prompt | llm
|
|
|
|
| 40 |
|
| 41 |
+
# Handle the submit button
|
| 42 |
if st.button('Submit'):
|
| 43 |
if input_text:
|
| 44 |
+
# Invoke the chain with input parameters
|
| 45 |
+
response = chain.invoke({
|
| 46 |
+
'il': il,
|
| 47 |
+
'ol': ol,
|
| 48 |
+
'i': input_text
|
| 49 |
+
})
|
| 50 |
+
# Display the translated text
|
| 51 |
+
st.write(response.content)
|
| 52 |
else:
|
| 53 |
+
st.write('Please enter text to translate.')
|