Spaces:
Paused
Paused
File size: 4,686 Bytes
e4be952 de5a023 e4be952 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import openai
import os
import gradio as gr
from gtts import gTTS
import tempfile
import re # regular expression
# import requests
### Translation function
def translate_to_japanese(api_key, text): #openai api key
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
#### translation
messages_translation = [
{"role": "system", "content": "You are a helpful translator."},
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"} #prompt template
]
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}"} #prompt template
]
### pronounciation
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() ### the pron of ___ is konichuwa
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
#### clean pronounciation
def clean_pronunciation(pronunciation_text):
pronunciation_cleaned=re.sub(r"(?i)(the pronunciation.*?is[:οΌ]*|\n|\"|\')", "", pronunciation_text).strip()
# Keep only the text inside [ ] if present
match = re.search(r"\[.*?\]", pronunciation_cleaned)
if match:
pronunciation_cleaned = match.group(0)
return pronunciation_cleaned ### the balah blah ignore [dsadasdasd] response: [dsadasdasd]
#### audio generation function
def generate_audio_from_text(text):
tts=gTTS(text, lang="ja")
temp_audio_file=tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") #.wav
tts.save(temp_audio_file.name)
return temp_audio_file.name
# ---------------------- MAIN FUNCTION ----------------------
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) ### konichuwa
# Create result text file
result_text = f"English Text: {english_text}\n\nJapanese Translation: {translated_text}\nPronunciation: {cleaned_pronunciation}" ## hello ___ konichuwa
result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
with open(result_file.name, "w", encoding="utf-8") as f:
f.write(result_text)
# Generate pronunciation audio
audio_path = generate_audio_from_text(cleaned_pronunciation)
return "", translated_text, cleaned_pronunciation, result_file.name, audio_path
### UI design
with gr.Blocks(theme=gr.themes.Ocean()) 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() |