Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
import streamlit.components.v1 as components
|
|
|
4 |
from gtts import gTTS
|
5 |
from gtts.lang import tts_langs
|
6 |
from io import BytesIO
|
@@ -39,41 +40,38 @@ else:
|
|
39 |
st.session_state['current_sub_menu'] = ''
|
40 |
|
41 |
|
|
|
|
|
|
|
|
|
42 |
# 'Sound' λ©λ΄μ 'TTS(Voice)' μ ν μ
|
43 |
if selected_menu == "Sound" and selected_sub_menu == "TTS(Voice)":
|
44 |
-
st.header("Text-to-Speech")
|
|
|
45 |
# ν
μ€νΈ μ
λ ₯
|
46 |
-
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
# ISO 639-1 νμ€μ λ°λΌ λ κΈμ μ½λλ₯Ό κ°μ§ μΈμ΄λ§ νν°λ§
|
51 |
-
two_letter_languages = {code: lang for code, lang in languages_dict.items() if len(code) == 2}
|
52 |
-
|
53 |
-
# μΈμ΄ μ νμ μν selectboxλ₯Ό μμ±ν©λλ€.
|
54 |
-
selected_language_code = st.selectbox(
|
55 |
-
"Choose Language",
|
56 |
-
options=list(two_letter_languages.keys()),
|
57 |
-
format_func=lambda x: f"{two_letter_languages[x]} ({x})",
|
58 |
-
index=list(two_letter_languages.keys()).index('en') # 'en'μ κΈ°λ³Έ μΈμ΄λ‘ μ€μ
|
59 |
-
)
|
60 |
|
61 |
-
#
|
62 |
-
if st.button("
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
|
79 |
# Pexels API ν€ μ€μ
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
import streamlit.components.v1 as components
|
4 |
+
from googletrans import Translator, LANGUAGES
|
5 |
from gtts import gTTS
|
6 |
from gtts.lang import tts_langs
|
7 |
from io import BytesIO
|
|
|
40 |
st.session_state['current_sub_menu'] = ''
|
41 |
|
42 |
|
43 |
+
|
44 |
+
|
45 |
+
translator = Translator()
|
46 |
+
|
47 |
# 'Sound' λ©λ΄μ 'TTS(Voice)' μ ν μ
|
48 |
if selected_menu == "Sound" and selected_sub_menu == "TTS(Voice)":
|
49 |
+
st.header("Text-to-Speech with Translation")
|
50 |
+
|
51 |
# ν
μ€νΈ μ
λ ₯
|
52 |
+
text_to_translate = st.text_area("Enter text to translate and synthesize", "Hello, welcome to ViDraft TTS service.")
|
53 |
|
54 |
+
# νκ² μΈμ΄ μ ν
|
55 |
+
target_language = st.selectbox("Choose Target Language for Translation", options=list(LANGUAGES.values()), index=list(LANGUAGES.values()).index('English'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# λ²μ λ²νΌ
|
58 |
+
if st.button("Translate"):
|
59 |
+
# λ²μ μ€ν
|
60 |
+
translation = translator.translate(text_to_translate, dest=list(LANGUAGES.keys())[list(LANGUAGES.values()).index(target_language)])
|
61 |
+
st.text_area("Translated Text", translation.text, height=100)
|
62 |
+
|
63 |
+
# μμ± μμ± λ²νΌ
|
64 |
+
if st.button("Synthesize Voice"):
|
65 |
+
try:
|
66 |
+
# λ²μλ ν
μ€νΈλ₯Ό μμ±μΌλ‘ λ³ν
|
67 |
+
tts = gTTS(text=translation.text, lang=list(LANGUAGES.keys())[list(LANGUAGES.values()).index(target_language)], slow=False)
|
68 |
+
audio_file = BytesIO()
|
69 |
+
tts.write_to_fp(audio_file)
|
70 |
+
audio_file.seek(0)
|
71 |
+
# μμ±λ μ€λμ€ νμΌμ μ¬μ
|
72 |
+
st.audio(audio_file, format="audio/mp3")
|
73 |
+
except Exception as e:
|
74 |
+
st.error(f"Error: {e}")
|
75 |
|
76 |
|
77 |
# Pexels API ν€ μ€μ
|