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
|
| 2 |
+
import os
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
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:
|
| 16 |
+
return "Error: API key is missing.", None
|
| 17 |
+
if not text:
|
| 18 |
+
return "Error: Input text is empty.", None
|
| 19 |
+
|
| 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"Translate the following English text to Japanese:\n\n{text}"}
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Call the OpenAI API to get the Japanese translation
|
| 31 |
+
response_translation = openai.ChatCompletion.create(
|
| 32 |
+
model="gpt-4o", # Use the correct endpoint for chat models
|
| 33 |
+
messages=messages_translation,
|
| 34 |
+
max_tokens=300,
|
| 35 |
+
temperature=0.5
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Extract the Japanese translation from the response
|
| 39 |
+
japanese_translation = response_translation.choices[0].message['content'].strip()
|
| 40 |
+
|
| 41 |
+
# Define the messages for the pronunciation (Romaji) request
|
| 42 |
+
messages_pronunciation = [
|
| 43 |
+
{"role": "system", "content": "You are a helpful assistant who provides the Romaji (Japanese pronunciation in Latin script) of Japanese text."},
|
| 44 |
+
{"role": "user", "content": f"Provide the Romaji pronunciation for the following Japanese text:\n\n{japanese_translation}"}
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
# Call the OpenAI API to get the pronunciation
|
| 48 |
+
response_pronunciation = openai.ChatCompletion.create(
|
| 49 |
+
model="gpt-4o",
|
| 50 |
+
messages=messages_pronunciation,
|
| 51 |
+
max_tokens=300,
|
| 52 |
+
temperature=0.5
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Extract the pronunciation (Romaji) from the response
|
| 56 |
+
pronunciation = response_pronunciation.choices[0].message['content'].strip()
|
| 57 |
+
|
| 58 |
+
return japanese_translation, pronunciation
|
| 59 |
+
|
| 60 |
+
except openai.error.OpenAIError as e:
|
| 61 |
+
return f"OpenAI API error: {str(e)}", None
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"An unexpected error occurred: {str(e)}", None
|
| 64 |
+
|
| 65 |
+
# Function to clean pronunciation text
|
| 66 |
+
def clean_pronunciation(pronunciation_text):
|
| 67 |
+
# Remove introductory phrases like "Sure! The Romaji pronunciation..."
|
| 68 |
+
pronunciation_cleaned = re.sub(r"^Sure! The Romaji pronunciation for the Japanese text.*?is[:]*", "", pronunciation_text).strip()
|
| 69 |
+
return pronunciation_cleaned
|
| 70 |
+
|
| 71 |
+
# Function to generate audio file from text using gTTS
|
| 72 |
+
def generate_audio_from_text(text):
|
| 73 |
+
tts = gTTS(text, lang='ja') # 'ja' for Japanese language
|
| 74 |
+
# Save audio to a temporary file
|
| 75 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 76 |
+
tts.save(temp_audio_file.name)
|
| 77 |
+
return temp_audio_file.name
|
| 78 |
+
|
| 79 |
+
# Streamlit UI
|
| 80 |
+
st.title("English to Japanese Translator with Pronunciation")
|
| 81 |
+
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji).")
|
| 82 |
+
|
| 83 |
+
translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory
|
| 84 |
+
st.image(translateimg, use_container_width=True) # Adjust the size as per preference
|
| 85 |
+
|
| 86 |
+
# Access the API key from Hugging Face Secrets
|
| 87 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 88 |
+
|
| 89 |
+
# Input field for the text
|
| 90 |
+
english_text = st.text_area("Enter the English text to translate")
|
| 91 |
+
|
| 92 |
+
# Initialize the progress bar and progress text above the translate button
|
| 93 |
+
progress_bar = st.progress(0)
|
| 94 |
+
progress_text = st.empty() # To show the progress text
|
| 95 |
+
|
| 96 |
+
# Button to trigger the translation
|
| 97 |
+
if st.button("Translate"):
|
| 98 |
+
if api_key and english_text:
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
# Step 1: Request translation
|
| 102 |
+
progress_text.text("Translating text...")
|
| 103 |
+
progress_bar.progress(33) # Update progress bar to 33%
|
| 104 |
+
|
| 105 |
+
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
| 106 |
+
|
| 107 |
+
# Step 2: Check if translation was successful
|
| 108 |
+
if pronunciation:
|
| 109 |
+
progress_text.text("Generating Romaji pronunciation...")
|
| 110 |
+
progress_bar.progress(66) # Update progress bar to 66%
|
| 111 |
+
|
| 112 |
+
# Clean pronunciation (remove unnecessary parts)
|
| 113 |
+
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
| 114 |
+
|
| 115 |
+
st.markdown("### Translation Result:")
|
| 116 |
+
st.write(f"**English Text:** {english_text}")
|
| 117 |
+
st.write(f"**Japanese Output:** {japanese_text}")
|
| 118 |
+
st.write(f"**Pronunciation:** {cleaned_pronunciation}")
|
| 119 |
+
|
| 120 |
+
# Save the result in a text file
|
| 121 |
+
result_text = f"English Text: {english_text}\n\nJapanese Translation: {japanese_text}\nPronunciation: {cleaned_pronunciation}"
|
| 122 |
+
|
| 123 |
+
# Write to a text file
|
| 124 |
+
with open("translation_result.txt", "w") as file:
|
| 125 |
+
file.write(result_text)
|
| 126 |
+
|
| 127 |
+
# Create a download button for the user to download the file
|
| 128 |
+
with open("translation_result.txt", "rb") as file:
|
| 129 |
+
st.download_button(
|
| 130 |
+
label="Download Translation Result",
|
| 131 |
+
data=file,
|
| 132 |
+
file_name="translation_result.txt",
|
| 133 |
+
mime="text/plain"
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Step 3: Generate audio for pronunciation
|
| 137 |
+
progress_text.text("Generating pronunciation audio...")
|
| 138 |
+
progress_bar.progress(100) # Update progress bar to 100%
|
| 139 |
+
|
| 140 |
+
audio_file_path = generate_audio_from_text(cleaned_pronunciation)
|
| 141 |
+
|
| 142 |
+
# Provide a button to play the pronunciation audio
|
| 143 |
+
st.audio(audio_file_path, format="audio/mp3")
|
| 144 |
+
|
| 145 |
+
translateimg2 = Image.open("v3.png") # Ensure the file is in the correct directory
|
| 146 |
+
st.image(translateimg2, width=150) # Adjust the size as per preference
|
| 147 |
+
|
| 148 |
+
else:
|
| 149 |
+
st.error(japanese_text) # Display error message if API call fails
|
| 150 |
+
|
| 151 |
+
except Exception as e:
|
| 152 |
+
st.error(f"An error occurred: {str(e)}")
|
| 153 |
+
else:
|
| 154 |
+
if not api_key:
|
| 155 |
+
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
| 156 |
+
else:
|
| 157 |
+
st.error("Please provide text to translate.")
|