shukdevdatta123 commited on
Commit
0199b96
·
verified ·
1 Parent(s): 10ac60d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -20
app.py CHANGED
@@ -7,9 +7,9 @@ import tempfile
7
  import shutil
8
  import re
9
 
10
- def translate_to_japanese(api_key, text, tone):
11
  """
12
- Translates English text to Japanese using OpenAI's API and provides pronunciation with a specified tone/style.
13
  """
14
  # Validate input
15
  if not api_key:
@@ -20,18 +20,10 @@ def translate_to_japanese(api_key, text, tone):
20
  # Set the OpenAI API key
21
  openai.api_key = api_key
22
 
23
- # Define the messages for the chat model with the selected tone
24
- tone_prompt = ""
25
- if tone == "Formal":
26
- tone_prompt = "Please translate this text into formal Japanese."
27
- elif tone == "Casual":
28
- tone_prompt = "Please translate this text into casual Japanese."
29
- elif tone == "Professional":
30
- tone_prompt = "Please translate this text into professional Japanese."
31
-
32
  messages_translation = [
33
  {"role": "system", "content": "You are a helpful translator."},
34
- {"role": "user", "content": f"{tone_prompt}\n\n{text}"}
35
  ]
36
 
37
  try:
@@ -97,12 +89,6 @@ api_key = os.getenv("OPENAI_API_KEY")
97
  # Input field for the text
98
  english_text = st.text_area("Enter the English text to translate")
99
 
100
- # Dropdown menu for translation tone/style
101
- tone_option = st.selectbox(
102
- "Select Translation Tone/Style",
103
- ["Formal", "Casual", "Professional"]
104
- )
105
-
106
  # Button to trigger the translation
107
  if st.button("Translate"):
108
  if api_key and english_text:
@@ -115,7 +101,7 @@ if st.button("Translate"):
115
  progress_text.text("Translating text...")
116
  progress_bar.progress(33) # Update progress bar to 33%
117
 
118
- japanese_text, pronunciation = translate_to_japanese(api_key, english_text, tone_option)
119
 
120
  # Step 2: Check if translation was successful
121
  if pronunciation:
@@ -167,4 +153,4 @@ if st.button("Translate"):
167
  if not api_key:
168
  st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
169
  else:
170
- st.error("Please provide text to translate.")
 
7
  import shutil
8
  import re
9
 
10
+ def translate_to_japanese(api_key, text):
11
  """
12
+ Translates English text to Japanese using OpenAI's API and provides pronunciation.
13
  """
14
  # Validate input
15
  if not api_key:
 
20
  # Set the OpenAI API key
21
  openai.api_key = api_key
22
 
23
+ # Define the messages for the chat model
 
 
 
 
 
 
 
 
24
  messages_translation = [
25
  {"role": "system", "content": "You are a helpful translator."},
26
+ {"role": "user", "content": f"Translate the following English text to Japanese:\n\n{text}"}
27
  ]
28
 
29
  try:
 
89
  # Input field for the text
90
  english_text = st.text_area("Enter the English text to translate")
91
 
 
 
 
 
 
 
92
  # Button to trigger the translation
93
  if st.button("Translate"):
94
  if api_key and english_text:
 
101
  progress_text.text("Translating text...")
102
  progress_bar.progress(33) # Update progress bar to 33%
103
 
104
+ japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
105
 
106
  # Step 2: Check if translation was successful
107
  if pronunciation:
 
153
  if not api_key:
154
  st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
155
  else:
156
+ st.error("Please provide text to translate.")