Spaces:
Paused
Paused
| import openai | |
| import streamlit as st | |
| def translate_to_japanese(api_key, text): | |
| """ | |
| Translates English text to Japanese using OpenAI's API and provides pronunciation. | |
| """ | |
| # Validate input | |
| if not api_key: | |
| return "Error: API key is missing." | |
| if not text: | |
| return "Error: Input text is empty." | |
| # 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 Japanese:\n\n{text}"} | |
| ] | |
| try: | |
| # Call the OpenAI API to get the Japanese translation | |
| response_translation = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", # Use the correct endpoint for chat models | |
| messages=messages_translation, | |
| max_tokens=150, | |
| temperature=0.5 | |
| ) | |
| # Extract the Japanese translation from the response | |
| japanese_translation = response_translation.choices[0].message['content'].strip() | |
| # Define the messages for the pronunciation (Romaji) request | |
| messages_pronunciation = [ | |
| {"role": "system", "content": "You are a helpful assistant who provides the Romaji (Japanese pronunciation in Latin script) of Japanese text."}, | |
| {"role": "user", "content": f"Provide the Romaji pronunciation for the following Japanese text:\n\n{japanese_translation}"} | |
| ] | |
| # Call the OpenAI API to get the pronunciation | |
| response_pronunciation = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=messages_pronunciation, | |
| max_tokens=150, | |
| temperature=0.5 | |
| ) | |
| # Extract the pronunciation (Romaji) from the response | |
| pronunciation = response_pronunciation.choices[0].message['content'].strip() | |
| return japanese_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 | |
| # Streamlit UI | |
| st.title("English to Japanese Translator with Pronunciation") | |
| st.markdown("Translate English text into Japanese and get its pronunciation (Romaji) using OpenAI's API.") | |
| # Input fields for the API key and text | |
| api_key = st.text_input("Enter your OpenAI API key", type="password") | |
| english_text = st.text_area("Enter the English text to translate") | |
| # Button to trigger the translation | |
| if st.button("Translate"): | |
| if api_key and english_text: | |
| japanese_text, pronunciation = translate_to_japanese(api_key, english_text) | |
| if pronunciation: | |
| st.markdown("### Translation Result:") | |
| st.write(f"**Japanese Output:** {japanese_text}") | |
| st.write(f"**Pronunciation:** {pronunciation}") | |
| else: | |
| st.error(japanese_text) # Display error message if API call fails | |
| else: | |
| st.error("Please provide both an API key and text to translate.") | |