Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import streamlit as st
|
|
3 |
|
4 |
def translate_to_japanese(api_key, text):
|
5 |
"""
|
6 |
-
Translates English text to Japanese using OpenAI's API.
|
7 |
"""
|
8 |
# Validate input
|
9 |
if not api_key:
|
@@ -15,32 +15,49 @@ def translate_to_japanese(api_key, text):
|
|
15 |
openai.api_key = api_key
|
16 |
|
17 |
# Define the messages for the chat model
|
18 |
-
|
19 |
{"role": "system", "content": "You are a helpful translator."},
|
20 |
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"}
|
21 |
]
|
22 |
|
23 |
try:
|
24 |
-
# Call the OpenAI API to get the translation
|
25 |
-
|
26 |
model="gpt-3.5-turbo", # Use the correct endpoint for chat models
|
27 |
-
messages=
|
28 |
-
max_tokens=150,
|
29 |
temperature=0.5
|
30 |
)
|
31 |
|
32 |
-
# Extract the
|
33 |
-
japanese_translation =
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
except openai.error.OpenAIError as e:
|
37 |
-
return f"OpenAI API error: {str(e)}"
|
38 |
except Exception as e:
|
39 |
-
return f"An unexpected error occurred: {str(e)}"
|
40 |
|
41 |
# Streamlit UI
|
42 |
-
st.title("English to Japanese Translator")
|
43 |
-
st.markdown("Translate English text into Japanese using OpenAI's API.")
|
44 |
|
45 |
# Input fields for the API key and text
|
46 |
api_key = st.text_input("Enter your OpenAI API key", type="password")
|
@@ -49,8 +66,12 @@ english_text = st.text_area("Enter the English text to translate")
|
|
49 |
# Button to trigger the translation
|
50 |
if st.button("Translate"):
|
51 |
if api_key and english_text:
|
52 |
-
japanese_text = translate_to_japanese(api_key, english_text)
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
st.error("Please provide both an API key and text to translate.")
|
|
|
3 |
|
4 |
def translate_to_japanese(api_key, text):
|
5 |
"""
|
6 |
+
Translates English text to Japanese using OpenAI's API and provides pronunciation.
|
7 |
"""
|
8 |
# Validate input
|
9 |
if not api_key:
|
|
|
15 |
openai.api_key = api_key
|
16 |
|
17 |
# Define the messages for the chat model
|
18 |
+
messages_translation = [
|
19 |
{"role": "system", "content": "You are a helpful translator."},
|
20 |
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"}
|
21 |
]
|
22 |
|
23 |
try:
|
24 |
+
# Call the OpenAI API to get the Japanese translation
|
25 |
+
response_translation = openai.ChatCompletion.create(
|
26 |
model="gpt-3.5-turbo", # Use the correct endpoint for chat models
|
27 |
+
messages=messages_translation,
|
28 |
+
max_tokens=150,
|
29 |
temperature=0.5
|
30 |
)
|
31 |
|
32 |
+
# Extract the Japanese translation from the response
|
33 |
+
japanese_translation = response_translation.choices[0].message['content'].strip()
|
34 |
+
|
35 |
+
# Define the messages for the pronunciation (Romaji) request
|
36 |
+
messages_pronunciation = [
|
37 |
+
{"role": "system", "content": "You are a helpful assistant who provides the Romaji (Japanese pronunciation in Latin script) of Japanese text."},
|
38 |
+
{"role": "user", "content": f"Provide the Romaji pronunciation for the following Japanese text:\n\n{japanese_translation}"}
|
39 |
+
]
|
40 |
+
|
41 |
+
# Call the OpenAI API to get the pronunciation
|
42 |
+
response_pronunciation = openai.ChatCompletion.create(
|
43 |
+
model="gpt-3.5-turbo",
|
44 |
+
messages=messages_pronunciation,
|
45 |
+
max_tokens=150,
|
46 |
+
temperature=0.5
|
47 |
+
)
|
48 |
+
|
49 |
+
# Extract the pronunciation (Romaji) from the response
|
50 |
+
pronunciation = response_pronunciation.choices[0].message['content'].strip()
|
51 |
+
return japanese_translation, pronunciation
|
52 |
+
|
53 |
except openai.error.OpenAIError as e:
|
54 |
+
return f"OpenAI API error: {str(e)}", None
|
55 |
except Exception as e:
|
56 |
+
return f"An unexpected error occurred: {str(e)}", None
|
57 |
|
58 |
# Streamlit UI
|
59 |
+
st.title("English to Japanese Translator with Pronunciation")
|
60 |
+
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji) using OpenAI's API.")
|
61 |
|
62 |
# Input fields for the API key and text
|
63 |
api_key = st.text_input("Enter your OpenAI API key", type="password")
|
|
|
66 |
# Button to trigger the translation
|
67 |
if st.button("Translate"):
|
68 |
if api_key and english_text:
|
69 |
+
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
70 |
+
if pronunciation:
|
71 |
+
st.markdown("### Translation Result:")
|
72 |
+
st.write(f"**Japanese Output:** {japanese_text}")
|
73 |
+
st.write(f"**Pronunciation:** {pronunciation}")
|
74 |
+
else:
|
75 |
+
st.error(japanese_text) # Display error message if API call fails
|
76 |
else:
|
77 |
st.error("Please provide both an API key and text to translate.")
|