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 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
def translate_to_japanese(api_key, text):
|
7 |
+
"""
|
8 |
+
Translates English text to Japanese using OpenAI's API and provides pronunciation.
|
9 |
+
"""
|
10 |
+
# Validate input
|
11 |
+
if not api_key:
|
12 |
+
return "Error: API key is missing."
|
13 |
+
if not text:
|
14 |
+
return "Error: Input text is empty."
|
15 |
+
|
16 |
+
# Set the OpenAI API key
|
17 |
+
openai.api_key = api_key
|
18 |
+
|
19 |
+
# Define the messages for the chat model
|
20 |
+
messages_translation = [
|
21 |
+
{"role": "system", "content": "You are a helpful translator."},
|
22 |
+
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"}
|
23 |
+
]
|
24 |
+
|
25 |
+
try:
|
26 |
+
# Call the OpenAI API to get the Japanese translation
|
27 |
+
response_translation = openai.ChatCompletion.create(
|
28 |
+
model="gpt-4o", # Use the correct endpoint for chat models
|
29 |
+
messages=messages_translation,
|
30 |
+
max_tokens=300,
|
31 |
+
temperature=0.5
|
32 |
+
)
|
33 |
+
|
34 |
+
# Extract the Japanese translation from the response
|
35 |
+
japanese_translation = response_translation.choices[0].message['content'].strip()
|
36 |
+
|
37 |
+
# Define the messages for the pronunciation (Romaji) request
|
38 |
+
messages_pronunciation = [
|
39 |
+
{"role": "system", "content": "You are a helpful assistant who provides the Romaji (Japanese pronunciation in Latin script) of Japanese text."},
|
40 |
+
{"role": "user", "content": f"Provide the Romaji pronunciation for the following Japanese text:\n\n{japanese_translation}"}
|
41 |
+
]
|
42 |
+
|
43 |
+
# Call the OpenAI API to get the pronunciation
|
44 |
+
response_pronunciation = openai.ChatCompletion.create(
|
45 |
+
model="gpt-4o",
|
46 |
+
messages=messages_pronunciation,
|
47 |
+
max_tokens=300,
|
48 |
+
temperature=0.5
|
49 |
+
)
|
50 |
+
|
51 |
+
# Extract the pronunciation (Romaji) from the response
|
52 |
+
pronunciation = response_pronunciation.choices[0].message['content'].strip()
|
53 |
+
return japanese_translation, pronunciation
|
54 |
+
|
55 |
+
except openai.error.OpenAIError as e:
|
56 |
+
return f"OpenAI API error: {str(e)}", None
|
57 |
+
except Exception as e:
|
58 |
+
return f"An unexpected error occurred: {str(e)}", None
|
59 |
+
|
60 |
+
# Streamlit UI
|
61 |
+
st.title("English to Japanese Translator with Pronunciation")
|
62 |
+
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji) using OpenAI's API.")
|
63 |
+
|
64 |
+
translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory
|
65 |
+
st.image(translateimg, use_container_width=True) # Adjust the size as per preference
|
66 |
+
|
67 |
+
# Access the API key from Hugging Face Secrets
|
68 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
69 |
+
|
70 |
+
# Input field for the text
|
71 |
+
english_text = st.text_area("Enter the English text to translate")
|
72 |
+
|
73 |
+
# Button to trigger the translation
|
74 |
+
if st.button("Translate"):
|
75 |
+
if api_key and english_text:
|
76 |
+
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
77 |
+
if pronunciation:
|
78 |
+
st.markdown("### Translation Result:")
|
79 |
+
st.write(f"**English Text:** {english_text}")
|
80 |
+
st.write(f"**Japanese Output:** {japanese_text}")
|
81 |
+
st.write(f"**Pronunciation:** {pronunciation}")
|
82 |
+
|
83 |
+
# Save the result in a text file
|
84 |
+
result_text = f"English Text: {english_text}\n\nJapanese Translation: {japanese_text}\nPronunciation: {pronunciation}"
|
85 |
+
|
86 |
+
# Write to a text file
|
87 |
+
with open("translation_result.txt", "w") as file:
|
88 |
+
file.write(result_text)
|
89 |
+
|
90 |
+
# Create a download button for the user to download the file
|
91 |
+
with open("translation_result.txt", "rb") as file:
|
92 |
+
st.download_button(
|
93 |
+
label="Download Translation Result",
|
94 |
+
data=file,
|
95 |
+
file_name="translation_result.txt",
|
96 |
+
mime="text/plain"
|
97 |
+
)
|
98 |
+
|
99 |
+
translateimg2 = Image.open("v3.png") # Ensure the file is in the correct directory
|
100 |
+
st.image(translateimg2, width=150) # Adjust the size as per preference
|
101 |
+
else:
|
102 |
+
st.error(japanese_text) # Display error message if API call fails
|
103 |
+
else:
|
104 |
+
if not api_key:
|
105 |
+
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
106 |
+
else:
|
107 |
+
st.error("Please provide text to translate.")
|
108 |
+
|
109 |
+
(Now I want to have the pronounciation text also be in voice can I do this?)
|
110 |
+
|
111 |
+
import openai
|
112 |
+
import os
|
113 |
+
import streamlit as st
|
114 |
+
from PIL import Image
|
115 |
+
from gtts import gTTS
|
116 |
+
import tempfile
|
117 |
+
import shutil
|
118 |
+
|
119 |
+
def translate_to_japanese(api_key, text):
|
120 |
+
"""
|
121 |
+
Translates English text to Japanese using OpenAI's API and provides pronunciation.
|
122 |
+
"""
|
123 |
+
# Validate input
|
124 |
+
if not api_key:
|
125 |
+
return "Error: API key is missing."
|
126 |
+
if not text:
|
127 |
+
return "Error: Input text is empty."
|
128 |
+
|
129 |
+
# Set the OpenAI API key
|
130 |
+
openai.api_key = api_key
|
131 |
+
|
132 |
+
# Define the messages for the chat model
|
133 |
+
messages_translation = [
|
134 |
+
{"role": "system", "content": "You are a helpful translator."},
|
135 |
+
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"}
|
136 |
+
]
|
137 |
+
|
138 |
+
try:
|
139 |
+
# Call the OpenAI API to get the Japanese translation
|
140 |
+
response_translation = openai.ChatCompletion.create(
|
141 |
+
model="gpt-4o", # Use the correct endpoint for chat models
|
142 |
+
messages=messages_translation,
|
143 |
+
max_tokens=300,
|
144 |
+
temperature=0.5
|
145 |
+
)
|
146 |
+
|
147 |
+
# Extract the Japanese translation from the response
|
148 |
+
japanese_translation = response_translation.choices[0].message['content'].strip()
|
149 |
+
|
150 |
+
# Define the messages for the pronunciation (Romaji) request
|
151 |
+
messages_pronunciation = [
|
152 |
+
{"role": "system", "content": "You are a helpful assistant who provides the Romaji (Japanese pronunciation in Latin script) of Japanese text."},
|
153 |
+
{"role": "user", "content": f"Provide the Romaji pronunciation for the following Japanese text:\n\n{japanese_translation}"}
|
154 |
+
]
|
155 |
+
|
156 |
+
# Call the OpenAI API to get the pronunciation
|
157 |
+
response_pronunciation = openai.ChatCompletion.create(
|
158 |
+
model="gpt-4o",
|
159 |
+
messages=messages_pronunciation,
|
160 |
+
max_tokens=300,
|
161 |
+
temperature=0.5
|
162 |
+
)
|
163 |
+
|
164 |
+
# Extract the pronunciation (Romaji) from the response
|
165 |
+
pronunciation = response_pronunciation.choices[0].message['content'].strip()
|
166 |
+
return japanese_translation, pronunciation
|
167 |
+
|
168 |
+
except openai.error.OpenAIError as e:
|
169 |
+
return f"OpenAI API error: {str(e)}", None
|
170 |
+
except Exception as e:
|
171 |
+
return f"An unexpected error occurred: {str(e)}", None
|
172 |
+
|
173 |
+
# Function to generate audio file from text using gTTS
|
174 |
+
def generate_audio_from_text(text):
|
175 |
+
tts = gTTS(text, lang='ja') # 'ja' for Japanese language
|
176 |
+
# Save audio to a temporary file
|
177 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
178 |
+
tts.save(temp_audio_file.name)
|
179 |
+
return temp_audio_file.name
|
180 |
+
|
181 |
+
# Streamlit UI
|
182 |
+
st.title("English to Japanese Translator with Pronunciation")
|
183 |
+
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji) using OpenAI's API.")
|
184 |
+
|
185 |
+
translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory
|
186 |
+
st.image(translateimg, use_container_width=True) # Adjust the size as per preference
|
187 |
+
|
188 |
+
# Access the API key from Hugging Face Secrets
|
189 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
190 |
+
|
191 |
+
# Input field for the text
|
192 |
+
english_text = st.text_area("Enter the English text to translate")
|
193 |
+
|
194 |
+
# Button to trigger the translation
|
195 |
+
if st.button("Translate"):
|
196 |
+
if api_key and english_text:
|
197 |
+
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
198 |
+
if pronunciation:
|
199 |
+
st.markdown("### Translation Result:")
|
200 |
+
st.write(f"**English Text:** {english_text}")
|
201 |
+
st.write(f"**Japanese Output:** {japanese_text}")
|
202 |
+
st.write(f"**Pronunciation:** {pronunciation}")
|
203 |
+
|
204 |
+
# Save the result in a text file
|
205 |
+
result_text = f"English Text: {english_text}\n\nJapanese Translation: {japanese_text}\nPronunciation: {pronunciation}"
|
206 |
+
|
207 |
+
# Write to a text file
|
208 |
+
with open("translation_result.txt", "w") as file:
|
209 |
+
file.write(result_text)
|
210 |
+
|
211 |
+
# Create a download button for the user to download the file
|
212 |
+
with open("translation_result.txt", "rb") as file:
|
213 |
+
st.download_button(
|
214 |
+
label="Download Translation Result",
|
215 |
+
data=file,
|
216 |
+
file_name="translation_result.txt",
|
217 |
+
mime="text/plain"
|
218 |
+
)
|
219 |
+
|
220 |
+
# Generate audio for pronunciation
|
221 |
+
audio_file_path = generate_audio_from_text(pronunciation)
|
222 |
+
|
223 |
+
# Provide a button to play the pronunciation audio
|
224 |
+
st.audio(audio_file_path, format="audio/mp3")
|
225 |
+
|
226 |
+
translateimg2 = Image.open("v3.png") # Ensure the file is in the correct directory
|
227 |
+
st.image(translateimg2, width=150) # Adjust the size as per preference
|
228 |
+
else:
|
229 |
+
st.error(japanese_text) # Display error message if API call fails
|
230 |
+
else:
|
231 |
+
if not api_key:
|
232 |
+
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
233 |
+
else:
|
234 |
+
st.error("Please provide text to translate.")
|