Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import os
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
def translate_to_japanese(api_key, text):
|
| 6 |
+
"""
|
| 7 |
+
Translates English text to Japanese using OpenAI's API and provides pronunciation.
|
| 8 |
+
"""
|
| 9 |
+
# Validate input
|
| 10 |
+
if not api_key:
|
| 11 |
+
return "Error: API key is missing."
|
| 12 |
+
if not text:
|
| 13 |
+
return "Error: Input text is empty."
|
| 14 |
+
|
| 15 |
+
# Set the OpenAI API key
|
| 16 |
+
openai.api_key = api_key
|
| 17 |
+
|
| 18 |
+
# Define the messages for the chat model
|
| 19 |
+
messages_translation = [
|
| 20 |
+
{"role": "system", "content": "You are a helpful translator."},
|
| 21 |
+
{"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"}
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Call the OpenAI API to get the Japanese translation
|
| 26 |
+
response_translation = openai.ChatCompletion.create(
|
| 27 |
+
model="gpt-3.5-turbo", # Use the correct endpoint for chat models
|
| 28 |
+
messages=messages_translation,
|
| 29 |
+
max_tokens=150,
|
| 30 |
+
temperature=0.5
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Extract the Japanese translation from the response
|
| 34 |
+
japanese_translation = response_translation.choices[0].message['content'].strip()
|
| 35 |
+
|
| 36 |
+
# Define the messages for the pronunciation (Romaji) request
|
| 37 |
+
messages_pronunciation = [
|
| 38 |
+
{"role": "system", "content": "You are a helpful assistant who provides the Romaji (Japanese pronunciation in Latin script) of Japanese text."},
|
| 39 |
+
{"role": "user", "content": f"Provide the Romaji pronunciation for the following Japanese text:\n\n{japanese_translation}"}
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
# Call the OpenAI API to get the pronunciation
|
| 43 |
+
response_pronunciation = openai.ChatCompletion.create(
|
| 44 |
+
model="gpt-3.5-turbo",
|
| 45 |
+
messages=messages_pronunciation,
|
| 46 |
+
max_tokens=150,
|
| 47 |
+
temperature=0.5
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Extract the pronunciation (Romaji) from the response
|
| 51 |
+
pronunciation = response_pronunciation.choices[0].message['content'].strip()
|
| 52 |
+
return japanese_translation, pronunciation
|
| 53 |
+
|
| 54 |
+
except openai.error.OpenAIError as e:
|
| 55 |
+
return f"OpenAI API error: {str(e)}", None
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"An unexpected error occurred: {str(e)}", None
|
| 58 |
+
|
| 59 |
+
# Streamlit UI
|
| 60 |
+
st.title("English to Japanese Translator with Pronunciation")
|
| 61 |
+
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji) using OpenAI's API.")
|
| 62 |
+
|
| 63 |
+
# Access the API key from Hugging Face Secrets
|
| 64 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 65 |
+
|
| 66 |
+
# Input field for the text
|
| 67 |
+
english_text = st.text_area("Enter the English text to translate")
|
| 68 |
+
|
| 69 |
+
# Button to trigger the translation
|
| 70 |
+
if st.button("Translate"):
|
| 71 |
+
if api_key and english_text:
|
| 72 |
+
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
| 73 |
+
if pronunciation:
|
| 74 |
+
st.markdown("### Translation Result:")
|
| 75 |
+
st.write(f"**Japanese Output:** {japanese_text}")
|
| 76 |
+
st.write(f"**Pronunciation:** {pronunciation}")
|
| 77 |
+
else:
|
| 78 |
+
st.error(japanese_text) # Display error message if API call fails
|
| 79 |
+
else:
|
| 80 |
+
if not api_key:
|
| 81 |
+
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
| 82 |
+
else:
|
| 83 |
+
st.error("Please provide text to translate.")
|