Spaces:
Running
Running
FauziIsyrinApridal
commited on
Commit
Β·
2ec6555
1
Parent(s):
becc88f
tambahkan fallback gtts ke edgetts
Browse files- app/chat.py +53 -14
- requirements.txt +1 -0
app/chat.py
CHANGED
@@ -2,14 +2,28 @@ import streamlit as st
|
|
2 |
from streamlit_chat import message
|
3 |
from streamlit_mic_recorder import speech_to_text
|
4 |
import base64
|
|
|
|
|
5 |
import gtts
|
|
|
6 |
from io import BytesIO
|
7 |
-
from app.db import supabase
|
8 |
import os
|
|
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
|
11 |
load_dotenv()
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def save_feedback_to_supabase(feedback_text):
|
15 |
try:
|
@@ -20,7 +34,6 @@ def save_feedback_to_supabase(feedback_text):
|
|
20 |
st.error(f"Gagal menyimpan feedback: {e}")
|
21 |
return False
|
22 |
|
23 |
-
|
24 |
def initialize_session_state():
|
25 |
if 'history' not in st.session_state:
|
26 |
st.session_state['history'] = []
|
@@ -39,25 +52,51 @@ def initialize_session_state():
|
|
39 |
if 'tts_output' not in st.session_state:
|
40 |
st.session_state['tts_output'] = ""
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
def text_to_speech(text):
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
def conversation_chat(query, chain, history):
|
56 |
result = chain({"question": query, "chat_history": history})
|
57 |
history.append((query, result["answer"]))
|
58 |
return result["answer"]
|
59 |
|
60 |
-
|
61 |
def display_chat_history(chain):
|
62 |
reply_container = st.container()
|
63 |
|
|
|
2 |
from streamlit_chat import message
|
3 |
from streamlit_mic_recorder import speech_to_text
|
4 |
import base64
|
5 |
+
import asyncio
|
6 |
+
import edge_tts
|
7 |
import gtts
|
8 |
+
from hashlib import md5
|
9 |
from io import BytesIO
|
10 |
+
from app.db import supabase
|
11 |
import os
|
12 |
+
import glob
|
13 |
+
import time
|
14 |
from dotenv import load_dotenv
|
15 |
|
16 |
load_dotenv()
|
17 |
|
18 |
+
# Bersihkan cache audio lama (opsional)
|
19 |
+
def clean_old_cache(tts_dir="cache_tts", max_age_hours=12):
|
20 |
+
now = time.time()
|
21 |
+
for f in glob.glob(os.path.join(tts_dir, "*.mp3")):
|
22 |
+
if os.stat(f).st_mtime < now - max_age_hours * 3600:
|
23 |
+
os.remove(f)
|
24 |
+
|
25 |
+
# Jalankan pembersihan saat startup
|
26 |
+
clean_old_cache()
|
27 |
|
28 |
def save_feedback_to_supabase(feedback_text):
|
29 |
try:
|
|
|
34 |
st.error(f"Gagal menyimpan feedback: {e}")
|
35 |
return False
|
36 |
|
|
|
37 |
def initialize_session_state():
|
38 |
if 'history' not in st.session_state:
|
39 |
st.session_state['history'] = []
|
|
|
52 |
if 'tts_output' not in st.session_state:
|
53 |
st.session_state['tts_output'] = ""
|
54 |
|
55 |
+
# edge-tts fallback (cadangan)
|
56 |
+
async def generate_audio_edge(text, path, voice="id-ID-GadisNeural"):
|
57 |
+
communicate = edge_tts.Communicate(text, voice=voice)
|
58 |
+
await communicate.save(path)
|
59 |
+
|
60 |
+
# fungsi utama TTS dengan fallback
|
61 |
def text_to_speech(text):
|
62 |
+
cache_dir = "cache_tts"
|
63 |
+
os.makedirs(cache_dir, exist_ok=True)
|
64 |
+
filename = f"{md5(text.encode()).hexdigest()}.mp3"
|
65 |
+
path = os.path.join(cache_dir, filename)
|
66 |
+
|
67 |
+
if not os.path.exists(path):
|
68 |
+
try:
|
69 |
+
# β
Utama: gTTS
|
70 |
+
tts = gtts.gTTS(text, lang="id")
|
71 |
+
tts.save(path)
|
72 |
+
except Exception as e:
|
73 |
+
print(f"[gTTS gagal] {e}")
|
74 |
+
try:
|
75 |
+
# β
Cadangan: edge-tts
|
76 |
+
asyncio.run(generate_audio_edge(text, path))
|
77 |
+
except Exception as e2:
|
78 |
+
print(f"[Edge-TTS juga gagal] {e2}")
|
79 |
+
st.warning("π Gagal membuat audio TTS.")
|
80 |
+
return ""
|
81 |
+
|
82 |
+
try:
|
83 |
+
with open(path, "rb") as audio_file:
|
84 |
+
audio_base64 = base64.b64encode(audio_file.read()).decode()
|
85 |
+
|
86 |
+
return f"""
|
87 |
+
<audio autoplay>
|
88 |
+
<source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
|
89 |
+
</audio>
|
90 |
+
"""
|
91 |
+
except Exception as e:
|
92 |
+
print(f"[Error saat membaca audio] {e}")
|
93 |
+
return ""
|
94 |
|
95 |
def conversation_chat(query, chain, history):
|
96 |
result = chain({"question": query, "chat_history": history})
|
97 |
history.append((query, result["answer"]))
|
98 |
return result["answer"]
|
99 |
|
|
|
100 |
def display_chat_history(chain):
|
101 |
reply_container = st.container()
|
102 |
|
requirements.txt
CHANGED
@@ -116,5 +116,6 @@ SpeechRecognition
|
|
116 |
chardet
|
117 |
streamlit_mic_recorder
|
118 |
gtts
|
|
|
119 |
playwright
|
120 |
dotenv
|
|
|
116 |
chardet
|
117 |
streamlit_mic_recorder
|
118 |
gtts
|
119 |
+
edge-tts
|
120 |
playwright
|
121 |
dotenv
|