FauziIsyrinApridal commited on
Commit
b8430a0
Β·
1 Parent(s): 6c4447c

tambah feedback ke supabase

Browse files
Files changed (1) hide show
  1. app/chat.py +53 -18
app/chat.py CHANGED
@@ -4,8 +4,27 @@ from streamlit_mic_recorder import speech_to_text
4
  import base64
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,7 +43,9 @@ def initialize_session_state():
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,34 +59,34 @@ def text_to_speech(text):
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",
54
- key="chat_input_field"
55
- )
56
 
57
  col2, col3 = st.columns([1, 1])
58
 
59
- # Tombol toggle suara
60
  with col2:
61
  if st.button("πŸ”Š Text-to-Speech Aktif" if st.session_state['should_speak'] else "πŸ”‡ Text-to-Speech Nonaktif",
62
- key="toggle_tts",
63
- help="Aktifkan/Nonaktifkan Text-to-Speech",
64
- use_container_width=True):
65
  st.session_state['should_speak'] = not st.session_state['should_speak']
66
  st.experimental_rerun()
67
 
68
- # Rekaman suara
69
  with col3:
70
  stt_text = speech_to_text(
71
  start_prompt="🎀 Input Suara",
@@ -76,12 +97,12 @@ def display_chat_history(chain):
76
  use_container_width=True,
77
  )
78
 
79
- # Jika hasil STT ada, pakai sebagai input
80
  if stt_text:
81
  st.session_state.input_text = stt_text
82
  st.experimental_rerun()
83
 
84
- # Ambil input dari teks atau STT
85
  user_input = user_input_obj or st.session_state.get("input_text", "")
86
 
87
  if user_input:
@@ -92,23 +113,37 @@ def display_chat_history(chain):
92
  st.session_state['generated'].append(output)
93
  st.session_state.input_text = ""
94
 
95
- # Simpan output untuk TTS
96
  if st.session_state['should_speak'] and output:
97
  st.session_state['tts_output'] = output
98
  if 'tts_played' in st.session_state:
99
  del st.session_state['tts_played']
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
- # Tampilkan dan mainkan audio TTS jika ada
109
  if st.session_state.get('tts_output') and not st.session_state.get('tts_played'):
110
  st.markdown(text_to_speech(st.session_state['tts_output']), unsafe_allow_html=True)
111
  st.session_state['tts_played'] = True
112
  elif st.session_state.get('tts_played'):
113
  st.session_state['tts_output'] = ""
114
  del st.session_state['tts_played']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import base64
5
  import gtts
6
  from io import BytesIO
7
+ from app.db import supabase # pastikan ini ada
8
+ import os
9
+ from dotenv import load_dotenv
10
 
11
+ load_dotenv()
12
+
13
+ # -------------------------------
14
+ # Simpan Feedback ke Supabase
15
+ # -------------------------------
16
+ def save_feedback_to_supabase(feedback_text):
17
+ try:
18
+ data = {"message": feedback_text}
19
+ supabase.table("feedback").insert(data).execute()
20
+ return True
21
+ except Exception as e:
22
+ st.error(f"Gagal menyimpan feedback: {e}")
23
+ return False
24
+
25
+ # -------------------------------
26
+ # Inisialisasi State
27
+ # -------------------------------
28
  def initialize_session_state():
29
  if 'history' not in st.session_state:
30
  st.session_state['history'] = []
 
43
  if 'tts_output' not in st.session_state:
44
  st.session_state['tts_output'] = ""
45
 
46
+ # -------------------------------
47
  # Fungsi TTS
48
+ # -------------------------------
49
  def text_to_speech(text):
50
  tts = gtts.gTTS(text, lang="id")
51
  audio_bytes = BytesIO()
 
59
  """
60
  return audio_player
61
 
62
+ # -------------------------------
63
+ # Fungsi Chatbot
64
+ # -------------------------------
65
  def conversation_chat(query, chain, history):
66
  result = chain({"question": query, "chat_history": history})
67
  history.append((query, result["answer"]))
68
  return result["answer"]
69
 
70
+ # -------------------------------
71
+ # Tampilan & Logika Chat
72
+ # -------------------------------
73
  def display_chat_history(chain):
74
  reply_container = st.container()
75
 
76
+ user_input_obj = st.chat_input("Masukkan pertanyaan", key="chat_input_field")
 
 
 
 
77
 
78
  col2, col3 = st.columns([1, 1])
79
 
80
+ # Tombol TTS Aktif / Nonaktif
81
  with col2:
82
  if st.button("πŸ”Š Text-to-Speech Aktif" if st.session_state['should_speak'] else "πŸ”‡ Text-to-Speech Nonaktif",
83
+ key="toggle_tts",
84
+ help="Aktifkan/Nonaktifkan Text-to-Speech",
85
+ use_container_width=True):
86
  st.session_state['should_speak'] = not st.session_state['should_speak']
87
  st.experimental_rerun()
88
 
89
+ # Tombol Input Suara
90
  with col3:
91
  stt_text = speech_to_text(
92
  start_prompt="🎀 Input Suara",
 
97
  use_container_width=True,
98
  )
99
 
100
+ # Jika ada STT
101
  if stt_text:
102
  st.session_state.input_text = stt_text
103
  st.experimental_rerun()
104
 
105
+ # Ambil input user
106
  user_input = user_input_obj or st.session_state.get("input_text", "")
107
 
108
  if user_input:
 
113
  st.session_state['generated'].append(output)
114
  st.session_state.input_text = ""
115
 
116
+ # Siapkan untuk TTS
117
  if st.session_state['should_speak'] and output:
118
  st.session_state['tts_output'] = output
119
  if 'tts_played' in st.session_state:
120
  del st.session_state['tts_played']
121
 
122
+ # Tampilkan Riwayat Chat
123
  if st.session_state['generated']:
124
  with reply_container:
125
  for i in range(len(st.session_state['generated'])):
126
  message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="no-avatar")
127
  message(st.session_state["generated"][i], key=str(i), avatar_style="no-avatar")
128
 
129
+ # TTS Play
130
  if st.session_state.get('tts_output') and not st.session_state.get('tts_played'):
131
  st.markdown(text_to_speech(st.session_state['tts_output']), unsafe_allow_html=True)
132
  st.session_state['tts_played'] = True
133
  elif st.session_state.get('tts_played'):
134
  st.session_state['tts_output'] = ""
135
  del st.session_state['tts_played']
136
+
137
+ # -------------------------------
138
+ # Form Kirim Feedback
139
+ # -------------------------------
140
+ with st.sidebar:
141
+ st.markdown("### πŸ’¬ Feedback")
142
+ with st.form("sidebar_feedback_form"):
143
+ feedback_text = st.text_area("Masukkan feedback Anda di sini:")
144
+ submitted = st.form_submit_button("Kirim")
145
+
146
+ if submitted and feedback_text.strip():
147
+ success = save_feedback_to_supabase(feedback_text)
148
+ if success:
149
+ st.success("βœ… Terima kasih atas feedback-nya!")