shukdevdatta123 commited on
Commit
baae19f
·
verified ·
1 Parent(s): 02006d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -0
app.py CHANGED
@@ -2,6 +2,9 @@ import openai
2
  import os
3
  import streamlit as st
4
  from PIL import Image
 
 
 
5
 
6
  def translate_to_japanese(api_key, text):
7
  """
@@ -57,6 +60,14 @@ def translate_to_japanese(api_key, text):
57
  except Exception as e:
58
  return f"An unexpected error occurred: {str(e)}", None
59
 
 
 
 
 
 
 
 
 
60
  # Streamlit UI
61
  st.title("English to Japanese Translator with Pronunciation")
62
  st.markdown("Translate English text into Japanese and get its pronunciation (Romaji) using OpenAI's API.")
@@ -96,6 +107,12 @@ if st.button("Translate"):
96
  mime="text/plain"
97
  )
98
 
 
 
 
 
 
 
99
  translateimg2 = Image.open("v3.png") # Ensure the file is in the correct directory
100
  st.image(translateimg2, width=150) # Adjust the size as per preference
101
  else:
 
2
  import os
3
  import streamlit as st
4
  from PIL import Image
5
+ from gtts import gTTS
6
+ import tempfile
7
+ import shutil
8
 
9
  def translate_to_japanese(api_key, text):
10
  """
 
60
  except Exception as e:
61
  return f"An unexpected error occurred: {str(e)}", None
62
 
63
+ # Function to generate audio file from text using gTTS
64
+ def generate_audio_from_text(text):
65
+ tts = gTTS(text, lang='ja') # 'ja' for Japanese language
66
+ # Save audio to a temporary file
67
+ temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
68
+ tts.save(temp_audio_file.name)
69
+ return temp_audio_file.name
70
+
71
  # Streamlit UI
72
  st.title("English to Japanese Translator with Pronunciation")
73
  st.markdown("Translate English text into Japanese and get its pronunciation (Romaji) using OpenAI's API.")
 
107
  mime="text/plain"
108
  )
109
 
110
+ # Generate audio for pronunciation
111
+ audio_file_path = generate_audio_from_text(pronunciation)
112
+
113
+ # Provide a button to play the pronunciation audio
114
+ st.audio(audio_file_path, format="audio/mp3")
115
+
116
  translateimg2 = Image.open("v3.png") # Ensure the file is in the correct directory
117
  st.image(translateimg2, width=150) # Adjust the size as per preference
118
  else: