randmimc commited on
Commit
1c7bc6f
·
verified ·
1 Parent(s): 39ca4e0

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +32 -4
src/streamlit_app.py CHANGED
@@ -40,10 +40,26 @@ def display_status_and_action(text):
40
  col1, col2, col3 = st.columns([6, 1, 1]) # 비율 조정
41
 
42
  with col3:
43
- if st.button("🎤", help="음성으로 듣기"):
44
- # gTTS로 mp3 생성
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  text_input = warning +" " +text_input
46
- cleaned_text = re.sub(r'[\x00-\x1F]+', '', text_input).replace("*", "")
47
  tts = gTTS(text=cleaned_text, lang='ko')
48
 
49
  # 메모리에 저장
@@ -52,7 +68,19 @@ def display_status_and_action(text):
52
 
53
  # 오디오 스트림 재설정 후 재생
54
  mp3_fp.seek(0)
55
- st.audio(mp3_fp, format="audio/mp3")
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # 상태 및 조치 출력
58
  display_status_and_action(selected_text)
 
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
  # 메모리에 저장
 
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)