seawolf2357 commited on
Commit
bb92fc2
β€’
1 Parent(s): 60d5502

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -30
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
- 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 ν‚€ μ„€μ •
 
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 ν‚€ μ„€μ •