GPTranslator / app.py
rushankg's picture
Update app.py
de412e8 verified
import os
import openai
import streamlit as st
# Set up OpenAI API key
openai.api_key = os.getenv("OPENAI_KEY")
# Supported languages
languages = ['English', 'French', 'Spanish', 'Hindi', 'Punjabi']
# Streamlit app
def main():
st.title("Language Translator")
# User input for input language
input_language = st.selectbox("Select Input Language", languages)
# User input for output language
output_language = st.selectbox("Select Output Language", languages)
# Text input box for user to input text
input_text = st.text_area("Enter the text to translate")
if st.button("Translate"):
if input_text.strip() == "":
st.error("Please enter some text to translate.")
elif input_language == output_language:
st.warning("Input and output languages are the same. Please select different languages.")
else:
# Perform translation
translation = translate_text(input_text, input_language, output_language)
st.success("Translation:")
st.write(translation)
# Function to translate text using GPT-3.5
def translate_text(text, input_language, output_language):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"You translate user input from {input_language} to {output_language}"},
{"role": "user", "content": text}
],
max_tokens=2000,
temperature=0
)
translation = response.choices[0].message['content'].strip()
return translation
if __name__ == "__main__":
main()