Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from gtts import gTTS
|
|
6 |
import tempfile
|
7 |
import shutil
|
8 |
import re
|
|
|
9 |
|
10 |
def translate_to_japanese(api_key, text):
|
11 |
"""
|
@@ -29,7 +30,7 @@ def translate_to_japanese(api_key, text):
|
|
29 |
try:
|
30 |
# Call the OpenAI API to get the Japanese translation
|
31 |
response_translation = openai.ChatCompletion.create(
|
32 |
-
model="gpt-
|
33 |
messages=messages_translation,
|
34 |
max_tokens=300,
|
35 |
temperature=0.5
|
@@ -46,7 +47,7 @@ def translate_to_japanese(api_key, text):
|
|
46 |
|
47 |
# Call the OpenAI API to get the pronunciation
|
48 |
response_pronunciation = openai.ChatCompletion.create(
|
49 |
-
model="gpt-
|
50 |
messages=messages_pronunciation,
|
51 |
max_tokens=300,
|
52 |
temperature=0.5
|
@@ -76,12 +77,32 @@ def generate_audio_from_text(text):
|
|
76 |
tts.save(temp_audio_file.name)
|
77 |
return temp_audio_file.name
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Streamlit UI
|
80 |
st.title("English to Japanese Translator with Pronunciation")
|
81 |
-
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji)
|
82 |
|
|
|
83 |
translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory
|
84 |
-
st.image(translateimg, use_container_width=True)
|
85 |
|
86 |
# Access the API key from Hugging Face Secrets
|
87 |
api_key = os.getenv("OPENAI_API_KEY")
|
@@ -89,19 +110,17 @@ api_key = os.getenv("OPENAI_API_KEY")
|
|
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:
|
95 |
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
96 |
if pronunciation:
|
97 |
-
# Clean pronunciation (remove unnecessary parts)
|
98 |
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
99 |
-
|
100 |
st.markdown("### Translation Result:")
|
101 |
st.write(f"**English Text:** {english_text}")
|
102 |
st.write(f"**Japanese Output:** {japanese_text}")
|
103 |
st.write(f"**Pronunciation:** {cleaned_pronunciation}")
|
104 |
-
|
105 |
# Save the result in a text file
|
106 |
result_text = f"English Text: {english_text}\n\nJapanese Translation: {japanese_text}\nPronunciation: {cleaned_pronunciation}"
|
107 |
|
@@ -120,12 +139,7 @@ if st.button("Translate"):
|
|
120 |
|
121 |
# Generate audio for pronunciation
|
122 |
audio_file_path = generate_audio_from_text(cleaned_pronunciation)
|
123 |
-
|
124 |
-
# Provide a button to play the pronunciation audio
|
125 |
st.audio(audio_file_path, format="audio/mp3")
|
126 |
-
|
127 |
-
translateimg2 = Image.open("v3.png") # Ensure the file is in the correct directory
|
128 |
-
st.image(translateimg2, width=150) # Adjust the size as per preference
|
129 |
else:
|
130 |
st.error(japanese_text) # Display error message if API call fails
|
131 |
else:
|
@@ -133,3 +147,26 @@ if st.button("Translate"):
|
|
133 |
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
134 |
else:
|
135 |
st.error("Please provide text to translate.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import tempfile
|
7 |
import shutil
|
8 |
import re
|
9 |
+
import speech_recognition as sr # Library for voice recognition
|
10 |
|
11 |
def translate_to_japanese(api_key, text):
|
12 |
"""
|
|
|
30 |
try:
|
31 |
# Call the OpenAI API to get the Japanese translation
|
32 |
response_translation = openai.ChatCompletion.create(
|
33 |
+
model="gpt-4", # Ensure using the correct endpoint for the GPT model
|
34 |
messages=messages_translation,
|
35 |
max_tokens=300,
|
36 |
temperature=0.5
|
|
|
47 |
|
48 |
# Call the OpenAI API to get the pronunciation
|
49 |
response_pronunciation = openai.ChatCompletion.create(
|
50 |
+
model="gpt-4",
|
51 |
messages=messages_pronunciation,
|
52 |
max_tokens=300,
|
53 |
temperature=0.5
|
|
|
77 |
tts.save(temp_audio_file.name)
|
78 |
return temp_audio_file.name
|
79 |
|
80 |
+
# Function to capture and transcribe voice input
|
81 |
+
def transcribe_voice_input():
|
82 |
+
recognizer = sr.Recognizer()
|
83 |
+
with sr.Microphone() as source:
|
84 |
+
st.info("Please speak now...")
|
85 |
+
recognizer.adjust_for_ambient_noise(source)
|
86 |
+
audio = recognizer.listen(source)
|
87 |
+
try:
|
88 |
+
# Use Google Web Speech API to transcribe audio
|
89 |
+
text = recognizer.recognize_google(audio)
|
90 |
+
st.success(f"Transcribed text: {text}")
|
91 |
+
return text
|
92 |
+
except sr.UnknownValueError:
|
93 |
+
st.error("Sorry, I could not understand the audio. Please try again.")
|
94 |
+
return None
|
95 |
+
except sr.RequestError:
|
96 |
+
st.error("Could not request results from Google Speech Recognition service.")
|
97 |
+
return None
|
98 |
+
|
99 |
# Streamlit UI
|
100 |
st.title("English to Japanese Translator with Pronunciation")
|
101 |
+
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji). You can input text or use voice.")
|
102 |
|
103 |
+
# Display an image if you have one
|
104 |
translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory
|
105 |
+
st.image(translateimg, use_container_width=True)
|
106 |
|
107 |
# Access the API key from Hugging Face Secrets
|
108 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
110 |
# Input field for the text
|
111 |
english_text = st.text_area("Enter the English text to translate")
|
112 |
|
113 |
+
# Button to trigger the translation from text input
|
114 |
+
if st.button("Translate from Text"):
|
115 |
if api_key and english_text:
|
116 |
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
117 |
if pronunciation:
|
|
|
118 |
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
|
|
119 |
st.markdown("### Translation Result:")
|
120 |
st.write(f"**English Text:** {english_text}")
|
121 |
st.write(f"**Japanese Output:** {japanese_text}")
|
122 |
st.write(f"**Pronunciation:** {cleaned_pronunciation}")
|
123 |
+
|
124 |
# Save the result in a text file
|
125 |
result_text = f"English Text: {english_text}\n\nJapanese Translation: {japanese_text}\nPronunciation: {cleaned_pronunciation}"
|
126 |
|
|
|
139 |
|
140 |
# Generate audio for pronunciation
|
141 |
audio_file_path = generate_audio_from_text(cleaned_pronunciation)
|
|
|
|
|
142 |
st.audio(audio_file_path, format="audio/mp3")
|
|
|
|
|
|
|
143 |
else:
|
144 |
st.error(japanese_text) # Display error message if API call fails
|
145 |
else:
|
|
|
147 |
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
148 |
else:
|
149 |
st.error("Please provide text to translate.")
|
150 |
+
|
151 |
+
# Button to trigger translation from voice input
|
152 |
+
if st.button("Translate from Voice"):
|
153 |
+
if api_key:
|
154 |
+
voice_input = transcribe_voice_input()
|
155 |
+
if voice_input:
|
156 |
+
japanese_text, pronunciation = translate_to_japanese(api_key, voice_input)
|
157 |
+
if pronunciation:
|
158 |
+
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
159 |
+
st.markdown("### Translation Result from Voice:")
|
160 |
+
st.write(f"**English Text (from Voice):** {voice_input}")
|
161 |
+
st.write(f"**Japanese Output:** {japanese_text}")
|
162 |
+
st.write(f"**Pronunciation:** {cleaned_pronunciation}")
|
163 |
+
|
164 |
+
# Generate audio for pronunciation
|
165 |
+
audio_file_path = generate_audio_from_text(cleaned_pronunciation)
|
166 |
+
st.audio(audio_file_path, format="audio/mp3")
|
167 |
+
else:
|
168 |
+
st.error(japanese_text) # Display error message if API call fails
|
169 |
+
else:
|
170 |
+
st.error("Could not transcribe voice input.")
|
171 |
+
else:
|
172 |
+
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|