Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,9 @@ import tempfile
|
|
7 |
import shutil
|
8 |
import re
|
9 |
|
10 |
-
def translate_to_japanese(api_key, text):
|
11 |
"""
|
12 |
-
Translates English text to Japanese using OpenAI's API and provides pronunciation.
|
13 |
"""
|
14 |
# Validate input
|
15 |
if not api_key:
|
@@ -20,10 +20,18 @@ def translate_to_japanese(api_key, text):
|
|
20 |
# Set the OpenAI API key
|
21 |
openai.api_key = api_key
|
22 |
|
23 |
-
# Define the messages for the chat model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
messages_translation = [
|
25 |
{"role": "system", "content": "You are a helpful translator."},
|
26 |
-
{"role": "user", "content": f"
|
27 |
]
|
28 |
|
29 |
try:
|
@@ -89,6 +97,12 @@ api_key = os.getenv("OPENAI_API_KEY")
|
|
89 |
# Input field for the text
|
90 |
english_text = st.text_area("Enter the English text to translate")
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
# Button to trigger the translation
|
93 |
if st.button("Translate"):
|
94 |
if api_key and english_text:
|
@@ -101,7 +115,7 @@ if st.button("Translate"):
|
|
101 |
progress_text.text("Translating text...")
|
102 |
progress_bar.progress(33) # Update progress bar to 33%
|
103 |
|
104 |
-
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
105 |
|
106 |
# Step 2: Check if translation was successful
|
107 |
if pronunciation:
|
|
|
7 |
import shutil
|
8 |
import re
|
9 |
|
10 |
+
def translate_to_japanese(api_key, text, tone):
|
11 |
"""
|
12 |
+
Translates English text to Japanese using OpenAI's API and provides pronunciation with a specified tone/style.
|
13 |
"""
|
14 |
# Validate input
|
15 |
if not api_key:
|
|
|
20 |
# Set the OpenAI API key
|
21 |
openai.api_key = api_key
|
22 |
|
23 |
+
# Define the messages for the chat model with the selected tone
|
24 |
+
tone_prompt = ""
|
25 |
+
if tone == "Formal":
|
26 |
+
tone_prompt = "Please translate this text into formal Japanese."
|
27 |
+
elif tone == "Casual":
|
28 |
+
tone_prompt = "Please translate this text into casual Japanese."
|
29 |
+
elif tone == "Professional":
|
30 |
+
tone_prompt = "Please translate this text into professional Japanese."
|
31 |
+
|
32 |
messages_translation = [
|
33 |
{"role": "system", "content": "You are a helpful translator."},
|
34 |
+
{"role": "user", "content": f"{tone_prompt}\n\n{text}"}
|
35 |
]
|
36 |
|
37 |
try:
|
|
|
97 |
# Input field for the text
|
98 |
english_text = st.text_area("Enter the English text to translate")
|
99 |
|
100 |
+
# Dropdown menu for translation tone/style
|
101 |
+
tone_option = st.selectbox(
|
102 |
+
"Select Translation Tone/Style",
|
103 |
+
["Formal", "Casual", "Professional"]
|
104 |
+
)
|
105 |
+
|
106 |
# Button to trigger the translation
|
107 |
if st.button("Translate"):
|
108 |
if api_key and english_text:
|
|
|
115 |
progress_text.text("Translating text...")
|
116 |
progress_bar.progress(33) # Update progress bar to 33%
|
117 |
|
118 |
+
japanese_text, pronunciation = translate_to_japanese(api_key, english_text, tone_option)
|
119 |
|
120 |
# Step 2: Check if translation was successful
|
121 |
if pronunciation:
|