Spaces:
Sleeping
Sleeping
Fixing narrate error
Browse files- chatbot.py +20 -21
chatbot.py
CHANGED
|
@@ -100,7 +100,7 @@ def render_chatbot(code, output, error):
|
|
| 100 |
st.session_state.setdefault('conversation', [])
|
| 101 |
st.session_state.setdefault('chat_summary', "")
|
| 102 |
st.session_state.setdefault('chat_display_count', 5)
|
| 103 |
-
st.session_state.setdefault('
|
| 104 |
|
| 105 |
c1, c2 = st.columns([4, 1], gap='small')
|
| 106 |
with c1:
|
|
@@ -125,9 +125,9 @@ def render_chatbot(code, output, error):
|
|
| 125 |
|
| 126 |
total = len(st.session_state.conversation)
|
| 127 |
start = max(0, total - st.session_state.chat_display_count)
|
| 128 |
-
visible = list(reversed(st.session_state.conversation[start:]))
|
| 129 |
|
| 130 |
-
for
|
| 131 |
st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
|
| 132 |
|
| 133 |
def format_response(txt):
|
|
@@ -147,23 +147,22 @@ def render_chatbot(code, output, error):
|
|
| 147 |
formatted = format_response(a)
|
| 148 |
st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
|
| 149 |
|
| 150 |
-
#
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
narration, audio_file = st.session_state.narrations[i]
|
| 167 |
st.audio(audio_file, format="audio/mp3", autoplay=False)
|
| 168 |
|
| 169 |
if start > 0 and st.button("π½ Show more"):
|
|
@@ -175,4 +174,4 @@ def render_chatbot(code, output, error):
|
|
| 175 |
const c = window.parent.document.querySelector('.chat-container');
|
| 176 |
if (c) c.scrollTop = c.scrollHeight;
|
| 177 |
</script>
|
| 178 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 100 |
st.session_state.setdefault('conversation', [])
|
| 101 |
st.session_state.setdefault('chat_summary', "")
|
| 102 |
st.session_state.setdefault('chat_display_count', 5)
|
| 103 |
+
st.session_state.setdefault('narrated_audio', {})
|
| 104 |
|
| 105 |
c1, c2 = st.columns([4, 1], gap='small')
|
| 106 |
with c1:
|
|
|
|
| 125 |
|
| 126 |
total = len(st.session_state.conversation)
|
| 127 |
start = max(0, total - st.session_state.chat_display_count)
|
| 128 |
+
visible = list(reversed(st.session_state.conversation[start:]))
|
| 129 |
|
| 130 |
+
for idx, (q, a) in enumerate(visible):
|
| 131 |
st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
|
| 132 |
|
| 133 |
def format_response(txt):
|
|
|
|
| 147 |
formatted = format_response(a)
|
| 148 |
st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
|
| 149 |
|
| 150 |
+
# Check if already narrated
|
| 151 |
+
audio_file = st.session_state.narrated_audio.get((q, a))
|
| 152 |
+
|
| 153 |
+
if not audio_file:
|
| 154 |
+
if st.button("π Narrate", key=f"narrate_{idx}"):
|
| 155 |
+
status_placeholder = st.empty()
|
| 156 |
+
status_placeholder.info("π§ Generating narration...")
|
| 157 |
+
bot = CodeAssistantBot()
|
| 158 |
+
narration = bot.narrate_response(code, output, error, a, st.session_state.chat_summary)
|
| 159 |
+
status_placeholder.info("ποΈ Converting to audio...")
|
| 160 |
+
audio_file = f"audio_{uuid.uuid4().hex}.mp3"
|
| 161 |
+
asyncio.run(text_to_speech(narration, audio_file))
|
| 162 |
+
st.session_state.narrated_audio[(q, a)] = audio_file
|
| 163 |
+
status_placeholder.success("π Narration ready!")
|
| 164 |
+
st.audio(audio_file, format="audio/mp3", autoplay=True)
|
| 165 |
+
else:
|
|
|
|
| 166 |
st.audio(audio_file, format="audio/mp3", autoplay=False)
|
| 167 |
|
| 168 |
if start > 0 and st.button("π½ Show more"):
|
|
|
|
| 174 |
const c = window.parent.document.querySelector('.chat-container');
|
| 175 |
if (c) c.scrollTop = c.scrollHeight;
|
| 176 |
</script>
|
| 177 |
+
""", unsafe_allow_html=True)
|