randmimc commited on
Commit
e1fcc4f
ยท
verified ยท
1 Parent(s): 1c7bc6f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +45 -42
src/streamlit_app.py CHANGED
@@ -37,50 +37,53 @@ def display_status_and_action(text):
37
  st.warning(warning)
38
  st.markdown(text_input)
39
  # ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์„ ์œ„ํ•œ ์ปฌ๋Ÿผ ์„ค์ •
40
- col1, col2, col3 = st.columns([6, 1, 1]) # ๋น„์œจ ์กฐ์ •
 
 
 
 
 
 
 
 
41
 
42
- with col3:
43
- # ์‚ฌ์šฉ์ž ์ธํ„ฐ๋ž™์…˜ ๊ฐ์ง€๋ฅผ ์œ„ํ•œ ํผ ๊ตฌ์„ฑ
44
- with st.form(key="voice_form"):
45
- button_html = """
46
- <style>
47
- .icon-button {
48
- background-color: transparent;
49
- border: none;
50
- cursor: pointer;
51
- font-size: 24px;
52
- }
53
- </style>
54
- <button class="icon-button" type="submit">๐ŸŽค</button>
55
- """
56
- st.markdown(button_html, unsafe_allow_html=True)
57
- submitted = st.form_submit_button("๐ŸŽค (invisible)", use_container_width=True) # ๋‚ด๋ถ€์ ์œผ๋กœ submit๋งŒ ๊ฐ์ง€์šฉ
58
-
59
- if submitted:
60
- # ํ…์ŠคํŠธ to ์Œ์„ฑ ์ฒ˜๋ฆฌ
61
- text_input = warning +" " +text_input
62
- cleaned_text = re.sub(r'[\x00-\x1F]+', '', text_input).replace("*", "").replace("โš ๏ธ", "")
63
- tts = gTTS(text=cleaned_text, lang='ko')
64
-
65
- # ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ
66
- mp3_fp = BytesIO()
67
- tts.write_to_fp(mp3_fp)
68
-
69
- # ์˜ค๋””์˜ค ์ŠคํŠธ๋ฆผ ์žฌ์„ค์ • ํ›„ ์žฌ์ƒ
70
- mp3_fp.seek(0)
71
- # base64 ์ธ์ฝ”๋”ฉ
72
- import base64
73
- audio_bytes = mp3_fp.read()
74
- b64_audio = base64.b64encode(audio_bytes).decode()
75
 
76
- # HTML ์˜ค๋””์˜ค ํ”Œ๋ ˆ์ด์–ด (์ „์ฒด ๋„ˆ๋น„)
77
- audio_html = f"""
78
- <audio controls style="width: 100%;">
79
- <source src="data:audio/mp3;base64,{b64_audio}" type="audio/mp3">
80
- ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์˜ค๋””์˜ค๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
81
- </audio>
82
- """
83
- st.markdown(audio_html, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  # ์ƒํƒœ ๋ฐ ์กฐ์น˜ ์ถœ๋ ฅ
86
  display_status_and_action(selected_text)
 
37
  st.warning(warning)
38
  st.markdown(text_input)
39
  # ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์„ ์œ„ํ•œ ์ปฌ๋Ÿผ ์„ค์ •
40
+ def render_voice_button_and_play_audio(tts_text):
41
+ # ํ…์ŠคํŠธ๋ฅผ ์Œ์„ฑ์œผ๋กœ ๋ณ€ํ™˜
42
+ cleaned_text = re.sub(r'[\x00-\x1F]+', '', tts_text).replace("*", "")
43
+ tts = gTTS(text=cleaned_text, lang='ko')
44
+ mp3_fp = BytesIO()
45
+ tts.write_to_fp(mp3_fp)
46
+ mp3_fp.seek(0)
47
+ audio_bytes = mp3_fp.read()
48
+ b64_audio = base64.b64encode(audio_bytes).decode()
49
 
50
+ # HTML ์˜ค๋””์˜ค ํ”Œ๋ ˆ์ด์–ด ์ „์ฒด ํญ์œผ๋กœ ์‚ฝ์ž…
51
+ audio_html = f"""
52
+ <audio controls style="width: 100%;">
53
+ <source src="data:audio/mp3;base64,{b64_audio}" type="audio/mp3">
54
+ </audio>
55
+ """
56
+ st.markdown(audio_html, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ # ๋ฒ„ํŠผ ๋ Œ๋”๋ง
59
+ col1, col2, col3 = st.columns([6, 1, 1])
60
+ with col3:
61
+ button_html = """
62
+ <style>
63
+ .icon-button {
64
+ background-color: transparent;
65
+ border: none;
66
+ cursor: pointer;
67
+ font-size: 28px;
68
+ padding: 0;
69
+ margin: 0;
70
+ }
71
+ </style>
72
+ <form action="#" method="post">
73
+ <button class="icon-button" name="play_audio" type="submit">๐ŸŽค</button>
74
+ </form>
75
+ """
76
+ st.markdown(button_html, unsafe_allow_html=True)
77
+
78
+ # ๋ฒ„ํŠผ์ด ํด๋ฆญ๋๋Š”์ง€ ๊ฐ์ง€ (์„ธ์…˜ ์ƒํƒœ๋กœ)
79
+ if st.session_state.get("audio_played") is None:
80
+ st.session_state.audio_played = False
81
+
82
+ # ๋ฒ„ํŠผ ํด๋ฆญ์„ ๊ฐ์ง€ (Streamlit์€ form submit ๊ฐ์ง€๋ฅผ ๋ชปํ•˜๋ฏ€๋กœ ์šฐํšŒ ๋ฐฉ๋ฒ• ํ•„์š” ์‹œ JS ์ด์šฉ)
83
+ # ์ž„์‹œ๋กœ ์•„๋ž˜์—์„œ ์กฐ๊ฑด์ ์œผ๋กœ ์žฌ์ƒ๋งŒ ํ—ˆ์šฉ
84
+ if st.query_params.get("play") == "true" or st.session_state.audio_played:
85
+ render_voice_button_and_play_audio(warning + " " + text_input)
86
+ st.session_state.audio_played = False
87
 
88
  # ์ƒํƒœ ๋ฐ ์กฐ์น˜ ์ถœ๋ ฅ
89
  display_status_and_action(selected_text)