Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import tempfile
|
| 6 |
+
import re # regular expression
|
| 7 |
+
|
| 8 |
+
# import requests
|
| 9 |
+
|
| 10 |
+
### Translation function
|
| 11 |
+
|
| 12 |
+
def translate_to_japanese(api_key, text): #openai api key
|
| 13 |
+
if not api_key:
|
| 14 |
+
return "Error: API key is missing.", None
|
| 15 |
+
|
| 16 |
+
if not text:
|
| 17 |
+
return "Error: Input text is empty.", None
|
| 18 |
+
|
| 19 |
+
openai.api_key = api_key
|
| 20 |
+
|
| 21 |
+
#### translation
|
| 22 |
+
|
| 23 |
+
messages_translation = [
|
| 24 |
+
{"role": "system", "content": "You are a helpful translator."},
|
| 25 |
+
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"} #prompt template
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
response_translation = openai.ChatCompletion.create(
|
| 30 |
+
model="gpt-4o",
|
| 31 |
+
messages=messages_translation,
|
| 32 |
+
max_tokens=300,
|
| 33 |
+
temperature=0.5
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
translated_text=response_translation.choices[0].message['content'].strip()
|
| 37 |
+
|
| 38 |
+
messages_pronounciation = [
|
| 39 |
+
{"role": "system", "content": "You are a helpful assistant who provides only the phonetic pronunciation (Romaji) of Japanese text, in square brackets."},
|
| 40 |
+
{"role": "user", "content": f"Provide only the Romaji pronunciation (phonetic) for this Japanese text:\n\n{translated_text}"} #prompt template
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
### pronounciation
|
| 44 |
+
response_pronunciation = openai.ChatCompletion.create(
|
| 45 |
+
model="gpt-4o",
|
| 46 |
+
messages=messages_pronunciation,
|
| 47 |
+
max_tokens=150,
|
| 48 |
+
temperature=0.5
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
pronunciation=response_pronunciation.choices[0].message['content'].strip() ### the pron of ___ is konichuwa
|
| 52 |
+
|
| 53 |
+
return translated_text, 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"Unexpected error: {str(e)}", None
|
| 59 |
+
|
| 60 |
+
#### clean pronounciation
|
| 61 |
+
|
| 62 |
+
def clean_pronunciation(pronunciation_text):
|
| 63 |
+
pronunciation_cleaned=re.sub(r"(?i)(the pronunciation.*?is[:οΌ]*|\n|\"|\')", "", pronunciation_text).strip()
|
| 64 |
+
# Keep only the text inside [ ] if present
|
| 65 |
+
match = re.search(r"\[.*?\]", pronunciation_cleaned)
|
| 66 |
+
if match:
|
| 67 |
+
pronunciation_cleaned = match.group(0)
|
| 68 |
+
return pronunciation_cleaned ### the balah blah ignore [dsadasdasd] response: [dsadasdasd]
|
| 69 |
+
|
| 70 |
+
#### audio generation function
|
| 71 |
+
|
| 72 |
+
def generate_audio_from_text(text):
|
| 73 |
+
tts=gTTS(text, lang="ja")
|
| 74 |
+
temp_audio_file=tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") #.wav
|
| 75 |
+
tts.save(temp_audio_file.name)
|
| 76 |
+
return temp_audio_file.name
|
| 77 |
+
|
| 78 |
+
# ---------------------- MAIN FUNCTION ----------------------
|
| 79 |
+
|
| 80 |
+
def process_translation(api_key, english_text):
|
| 81 |
+
if not api_key:
|
| 82 |
+
return "Missing API Key", "", "", None, None
|
| 83 |
+
if not english_text:
|
| 84 |
+
return "Please enter English text.", "", "", None, None
|
| 85 |
+
|
| 86 |
+
translated_text, pronunciation = translate_to_japanese(api_key, english_text)
|
| 87 |
+
|
| 88 |
+
if not pronunciation:
|
| 89 |
+
return translated_text, "", "", None, None
|
| 90 |
+
|
| 91 |
+
cleaned_pronunciation = clean_pronunciation(pronunciation) ### konichuwa
|
| 92 |
+
|
| 93 |
+
# Create result text file
|
| 94 |
+
result_text = f"English Text: {english_text}\n\nJapanese Translation: {translated_text}\nPronunciation: {cleaned_pronunciation}" ## hello ___ konichuwa
|
| 95 |
+
result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
| 96 |
+
with open(result_file.name, "w", encoding="utf-8") as f:
|
| 97 |
+
f.write(result_text)
|
| 98 |
+
|
| 99 |
+
# Generate pronunciation audio
|
| 100 |
+
audio_path = generate_audio_from_text(cleaned_pronunciation)
|
| 101 |
+
|
| 102 |
+
return "", translated_text, cleaned_pronunciation, result_file.name, audio_path
|
| 103 |
+
|
| 104 |
+
### UI design
|
| 105 |
+
|
| 106 |
+
with gr.Blocks(theme=gr.themes.Ocean()) as app:
|
| 107 |
+
gr.Markdown("# π―π΅ English β Japanese Translator with Pronunciation")
|
| 108 |
+
gr.Markdown("Translate English text into **Japanese**, get **Romaji pronunciation**, and listen to it!")
|
| 109 |
+
|
| 110 |
+
api_key = gr.Textbox(label="π OpenAI API Key", type="password", placeholder="Enter your OpenAI API key")
|
| 111 |
+
|
| 112 |
+
english_text = gr.Textbox(label="π Enter English Text", lines=4, placeholder="Type something in English...")
|
| 113 |
+
|
| 114 |
+
translate_button = gr.Button("Translate to Japanese π")
|
| 115 |
+
|
| 116 |
+
translation_output = gr.Textbox(label="π Japanese Translation")
|
| 117 |
+
|
| 118 |
+
pronunciation_output = gr.Textbox(label="π€ Pronunciation (Romaji)")
|
| 119 |
+
|
| 120 |
+
audio_output=gr.Audio(label="π Listen to Pronunciation", type="filepath")
|
| 121 |
+
|
| 122 |
+
file_output = gr.File(label="β¬οΈ Download Translation Result")
|
| 123 |
+
|
| 124 |
+
translate_button.click(
|
| 125 |
+
fn=process_translation,
|
| 126 |
+
inputs=[api_key, english_text],
|
| 127 |
+
outputs=[gr.Label(), translation_output, pronunciation_output, file_output, audio_output]
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
app.launch()
|