|
|
import openai |
|
|
import os |
|
|
import gradio as gr |
|
|
from gtts import gTTS |
|
|
import tempfile |
|
|
import re |
|
|
|
|
|
|
|
|
def translate_to_japanese(api_key, text): |
|
|
""" |
|
|
Translates English text to Japanese and provides phonetic pronunciation. |
|
|
""" |
|
|
if not api_key: |
|
|
return "Error: API key is missing.", None |
|
|
if not text: |
|
|
return "Error: Input text is empty.", None |
|
|
|
|
|
openai.api_key = api_key |
|
|
|
|
|
|
|
|
messages_translation = [ |
|
|
{"role": "system", "content": "You are a helpful translator."}, |
|
|
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"} |
|
|
] |
|
|
|
|
|
try: |
|
|
response_translation = openai.ChatCompletion.create( |
|
|
model="gpt-4o", |
|
|
messages=messages_translation, |
|
|
max_tokens=300, |
|
|
temperature=0.5 |
|
|
) |
|
|
translated_text = response_translation.choices[0].message['content'].strip() |
|
|
|
|
|
|
|
|
messages_pronunciation = [ |
|
|
{"role": "system", "content": "You are a helpful assistant who provides only the phonetic pronunciation (Romaji) of Japanese text, in square brackets."}, |
|
|
{"role": "user", "content": f"Provide only the Romaji pronunciation (phonetic) for this Japanese text:\n\n{translated_text}"} |
|
|
] |
|
|
|
|
|
response_pronunciation = openai.ChatCompletion.create( |
|
|
model="gpt-4o", |
|
|
messages=messages_pronunciation, |
|
|
max_tokens=150, |
|
|
temperature=0.5 |
|
|
) |
|
|
pronunciation = response_pronunciation.choices[0].message['content'].strip() |
|
|
|
|
|
return translated_text, pronunciation |
|
|
|
|
|
except openai.error.OpenAIError as e: |
|
|
return f"OpenAI API error: {str(e)}", None |
|
|
except Exception as e: |
|
|
return f"Unexpected error: {str(e)}", None |
|
|
|
|
|
|
|
|
|
|
|
def clean_pronunciation(pronunciation_text): |
|
|
|
|
|
pronunciation_cleaned = re.sub(r"(?i)(the pronunciation.*?is[:οΌ]*|\n|\"|\')", "", pronunciation_text).strip() |
|
|
|
|
|
match = re.search(r"\[.*?\]", pronunciation_cleaned) |
|
|
if match: |
|
|
pronunciation_cleaned = match.group(0) |
|
|
return pronunciation_cleaned |
|
|
|
|
|
|
|
|
|
|
|
def generate_audio_from_text(text): |
|
|
tts = gTTS(text, lang="ja") |
|
|
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") |
|
|
tts.save(temp_audio_file.name) |
|
|
return temp_audio_file.name |
|
|
|
|
|
|
|
|
|
|
|
def process_translation(api_key, english_text): |
|
|
if not api_key: |
|
|
return "Missing API Key", "", "", None, None |
|
|
if not english_text: |
|
|
return "Please enter English text.", "", "", None, None |
|
|
|
|
|
translated_text, pronunciation = translate_to_japanese(api_key, english_text) |
|
|
|
|
|
if not pronunciation: |
|
|
return translated_text, "", "", None, None |
|
|
|
|
|
cleaned_pronunciation = clean_pronunciation(pronunciation) |
|
|
|
|
|
|
|
|
result_text = f"English Text: {english_text}\n\nJapanese Translation: {translated_text}\nPronunciation: {cleaned_pronunciation}" |
|
|
result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt") |
|
|
with open(result_file.name, "w", encoding="utf-8") as f: |
|
|
f.write(result_text) |
|
|
|
|
|
|
|
|
audio_path = generate_audio_from_text(cleaned_pronunciation) |
|
|
|
|
|
return "", translated_text, cleaned_pronunciation, result_file.name, audio_path |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as app: |
|
|
gr.Markdown("# π―π΅ English β Japanese Translator with Pronunciation") |
|
|
gr.Markdown("Translate English text into **Japanese**, get **Romaji pronunciation**, and listen to it!") |
|
|
|
|
|
api_key = gr.Textbox(label="π OpenAI API Key", type="password", placeholder="Enter your OpenAI API key") |
|
|
english_text = gr.Textbox(label="π Enter English Text", lines=4, placeholder="Type something in English...") |
|
|
|
|
|
translate_button = gr.Button("Translate to Japanese π") |
|
|
|
|
|
translation_output = gr.Textbox(label="π Japanese Translation") |
|
|
pronunciation_output = gr.Textbox(label="π€ Pronunciation (Romaji)") |
|
|
audio_output = gr.Audio(label="π Listen to Pronunciation", type="filepath") |
|
|
file_output = gr.File(label="β¬οΈ Download Translation Result") |
|
|
|
|
|
translate_button.click( |
|
|
fn=process_translation, |
|
|
inputs=[api_key, english_text], |
|
|
outputs=[gr.Label(), translation_output, pronunciation_output, file_output, audio_output] |
|
|
) |
|
|
|
|
|
app.launch() |