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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +24 -46
src/streamlit_app.py CHANGED
@@ -37,53 +37,31 @@ def display_status_and_action(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)
 
 
37
  st.warning(warning)
38
  st.markdown(text_input)
39
  # ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์„ ์œ„ํ•œ ์ปฌ๋Ÿผ ์„ค์ •
40
+ col1, col2, col3 = st.columns([6, 1, 1])
 
 
 
 
 
 
 
 
41
 
42
+ with col3:
43
+ if st.button("๐ŸŽค", help="์Œ์„ฑ์œผ๋กœ ๋“ฃ๊ธฐ"):
44
+ text_input = warning + " " + text_input
45
+ cleaned_text = re.sub(r'[\x00-\x1F]+', '', text_input).replace("*", "")
46
+ tts = gTTS(text=cleaned_text, lang='ko')
47
+ mp3_fp = BytesIO()
48
+ tts.write_to_fp(mp3_fp)
49
+ mp3_fp.seek(0)
50
+
51
+ # base64 ์ธ์ฝ”๋”ฉ
52
+ import base64
53
+ audio_bytes = mp3_fp.read()
54
+ b64_audio = base64.b64encode(audio_bytes).decode()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ # HTML ์˜ค๋””์˜ค ํ”Œ๋ ˆ์ด์–ด (์ „์ฒด ๋„ˆ๋น„)
57
+ audio_html = f"""
58
+ <audio controls style="width: 100%;">
59
+ <source src="data:audio/mp3;base64,{b64_audio}" type="audio/mp3">
60
+ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์˜ค๋””์˜ค๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
61
+ </audio>
62
+ """
63
+ st.markdown(audio_html, unsafe_allow_html=True)
64
+
65
  # ์ƒํƒœ ๋ฐ ์กฐ์น˜ ์ถœ๋ ฅ
66
  display_status_and_action(selected_text)
67
+