| import openai |
| import os |
| import streamlit as st |
| from PIL import Image |
| from gtts import gTTS |
| import tempfile |
| import shutil |
| import re |
|
|
| |
| def translate_to_language(api_key, text, language): |
| """ |
| Translates English text to the target language using OpenAI's API and provides 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 {language}:\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": f"You are a helpful assistant who provides the pronunciation in phonetic script of {language} text."}, |
| {"role": "user", "content": f"Provide the pronunciation for the following {language} text:\n\n{translated_text}"} |
| ] |
| |
| |
| response_pronunciation = openai.ChatCompletion.create( |
| model="gpt-4o", |
| messages=messages_pronunciation, |
| max_tokens=300, |
| 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"An unexpected error occurred: {str(e)}", None |
|
|
| |
| def clean_pronunciation(pronunciation_text): |
| |
| pronunciation_cleaned = re.sub(r"^Sure! The pronunciation for the.*?text.*?is[:]*", "", pronunciation_text).strip() |
| return pronunciation_cleaned |
|
|
| |
| def generate_audio_from_text(text, language_code): |
| tts = gTTS(text, lang=language_code) |
| |
| temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") |
| tts.save(temp_audio_file.name) |
| return temp_audio_file.name |
|
|
| |
| st.title("English to Multiple Language Translator with Pronunciation") |
| st.markdown("Translate English text into Japanese, Spanish, Italian, and German and get their pronunciation (phonetic).") |
|
|
| translateimg = Image.open("Untitled.png") |
| st.image(translateimg, use_container_width=True) |
|
|
| |
| api_key = os.getenv("OPENAI_API_KEY") |
|
|
| |
| english_text = st.text_area("Enter the English text to translate") |
|
|
| |
| languages = ["Japanese", "Spanish", "Italian", "German"] |
| selected_language = st.selectbox("Select the target language", languages) |
|
|
| |
| progress_bar = st.progress(0) |
| progress_text = st.empty() |
|
|
| |
| language_codes = { |
| "Japanese": "ja", |
| "Spanish": "es", |
| "Italian": "it", |
| "German": "de" |
| } |
|
|
| |
| if st.button("Translate"): |
| if api_key and english_text: |
| try: |
| |
| progress_text.text(f"Translating text to {selected_language}...") |
| progress_bar.progress(33) |
|
|
| |
| translated_text, pronunciation = translate_to_language(api_key, english_text, selected_language) |
|
|
| |
| if pronunciation: |
| progress_text.text(f"Generating {selected_language} pronunciation...") |
| progress_bar.progress(66) |
|
|
| |
| cleaned_pronunciation = clean_pronunciation(pronunciation) |
|
|
| st.markdown("### Translation Result:") |
| st.write(f"**English Text:** {english_text}") |
| st.write(f"**{selected_language} Translation:** {translated_text}") |
| st.write(f"**Pronunciation:** {cleaned_pronunciation}") |
|
|
| |
| result_text = f"English Text: {english_text}\n\n{selected_language} Translation: {translated_text}\nPronunciation: {cleaned_pronunciation}" |
|
|
| |
| with open("translation_result.txt", "w") as file: |
| file.write(result_text) |
|
|
| |
| with open("translation_result.txt", "rb") as file: |
| st.download_button( |
| label="Download Translation Result", |
| data=file, |
| file_name="translation_result.txt", |
| mime="text/plain" |
| ) |
|
|
| |
| progress_text.text(f"Generating pronunciation audio for {selected_language}...") |
| progress_bar.progress(100) |
|
|
| |
| audio_file_path = generate_audio_from_text(cleaned_pronunciation, language_codes[selected_language]) |
|
|
| |
| st.audio(audio_file_path, format="audio/mp3") |
|
|
| translateimg2 = Image.open("v3.png") |
| st.image(translateimg2, width=150) |
|
|
| else: |
| st.error(translated_text) |
|
|
| except Exception as e: |
| st.error(f"An error occurred: {str(e)}") |
| else: |
| if not api_key: |
| st.error("API key is missing. Please add it as a secret in Hugging Face Settings.") |
| else: |
| st.error("Please provide text to translate.") |
|
|