Spaces:
Running
Running
FauziIsyrinApridal
commited on
Commit
·
9de931e
1
Parent(s):
6cc3b6e
tambahkan putar ulang
Browse files- app/chat.py +53 -18
app/chat.py
CHANGED
@@ -21,18 +21,34 @@ def initialize_session_state():
|
|
21 |
st.session_state['should_speak'] = True
|
22 |
if 'input_text' not in st.session_state:
|
23 |
st.session_state['input_text'] = ""
|
24 |
-
if '
|
25 |
-
st.session_state['
|
|
|
|
|
26 |
|
27 |
-
# Fungsi TTS
|
28 |
-
def text_to_speech(text):
|
|
|
|
|
|
|
|
|
29 |
tts = gtts.gTTS(text, lang="id")
|
30 |
audio_bytes = BytesIO()
|
31 |
tts.write_to_fp(audio_bytes)
|
32 |
audio_bytes.seek(0)
|
33 |
audio_base64 = base64.b64encode(audio_bytes.read()).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
audio_player = f"""
|
35 |
-
<audio
|
36 |
<source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
|
37 |
</audio>
|
38 |
"""
|
@@ -92,23 +108,42 @@ def display_chat_history(chain):
|
|
92 |
st.session_state['generated'].append(output)
|
93 |
st.session_state.input_text = ""
|
94 |
|
95 |
-
#
|
96 |
if st.session_state['should_speak'] and output:
|
97 |
-
st.session_state['
|
98 |
-
|
99 |
-
|
100 |
|
101 |
-
# Tampilkan riwayat chat
|
102 |
if st.session_state['generated']:
|
103 |
with reply_container:
|
104 |
for i in range(len(st.session_state['generated'])):
|
|
|
105 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="no-avatar")
|
|
|
|
|
106 |
message(st.session_state["generated"][i], key=str(i), avatar_style="no-avatar")
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
st.session_state['should_speak'] = True
|
22 |
if 'input_text' not in st.session_state:
|
23 |
st.session_state['input_text'] = ""
|
24 |
+
if 'audio_cache' not in st.session_state:
|
25 |
+
st.session_state['audio_cache'] = {}
|
26 |
+
if 'play_audio' not in st.session_state:
|
27 |
+
st.session_state['play_audio'] = None
|
28 |
|
29 |
+
# Fungsi TTS dengan cache
|
30 |
+
def text_to_speech(text, message_id=None):
|
31 |
+
# Cek cache terlebih dahulu
|
32 |
+
if message_id and message_id in st.session_state['audio_cache']:
|
33 |
+
return st.session_state['audio_cache'][message_id]
|
34 |
+
|
35 |
tts = gtts.gTTS(text, lang="id")
|
36 |
audio_bytes = BytesIO()
|
37 |
tts.write_to_fp(audio_bytes)
|
38 |
audio_bytes.seek(0)
|
39 |
audio_base64 = base64.b64encode(audio_bytes.read()).decode()
|
40 |
+
|
41 |
+
# Simpan ke cache jika ada message_id
|
42 |
+
if message_id:
|
43 |
+
st.session_state['audio_cache'][message_id] = audio_base64
|
44 |
+
|
45 |
+
return audio_base64
|
46 |
+
|
47 |
+
# Fungsi untuk membuat audio player
|
48 |
+
def create_audio_player(audio_base64, autoplay=False):
|
49 |
+
autoplay_attr = "autoplay" if autoplay else ""
|
50 |
audio_player = f"""
|
51 |
+
<audio {autoplay_attr} controls style="width: 100%; margin-top: 5px;">
|
52 |
<source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
|
53 |
</audio>
|
54 |
"""
|
|
|
108 |
st.session_state['generated'].append(output)
|
109 |
st.session_state.input_text = ""
|
110 |
|
111 |
+
# Auto-play TTS untuk pesan baru jika diaktifkan
|
112 |
if st.session_state['should_speak'] and output:
|
113 |
+
message_id = len(st.session_state['generated']) - 1
|
114 |
+
audio_base64 = text_to_speech(output, message_id)
|
115 |
+
st.session_state['play_audio'] = message_id
|
116 |
|
117 |
+
# Tampilkan riwayat chat dengan tombol TTS
|
118 |
if st.session_state['generated']:
|
119 |
with reply_container:
|
120 |
for i in range(len(st.session_state['generated'])):
|
121 |
+
# Tampilkan pesan user
|
122 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="no-avatar")
|
123 |
+
|
124 |
+
# Tampilkan pesan bot
|
125 |
message(st.session_state["generated"][i], key=str(i), avatar_style="no-avatar")
|
126 |
+
|
127 |
+
# Tombol dan audio player untuk pesan bot
|
128 |
+
col1, col2 = st.columns([1, 4])
|
129 |
+
with col1:
|
130 |
+
if st.button(f"🔊", key=f"play_tts_{i}", help="Putar ulang audio"):
|
131 |
+
st.session_state['play_audio'] = i
|
132 |
+
st.experimental_rerun()
|
133 |
+
|
134 |
+
# Tampilkan audio player jika pesan ini yang dipilih untuk diputar
|
135 |
+
if st.session_state.get('play_audio') == i:
|
136 |
+
with col2:
|
137 |
+
message_text = st.session_state["generated"][i]
|
138 |
+
audio_base64 = text_to_speech(message_text, i)
|
139 |
+
|
140 |
+
# Auto-play untuk pesan baru, manual untuk replay
|
141 |
+
autoplay = (i == len(st.session_state['generated']) - 1 and
|
142 |
+
st.session_state['should_speak'])
|
143 |
+
|
144 |
+
audio_player = create_audio_player(audio_base64, autoplay)
|
145 |
+
st.markdown(audio_player, unsafe_allow_html=True)
|
146 |
+
|
147 |
+
# Reset play_audio setelah ditampilkan
|
148 |
+
if st.session_state.get('play_audio') == i:
|
149 |
+
st.session_state['play_audio'] = None
|