File size: 1,626 Bytes
b33c26e
29eabe2
 
c414752
29eabe2
 
c414752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a84fbff
c414752
d446c1e
fe7bf79
d446c1e
 
 
 
de412e8
d446c1e
fe7bf79
d446c1e
fe7bf79
c414752
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()