import openai import os import streamlit as st from PIL import Image from gtts import gTTS import tempfile import shutil import re # Language mapping for gTTS LANGUAGE_MAP = { "English": "en", "Japanese": "ja", "French": "fr", "Spanish": "es", "German": "de", "Chinese": "zh", "Italian": "it", "Portuguese": "pt", "Korean": "ko", "Arabic": "ar" } def translate_text(api_key, text, target_language): """ Translates English text to the selected target language using OpenAI's API and provides pronunciation. """ # Validate input if not api_key: return "Error: API key is missing.", None if not text: return "Error: Input text is empty.", None # Set the OpenAI API key openai.api_key = api_key # Define the messages for the chat model messages_translation = [ {"role": "system", "content": "You are a helpful translator."}, {"role": "user", "content": f"Translate the following English text to {target_language}:\n\n{text}"} ] try: # Call the OpenAI API to get the translation response_translation = openai.ChatCompletion.create( model="gpt-4o", # Use the correct endpoint for chat models messages=messages_translation, max_tokens=300, temperature=0.5 ) # Extract the translation from the response translation = response_translation.choices[0].message['content'].strip() # Define the messages for the pronunciation request (Romaji or other phonetic systems) messages_pronunciation = [ {"role": "system", "content": "You are a helpful assistant who provides the pronunciation of the translated text."}, {"role": "user", "content": f"Provide the pronunciation for the following {target_language} text:\n\n{translation}"} ] # Call the OpenAI API to get the pronunciation response_pronunciation = openai.ChatCompletion.create( model="gpt-4o", messages=messages_pronunciation, max_tokens=300, temperature=0.5 ) # Extract the pronunciation from the response pronunciation = response_pronunciation.choices[0].message['content'].strip() return translation, 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 # Function to clean pronunciation text def clean_pronunciation(pronunciation_text): # Remove introductory phrases like "Sure! The Romaji pronunciation..." pronunciation_cleaned = re.sub(r"^Sure! The pronunciation for the text.*?is[:]*", "", pronunciation_text).strip() return pronunciation_cleaned # Function to generate audio file from text using gTTS def generate_audio_from_text(text, language_code): tts = gTTS(text, lang=language_code) # Dynamically use the language code # Save audio to a temporary file temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") tts.save(temp_audio_file.name) return temp_audio_file.name # Streamlit UI st.title("Multi-language Translator with Pronunciation") st.markdown("Translate English text into various languages and get its pronunciation.") translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory st.image(translateimg, use_container_width=True) # Adjust the size as per preference # Access the API key from Hugging Face Secrets api_key = os.getenv("OPENAI_API_KEY") # Input field for the text english_text = st.text_area("Enter the English text to translate") # Dropdown menu for language selection language_option = st.selectbox( "Select Target Language", ["English", "Japanese", "French", "Spanish", "German", "Chinese", "Italian", "Portuguese", "Korean", "Arabic"] ) # Button to trigger the translation if st.button("Translate"): if api_key and english_text: # Initialize the progress bar progress_bar = st.progress(0) progress_text = st.empty() # To show the progress text try: # Step 1: Request translation progress_text.text("Translating text...") progress_bar.progress(33) # Update progress bar to 33% # Translate text and get pronunciation translated_text, pronunciation = translate_text(api_key, english_text, language_option) # Step 2: Check if translation was successful if pronunciation: progress_text.text("Generating pronunciation...") progress_bar.progress(66) # Update progress bar to 66% # Clean pronunciation (remove unnecessary parts) cleaned_pronunciation = clean_pronunciation(pronunciation) st.markdown("### Translation Result:") st.write(f"**Original English Text:** {english_text}") st.write(f"**Translated Text ({language_option}):** {translated_text}") st.write(f"**Pronunciation:** {cleaned_pronunciation}") # Save the result in a text file result_text = f"Original English Text: {english_text}\n\nTranslated Text ({language_option}): {translated_text}\nPronunciation: {cleaned_pronunciation}" # Write to a text file with open("translation_result.txt", "w") as file: file.write(result_text) # Create a download button for the user to download the file 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" ) # Step 3: Generate audio for pronunciation progress_text.text("Generating pronunciation audio...") progress_bar.progress(100) # Update progress bar to 100% audio_file_path = generate_audio_from_text(cleaned_pronunciation, LANGUAGE_MAP[language_option]) # Provide a button to play the pronunciation audio st.audio(audio_file_path, format="audio/mp3") translateimg2 = Image.open("v3.png") # Ensure the file is in the correct directory st.image(translateimg2, width=150) # Adjust the size as per preference else: st.error(translated_text) # Display error message if API call fails 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.")