Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,6 @@ from gtts import gTTS
|
|
6 |
import tempfile
|
7 |
import shutil
|
8 |
import re
|
9 |
-
import speech_recognition as sr # Library for voice recognition
|
10 |
-
import sounddevice as sd
|
11 |
|
12 |
def translate_to_japanese(api_key, text):
|
13 |
"""
|
@@ -31,7 +29,7 @@ def translate_to_japanese(api_key, text):
|
|
31 |
try:
|
32 |
# Call the OpenAI API to get the Japanese translation
|
33 |
response_translation = openai.ChatCompletion.create(
|
34 |
-
model="gpt-
|
35 |
messages=messages_translation,
|
36 |
max_tokens=300,
|
37 |
temperature=0.5
|
@@ -48,7 +46,7 @@ def translate_to_japanese(api_key, text):
|
|
48 |
|
49 |
# Call the OpenAI API to get the pronunciation
|
50 |
response_pronunciation = openai.ChatCompletion.create(
|
51 |
-
model="gpt-
|
52 |
messages=messages_pronunciation,
|
53 |
max_tokens=300,
|
54 |
temperature=0.5
|
@@ -78,39 +76,12 @@ def generate_audio_from_text(text):
|
|
78 |
tts.save(temp_audio_file.name)
|
79 |
return temp_audio_file.name
|
80 |
|
81 |
-
def transcribe_voice_input():
|
82 |
-
recognizer = sr.Recognizer()
|
83 |
-
|
84 |
-
# Set parameters for sounddevice (e.g., 44100 Hz, 1 channel)
|
85 |
-
duration = 5 # Seconds to record
|
86 |
-
fs = 44100 # Sampling frequency
|
87 |
-
|
88 |
-
st.info("Please speak now...")
|
89 |
-
|
90 |
-
# Record the audio using sounddevice
|
91 |
-
audio_data = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
|
92 |
-
sd.wait() # Wait until recording is finished
|
93 |
-
|
94 |
-
# Convert the numpy array to audio and recognize the speech
|
95 |
-
with sr.AudioData(audio_data.tobytes(), fs, 2) as source:
|
96 |
-
try:
|
97 |
-
text = recognizer.recognize_google(source)
|
98 |
-
st.success(f"Transcribed text: {text}")
|
99 |
-
return text
|
100 |
-
except sr.UnknownValueError:
|
101 |
-
st.error("Sorry, I could not understand the audio. Please try again.")
|
102 |
-
return None
|
103 |
-
except sr.RequestError:
|
104 |
-
st.error("Could not request results from Google Speech Recognition service.")
|
105 |
-
return None
|
106 |
-
|
107 |
# Streamlit UI
|
108 |
st.title("English to Japanese Translator with Pronunciation")
|
109 |
-
st.markdown("Translate English text into Japanese and get its pronunciation (Romaji)
|
110 |
|
111 |
-
# Display an image if you have one
|
112 |
translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory
|
113 |
-
st.image(translateimg, use_container_width=True)
|
114 |
|
115 |
# Access the API key from Hugging Face Secrets
|
116 |
api_key = os.getenv("OPENAI_API_KEY")
|
@@ -118,17 +89,19 @@ api_key = os.getenv("OPENAI_API_KEY")
|
|
118 |
# Input field for the text
|
119 |
english_text = st.text_area("Enter the English text to translate")
|
120 |
|
121 |
-
# Button to trigger the translation
|
122 |
-
if st.button("Translate
|
123 |
if api_key and english_text:
|
124 |
japanese_text, pronunciation = translate_to_japanese(api_key, english_text)
|
125 |
if pronunciation:
|
|
|
126 |
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
|
|
127 |
st.markdown("### Translation Result:")
|
128 |
st.write(f"**English Text:** {english_text}")
|
129 |
st.write(f"**Japanese Output:** {japanese_text}")
|
130 |
st.write(f"**Pronunciation:** {cleaned_pronunciation}")
|
131 |
-
|
132 |
# Save the result in a text file
|
133 |
result_text = f"English Text: {english_text}\n\nJapanese Translation: {japanese_text}\nPronunciation: {cleaned_pronunciation}"
|
134 |
|
@@ -147,34 +120,16 @@ if st.button("Translate from Text"):
|
|
147 |
|
148 |
# Generate audio for pronunciation
|
149 |
audio_file_path = generate_audio_from_text(cleaned_pronunciation)
|
|
|
|
|
150 |
st.audio(audio_file_path, format="audio/mp3")
|
|
|
|
|
|
|
151 |
else:
|
152 |
st.error(japanese_text) # Display error message if API call fails
|
153 |
else:
|
154 |
if not api_key:
|
155 |
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
156 |
else:
|
157 |
-
st.error("Please provide text to translate.")
|
158 |
-
|
159 |
-
# Button to trigger translation from voice input
|
160 |
-
if st.button("Translate from Voice"):
|
161 |
-
if api_key:
|
162 |
-
voice_input = transcribe_voice_input()
|
163 |
-
if voice_input:
|
164 |
-
japanese_text, pronunciation = translate_to_japanese(api_key, voice_input)
|
165 |
-
if pronunciation:
|
166 |
-
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
167 |
-
st.markdown("### Translation Result from Voice:")
|
168 |
-
st.write(f"**English Text (from Voice):** {voice_input}")
|
169 |
-
st.write(f"**Japanese Output:** {japanese_text}")
|
170 |
-
st.write(f"**Pronunciation:** {cleaned_pronunciation}")
|
171 |
-
|
172 |
-
# Generate audio for pronunciation
|
173 |
-
audio_file_path = generate_audio_from_text(cleaned_pronunciation)
|
174 |
-
st.audio(audio_file_path, format="audio/mp3")
|
175 |
-
else:
|
176 |
-
st.error(japanese_text) # Display error message if API call fails
|
177 |
-
else:
|
178 |
-
st.error("Could not transcribe voice input.")
|
179 |
-
else:
|
180 |
-
st.error("API key is missing. Please add it as a secret in Hugging Face Settings.")
|
|
|
6 |
import tempfile
|
7 |
import shutil
|
8 |
import re
|
|
|
|
|
9 |
|
10 |
def translate_to_japanese(api_key, text):
|
11 |
"""
|
|
|
29 |
try:
|
30 |
# Call the OpenAI API to get the Japanese translation
|
31 |
response_translation = openai.ChatCompletion.create(
|
32 |
+
model="gpt-4o", # Use the correct endpoint for chat models
|
33 |
messages=messages_translation,
|
34 |
max_tokens=300,
|
35 |
temperature=0.5
|
|
|
46 |
|
47 |
# Call the OpenAI API to get the pronunciation
|
48 |
response_pronunciation = openai.ChatCompletion.create(
|
49 |
+
model="gpt-4o",
|
50 |
messages=messages_pronunciation,
|
51 |
max_tokens=300,
|
52 |
temperature=0.5
|
|
|
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) using OpenAI's API.")
|
82 |
|
|
|
83 |
translateimg = Image.open("Untitled.png") # Ensure the file is in the correct directory
|
84 |
+
st.image(translateimg, use_container_width=True) # Adjust the size as per preference
|
85 |
|
86 |
# Access the API key from Hugging Face Secrets
|
87 |
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 |
|
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:
|
132 |
if not api_key:
|
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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|