Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
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,37 +39,41 @@ else:
|
|
40 |
st.session_state['current_sub_menu'] = ''
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
translator = Translator()
|
45 |
-
|
46 |
-
if 'translated_text' not in st.session_state:
|
47 |
-
st.session_state['translated_text'] = ""
|
48 |
-
|
49 |
-
# ๋ฒ์ญํ ํ
์คํธ ์
๋ ฅ
|
50 |
-
text_to_translate = st.text_area("Enter text to translate and synthesize", "")
|
51 |
-
|
52 |
-
# ํ๊ฒ ์ธ์ด ์ ํ
|
53 |
-
target_language = st.selectbox("Select target language for translation", list(LANGUAGES.values()))
|
54 |
-
|
55 |
-
# ๋ฒ์ญ ๋ฒํผ
|
56 |
-
if st.button("Translate"):
|
57 |
-
translation = translator.translate(text_to_translate, dest=target_language)
|
58 |
-
st.session_state['translated_text'] = translation.text
|
59 |
-
st.write(f"Translated Text: {translation.text}")
|
60 |
-
|
61 |
-
# TTS ์์ฑ ๋ฒํผ
|
62 |
-
if st.button("Synthesize Speech"):
|
63 |
-
if st.session_state['translated_text']:
|
64 |
-
try:
|
65 |
-
tts = gTTS(text=st.session_state['translated_text'], lang='en', slow=False) # ์์์์๋ 'en'์ ์ฌ์ฉ
|
66 |
-
audio_file = BytesIO()
|
67 |
-
tts.write_to_fp(audio_file)
|
68 |
-
audio_file.seek(0)
|
69 |
-
st.audio(audio_file, format="audio/mp3")
|
70 |
-
except Exception as e:
|
71 |
-
st.error(f"Error: {e}")
|
72 |
-
else:
|
73 |
-
st.warning("Please translate some text first.")
|
74 |
|
75 |
|
76 |
# Pexels API ํค ์ค์
|
|
|
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 |
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 |
+
text = st.text_area("Enter text to synthesize", "Hello, welcome to ViDraft TTS service.")
|
47 |
+
|
48 |
+
# ์ง์๋๋ ์ธ์ด ๋ชฉ๋ก์ ๋ถ๋ฌ์ต๋๋ค.
|
49 |
+
languages_dict = tts_langs()
|
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 |
+
# 'Synthesize' ๋ฒํผ
|
62 |
+
if st.button("Synthesize"):
|
63 |
+
if text:
|
64 |
+
try:
|
65 |
+
# ์ ํ๋ ์ธ์ด๋ก gTTS ๊ฐ์ฒด ์์ฑ
|
66 |
+
tts = gTTS(text=text, lang=selected_language_code, slow=False)
|
67 |
+
audio_file = BytesIO()
|
68 |
+
tts.write_to_fp(audio_file)
|
69 |
+
audio_file.seek(0)
|
70 |
+
# ์์ฑ๋ ์ค๋์ค ํ์ผ์ ์ฌ์
|
71 |
+
st.audio(audio_file, format="audio/mp3")
|
72 |
+
except Exception as e:
|
73 |
+
st.error(f"Error: {e}")
|
74 |
+
else:
|
75 |
+
st.warning("Please enter some text to synthesize.")
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
|
79 |
# Pexels API ํค ์ค์
|