Spaces:
Running
Running
updating
Browse files- chatbot.py +82 -21
chatbot.py
CHANGED
|
@@ -41,25 +41,86 @@ async def text_to_speech(text, filename):
|
|
| 41 |
await edge_tts.Communicate(text, voice).save(filename)
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 a scrollable chat container."""
|
| 46 |
+
|
| 47 |
+
# Inject CSS to make chat container scrollable
|
| 48 |
+
st.markdown("""
|
| 49 |
+
<style>
|
| 50 |
+
.chat-container {
|
| 51 |
+
max-height: 60vh;
|
| 52 |
+
overflow-y: auto;
|
| 53 |
+
padding-right: 0.5rem;
|
| 54 |
+
border: 1px solid #ddd;
|
| 55 |
+
border-radius: 8px;
|
| 56 |
+
margin-top: 1rem;
|
| 57 |
+
padding: 1rem;
|
| 58 |
+
background-color: #f9f9f9;
|
| 59 |
+
}
|
| 60 |
+
.chat-message {
|
| 61 |
+
margin-bottom: 1rem;
|
| 62 |
+
word-wrap: break-word;
|
| 63 |
+
}
|
| 64 |
+
.user-message {
|
| 65 |
+
font-weight: bold;
|
| 66 |
+
color: #1a73e8;
|
| 67 |
+
}
|
| 68 |
+
.bot-message pre {
|
| 69 |
+
background-color: #f0f0f0;
|
| 70 |
+
padding: 0.5rem;
|
| 71 |
+
border-radius: 5px;
|
| 72 |
+
overflow-x: auto;
|
| 73 |
+
}
|
| 74 |
+
</style>
|
| 75 |
+
""", unsafe_allow_html=True)
|
| 76 |
|
| 77 |
+
# Setup session state
|
| 78 |
+
st.session_state.setdefault('conversation', [])
|
| 79 |
+
st.session_state.setdefault('audio_count', 0)
|
| 80 |
+
|
| 81 |
+
# Input row
|
| 82 |
+
c1, c2 = st.columns([4, 1], gap='small')
|
| 83 |
+
with c1:
|
| 84 |
+
question = st.text_input("Ask your question…", key="chat_input")
|
| 85 |
+
with c2:
|
| 86 |
+
send = st.button("🚀")
|
| 87 |
+
|
| 88 |
+
# Handle send
|
| 89 |
+
if send and question:
|
| 90 |
+
bot = CodeAssistantBot()
|
| 91 |
+
response = bot.analyze_code(code, output, error, question)
|
| 92 |
+
st.session_state.conversation.append((question, response))
|
| 93 |
+
|
| 94 |
+
# Chat container
|
| 95 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 96 |
+
for q, a in st.session_state.conversation:
|
| 97 |
+
# User message
|
| 98 |
+
st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
|
| 99 |
+
|
| 100 |
+
# Bot message with code formatting
|
| 101 |
+
def format_response(text):
|
| 102 |
+
parts = text.split('```')
|
| 103 |
+
result = ''
|
| 104 |
+
for i, part in enumerate(parts):
|
| 105 |
+
if i % 2 == 1:
|
| 106 |
+
# Remove optional language tag
|
| 107 |
+
lines = part.splitlines()
|
| 108 |
+
if lines and lines[0].isalpha():
|
| 109 |
+
lines = lines[1:]
|
| 110 |
+
code_html = escape("\n".join(lines))
|
| 111 |
+
result += f'<pre><code>{code_html}</code></pre>'
|
| 112 |
+
else:
|
| 113 |
+
result += escape(part)
|
| 114 |
+
return result
|
| 115 |
+
|
| 116 |
+
formatted = format_response(a)
|
| 117 |
+
st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
|
| 118 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 119 |
+
|
| 120 |
+
# Auto-scroll inside chat container
|
| 121 |
+
st.markdown("""
|
| 122 |
+
<script>
|
| 123 |
+
const c = window.parent.document.querySelector('.chat-container');
|
| 124 |
+
if (c) c.scrollTop = c.scrollHeight;
|
| 125 |
+
</script>
|
| 126 |
+
""", unsafe_allow_html=True)
|