FauziIsyrinApridal commited on
Commit
a015207
Β·
1 Parent(s): 71e79b1

update chat.py tts berulang

Browse files
Files changed (1) hide show
  1. app/chat.py +13 -8
app/chat.py CHANGED
@@ -20,6 +20,8 @@ def initialize_session_state():
20
  st.session_state['should_speak'] = True
21
  if 'input_text' not in st.session_state:
22
  st.session_state['input_text'] = ""
 
 
23
 
24
  def text_to_speech(text):
25
  tts = gtts.gTTS(text, lang="id")
@@ -42,15 +44,13 @@ def conversation_chat(query, chain, history):
42
  def display_chat_history(chain):
43
  reply_container = st.container()
44
 
45
- # Pindahkan chat_input ke luar kolom, biar di root level:
46
  user_input_obj = st.chat_input(
47
  "Masukkan pertanyaan atau Tekan tombol mic untuk berbicara!",
48
  key="chat_input_field"
49
  )
50
 
51
- # Sekarang baru buat kolom untuk tombol toggle dan mic:
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 "πŸ”‡"
@@ -67,11 +67,12 @@ def display_chat_history(chain):
67
  use_container_width=True,
68
  )
69
 
70
- # STT handling dan input processing tetap sama
71
  if stt_text:
72
  st.session_state.input_text = stt_text
73
- st.experimental_rerun() # pakai rerun yang direkomendasikan
74
 
 
75
  user_input = user_input_obj or st.session_state.get("input_text", "")
76
 
77
  if user_input:
@@ -82,14 +83,18 @@ def display_chat_history(chain):
82
  st.session_state['generated'].append(output)
83
  st.session_state.input_text = ""
84
 
 
85
  if st.session_state['should_speak'] and output:
86
- st.markdown(text_to_speech(output), unsafe_allow_html=True)
87
 
 
88
  if st.session_state['generated']:
89
  with reply_container:
90
  for i in range(len(st.session_state['generated'])):
91
  message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="no-avatar")
92
  message(st.session_state["generated"][i], key=str(i), avatar_style="no-avatar")
93
 
94
-
95
-
 
 
 
20
  st.session_state['should_speak'] = True
21
  if 'input_text' not in st.session_state:
22
  st.session_state['input_text'] = ""
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")
 
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"
50
  )
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 "πŸ”‡"
 
67
  use_container_width=True,
68
  )
69
 
70
+ # Jika STT aktif, simpan ke input dan rerun
71
  if stt_text:
72
  st.session_state.input_text = stt_text
73
+ st.experimental_rerun()
74
 
75
+ # Ambil input dari teks biasa atau hasil STT
76
  user_input = user_input_obj or st.session_state.get("input_text", "")
77
 
78
  if user_input:
 
83
  st.session_state['generated'].append(output)
84
  st.session_state.input_text = ""
85
 
86
+ # Simpan output untuk TTS (dieksekusi setelah UI selesai)
87
  if st.session_state['should_speak'] and output:
88
+ st.session_state['tts_output'] = output
89
 
90
+ # Tampilkan riwayat obrolan
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
+ # Mainkan TTS jika ada
98
+ if st.session_state.get('tts_output'):
99
+ st.markdown(text_to_speech(st.session_state['tts_output']), unsafe_allow_html=True)
100
+ st.session_state['tts_output'] = ""