Spaces:
Sleeping
Sleeping
Updating randor_chatbot
Browse files- chatbot.py +21 -50
chatbot.py
CHANGED
|
@@ -41,54 +41,25 @@ async def text_to_speech(text, filename):
|
|
| 41 |
await edge_tts.Communicate(text, voice).save(filename)
|
| 42 |
|
| 43 |
|
| 44 |
-
def render_chatbot(code, output, error):
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if send and question:
|
| 58 |
-
bot = CodeAssistantBot()
|
| 59 |
-
response = bot.analyze_code(code, output, error, question)
|
| 60 |
-
st.session_state.conversation.append((question, response))
|
| 61 |
-
|
| 62 |
-
# Chat container
|
| 63 |
-
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 64 |
-
for q, a in st.session_state.conversation:
|
| 65 |
-
# User message
|
| 66 |
-
st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
|
| 67 |
-
|
| 68 |
-
# Bot message with code formatting
|
| 69 |
-
def format_response(text):
|
| 70 |
-
parts = text.split('```')
|
| 71 |
-
result = ''
|
| 72 |
-
for i, part in enumerate(parts):
|
| 73 |
-
if i % 2 == 1:
|
| 74 |
-
# Remove optional language tag
|
| 75 |
-
lines = part.splitlines()
|
| 76 |
-
if lines and lines[0].isalpha():
|
| 77 |
-
lines = lines[1:]
|
| 78 |
-
code_html = escape("\n".join(lines))
|
| 79 |
-
result += f'<pre><code>{code_html}</code></pre>'
|
| 80 |
-
else:
|
| 81 |
-
result += escape(part)
|
| 82 |
-
return result
|
| 83 |
-
|
| 84 |
-
formatted = format_response(a)
|
| 85 |
-
st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
|
| 86 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 87 |
-
|
| 88 |
-
# Auto-scroll
|
| 89 |
-
st.markdown("""
|
| 90 |
-
<script>
|
| 91 |
-
const c = window.parent.document.querySelector('.chat-container');
|
| 92 |
-
if (c) c.scrollTop = c.scrollHeight;
|
| 93 |
-
</script>
|
| 94 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 41 |
await edge_tts.Communicate(text, voice).save(filename)
|
| 42 |
|
| 43 |
|
| 44 |
+
def render_chatbot(code, output, error):
|
| 45 |
+
"""Render the chatbot UI with code-block support and no deprecated APIs."""
|
| 46 |
+
+ # ---- begin: make chat area its own scrollable box ----
|
| 47 |
+
+ st.markdown("""
|
| 48 |
+
+ <style>
|
| 49 |
+
+ .chat-container {
|
| 50 |
+
+ max-height: 60vh; /* or a fixed px value like 400px */
|
| 51 |
+
+ overflow-y: auto;
|
| 52 |
+
+ padding-right: 0.5rem; /* prevent text clipping */
|
| 53 |
+
+ }
|
| 54 |
+
+ </style>
|
| 55 |
+
+ """, unsafe_allow_html=True)
|
| 56 |
+
+ # ---- end: scrollable chat CSS ----
|
| 57 |
|
| 58 |
+
st.session_state.setdefault('conversation', [])
|
| 59 |
+
st.session_state.setdefault('audio_count', 0)
|
| 60 |
+
…
|
| 61 |
+
# Chat container
|
| 62 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 63 |
+
…
|
| 64 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 65 |
+
…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|