Spaces:
Sleeping
Sleeping
FauziIsyrinApridal
commited on
Commit
Β·
40eca06
1
Parent(s):
a015207
update chat.py tts berulang dan teks icon
Browse files- app/chat.py +22 -8
app/chat.py
CHANGED
|
@@ -5,6 +5,7 @@ import base64
|
|
| 5 |
import gtts
|
| 6 |
from io import BytesIO
|
| 7 |
|
|
|
|
| 8 |
def initialize_session_state():
|
| 9 |
if 'history' not in st.session_state:
|
| 10 |
st.session_state['history'] = []
|
|
@@ -23,6 +24,7 @@ def initialize_session_state():
|
|
| 23 |
if 'tts_output' not in st.session_state:
|
| 24 |
st.session_state['tts_output'] = ""
|
| 25 |
|
|
|
|
| 26 |
def text_to_speech(text):
|
| 27 |
tts = gtts.gTTS(text, lang="id")
|
| 28 |
audio_bytes = BytesIO()
|
|
@@ -36,14 +38,17 @@ def text_to_speech(text):
|
|
| 36 |
"""
|
| 37 |
return audio_player
|
| 38 |
|
|
|
|
| 39 |
def conversation_chat(query, chain, history):
|
| 40 |
result = chain({"question": query, "chat_history": history})
|
| 41 |
history.append((query, result["answer"]))
|
| 42 |
return result["answer"]
|
| 43 |
|
|
|
|
| 44 |
def display_chat_history(chain):
|
| 45 |
reply_container = st.container()
|
| 46 |
|
|
|
|
| 47 |
user_input_obj = st.chat_input(
|
| 48 |
"Masukkan pertanyaan atau Tekan tombol mic untuk berbicara!",
|
| 49 |
key="chat_input_field"
|
|
@@ -51,15 +56,17 @@ def display_chat_history(chain):
|
|
| 51 |
|
| 52 |
col2, col3 = st.columns([1, 1])
|
| 53 |
|
|
|
|
| 54 |
with col2:
|
| 55 |
should_speak = st.session_state.get('should_speak', True)
|
| 56 |
-
icon_label = "π" if should_speak else "π"
|
| 57 |
if st.button(icon_label, key="toggle_tts", help="Aktifkan/Nonaktifkan Text-to-Speech", use_container_width=True):
|
| 58 |
st.session_state['should_speak'] = not should_speak
|
| 59 |
|
|
|
|
| 60 |
with col3:
|
| 61 |
stt_text = speech_to_text(
|
| 62 |
-
start_prompt="π€",
|
| 63 |
stop_prompt="π Stop",
|
| 64 |
language='id',
|
| 65 |
just_once=True,
|
|
@@ -67,12 +74,12 @@ def display_chat_history(chain):
|
|
| 67 |
use_container_width=True,
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# Jika STT
|
| 71 |
if stt_text:
|
| 72 |
st.session_state.input_text = stt_text
|
| 73 |
st.experimental_rerun()
|
| 74 |
|
| 75 |
-
# Ambil input dari teks
|
| 76 |
user_input = user_input_obj or st.session_state.get("input_text", "")
|
| 77 |
|
| 78 |
if user_input:
|
|
@@ -83,18 +90,25 @@ def display_chat_history(chain):
|
|
| 83 |
st.session_state['generated'].append(output)
|
| 84 |
st.session_state.input_text = ""
|
| 85 |
|
| 86 |
-
# Simpan output untuk TTS
|
| 87 |
if st.session_state['should_speak'] and output:
|
| 88 |
st.session_state['tts_output'] = output
|
| 89 |
|
| 90 |
-
# Tampilkan riwayat
|
| 91 |
if st.session_state['generated']:
|
| 92 |
with reply_container:
|
| 93 |
for i in range(len(st.session_state['generated'])):
|
| 94 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="no-avatar")
|
| 95 |
message(st.session_state["generated"][i], key=str(i), avatar_style="no-avatar")
|
| 96 |
|
| 97 |
-
#
|
| 98 |
if st.session_state.get('tts_output'):
|
| 99 |
st.markdown(text_to_speech(st.session_state['tts_output']), unsafe_allow_html=True)
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import gtts
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
# Inisialisasi state
|
| 9 |
def initialize_session_state():
|
| 10 |
if 'history' not in st.session_state:
|
| 11 |
st.session_state['history'] = []
|
|
|
|
| 24 |
if 'tts_output' not in st.session_state:
|
| 25 |
st.session_state['tts_output'] = ""
|
| 26 |
|
| 27 |
+
# Fungsi TTS
|
| 28 |
def text_to_speech(text):
|
| 29 |
tts = gtts.gTTS(text, lang="id")
|
| 30 |
audio_bytes = BytesIO()
|
|
|
|
| 38 |
"""
|
| 39 |
return audio_player
|
| 40 |
|
| 41 |
+
# Fungsi untuk memanggil chatbot
|
| 42 |
def conversation_chat(query, chain, history):
|
| 43 |
result = chain({"question": query, "chat_history": history})
|
| 44 |
history.append((query, result["answer"]))
|
| 45 |
return result["answer"]
|
| 46 |
|
| 47 |
+
# Tampilan chat dan logika STT + TTS
|
| 48 |
def display_chat_history(chain):
|
| 49 |
reply_container = st.container()
|
| 50 |
|
| 51 |
+
# Input teks biasa
|
| 52 |
user_input_obj = st.chat_input(
|
| 53 |
"Masukkan pertanyaan atau Tekan tombol mic untuk berbicara!",
|
| 54 |
key="chat_input_field"
|
|
|
|
| 56 |
|
| 57 |
col2, col3 = st.columns([1, 1])
|
| 58 |
|
| 59 |
+
# Tombol toggle suara
|
| 60 |
with col2:
|
| 61 |
should_speak = st.session_state.get('should_speak', True)
|
| 62 |
+
icon_label = "π Text-to-Speech Aktif" if should_speak else "π Text-to-Speech Nonaktif"
|
| 63 |
if st.button(icon_label, key="toggle_tts", help="Aktifkan/Nonaktifkan Text-to-Speech", use_container_width=True):
|
| 64 |
st.session_state['should_speak'] = not should_speak
|
| 65 |
|
| 66 |
+
# Rekaman suara
|
| 67 |
with col3:
|
| 68 |
stt_text = speech_to_text(
|
| 69 |
+
start_prompt="π€ Input Suara",
|
| 70 |
stop_prompt="π Stop",
|
| 71 |
language='id',
|
| 72 |
just_once=True,
|
|
|
|
| 74 |
use_container_width=True,
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# Jika hasil STT ada, pakai sebagai input
|
| 78 |
if stt_text:
|
| 79 |
st.session_state.input_text = stt_text
|
| 80 |
st.experimental_rerun()
|
| 81 |
|
| 82 |
+
# Ambil input dari teks atau STT
|
| 83 |
user_input = user_input_obj or st.session_state.get("input_text", "")
|
| 84 |
|
| 85 |
if user_input:
|
|
|
|
| 90 |
st.session_state['generated'].append(output)
|
| 91 |
st.session_state.input_text = ""
|
| 92 |
|
| 93 |
+
# Simpan output untuk TTS
|
| 94 |
if st.session_state['should_speak'] and output:
|
| 95 |
st.session_state['tts_output'] = output
|
| 96 |
|
| 97 |
+
# Tampilkan riwayat chat
|
| 98 |
if st.session_state['generated']:
|
| 99 |
with reply_container:
|
| 100 |
for i in range(len(st.session_state['generated'])):
|
| 101 |
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="no-avatar")
|
| 102 |
message(st.session_state["generated"][i], key=str(i), avatar_style="no-avatar")
|
| 103 |
|
| 104 |
+
# Tampilkan dan mainkan audio TTS jika ada
|
| 105 |
if st.session_state.get('tts_output'):
|
| 106 |
st.markdown(text_to_speech(st.session_state['tts_output']), unsafe_allow_html=True)
|
| 107 |
+
|
| 108 |
+
# Beri waktu render audio sebelum menghapus tts_output
|
| 109 |
+
if 'tts_played' not in st.session_state:
|
| 110 |
+
st.session_state['tts_played'] = True
|
| 111 |
+
st.experimental_rerun()
|
| 112 |
+
else:
|
| 113 |
+
st.session_state['tts_output'] = ""
|
| 114 |
+
del st.session_state['tts_played']
|