Spaces:
Sleeping
Sleeping
Updating to test
Browse files- chatbot.py +40 -23
chatbot.py
CHANGED
|
@@ -66,7 +66,7 @@ async def text_to_speech(text: str, filename: str):
|
|
| 66 |
|
| 67 |
|
| 68 |
def render_chatbot(code: str, output: str, error: str):
|
| 69 |
-
"""Render the chatbot UI
|
| 70 |
|
| 71 |
# Initialize session state
|
| 72 |
if "conversation" not in st.session_state:
|
|
@@ -77,9 +77,17 @@ def render_chatbot(code: str, output: str, error: str):
|
|
| 77 |
# Create bot instance
|
| 78 |
bot = CodeAssistantBot()
|
| 79 |
|
| 80 |
-
#
|
| 81 |
st.markdown("""
|
| 82 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
.chat-message {
|
| 84 |
padding: 10px;
|
| 85 |
border-radius: 5px;
|
|
@@ -92,13 +100,25 @@ def render_chatbot(code: str, output: str, error: str):
|
|
| 92 |
background-color: #f5f5f5;
|
| 93 |
}
|
| 94 |
</style>
|
| 95 |
-
""",
|
| 96 |
-
|
| 97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
col1, col2 = st.columns([4, 1])
|
| 99 |
with col1:
|
| 100 |
-
user_input = st.text_input("Ask your Question here",key="chat_input",
|
| 101 |
-
placeholder="Type your question here...")
|
| 102 |
with col2:
|
| 103 |
send_clicked = st.button("🚀") # Button to send the message
|
| 104 |
|
|
@@ -106,30 +126,27 @@ def render_chatbot(code: str, output: str, error: str):
|
|
| 106 |
# Get response
|
| 107 |
response = bot.analyze_code(code, output, error, user_input)
|
| 108 |
st.session_state.conversation.append((user_input, response))
|
| 109 |
-
|
| 110 |
-
|
| 111 |
# Generate summary and speech if conversation is long enough
|
| 112 |
if len(st.session_state.conversation) > 3:
|
| 113 |
with st.spinner("Generating conversation summary..."):
|
| 114 |
-
summary = bot.summarize_conversation(
|
| 115 |
-
st.session_state.conversation)
|
| 116 |
audio_file = f"summary_{st.session_state.audio_count}.wav"
|
| 117 |
asyncio.run(text_to_speech(summary, audio_file))
|
| 118 |
st.session_state.audio_count += 1
|
| 119 |
|
| 120 |
-
# Display summary in a collapsible section
|
| 121 |
with st.expander("📝 Conversation Summary", expanded=False):
|
| 122 |
st.markdown(summary)
|
| 123 |
st.audio(audio_file, format="audio/wav")
|
| 124 |
-
|
| 125 |
-
#
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
def render_chatbot(code: str, output: str, error: str):
|
| 69 |
+
"""Render the chatbot UI in a fixed-height, scrollable panel"""
|
| 70 |
|
| 71 |
# Initialize session state
|
| 72 |
if "conversation" not in st.session_state:
|
|
|
|
| 77 |
# Create bot instance
|
| 78 |
bot = CodeAssistantBot()
|
| 79 |
|
| 80 |
+
# Apply CSS for scrollable chat window
|
| 81 |
st.markdown("""
|
| 82 |
<style>
|
| 83 |
+
.chat-container {
|
| 84 |
+
height: 500px; /* Fixed height */
|
| 85 |
+
overflow-y: auto; /* Scrollable */
|
| 86 |
+
border: 1px solid #ddd;
|
| 87 |
+
padding: 10px;
|
| 88 |
+
background-color: #f9f9f9;
|
| 89 |
+
border-radius: 5px;
|
| 90 |
+
}
|
| 91 |
.chat-message {
|
| 92 |
padding: 10px;
|
| 93 |
border-radius: 5px;
|
|
|
|
| 100 |
background-color: #f5f5f5;
|
| 101 |
}
|
| 102 |
</style>
|
| 103 |
+
""", unsafe_allow_html=True)
|
| 104 |
+
|
| 105 |
+
# Chat history in a scrollable container
|
| 106 |
+
chat_container = st.container()
|
| 107 |
+
with chat_container:
|
| 108 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 109 |
+
|
| 110 |
+
# Display all chat messages
|
| 111 |
+
for q, a in st.session_state.conversation:
|
| 112 |
+
st.markdown(f'<div class="chat-message user-message">You: {q}</div>', unsafe_allow_html=True)
|
| 113 |
+
st.markdown(f'<div class="chat-message bot-message">Assistant: {a}</div>', unsafe_allow_html=True)
|
| 114 |
+
|
| 115 |
+
# Close the chat container div
|
| 116 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 117 |
+
|
| 118 |
+
# Input area at the bottom
|
| 119 |
col1, col2 = st.columns([4, 1])
|
| 120 |
with col1:
|
| 121 |
+
user_input = st.text_input("Ask your Question here", key="chat_input", placeholder="Type your question here...")
|
|
|
|
| 122 |
with col2:
|
| 123 |
send_clicked = st.button("🚀") # Button to send the message
|
| 124 |
|
|
|
|
| 126 |
# Get response
|
| 127 |
response = bot.analyze_code(code, output, error, user_input)
|
| 128 |
st.session_state.conversation.append((user_input, response))
|
| 129 |
+
|
|
|
|
| 130 |
# Generate summary and speech if conversation is long enough
|
| 131 |
if len(st.session_state.conversation) > 3:
|
| 132 |
with st.spinner("Generating conversation summary..."):
|
| 133 |
+
summary = bot.summarize_conversation(st.session_state.conversation)
|
|
|
|
| 134 |
audio_file = f"summary_{st.session_state.audio_count}.wav"
|
| 135 |
asyncio.run(text_to_speech(summary, audio_file))
|
| 136 |
st.session_state.audio_count += 1
|
| 137 |
|
|
|
|
| 138 |
with st.expander("📝 Conversation Summary", expanded=False):
|
| 139 |
st.markdown(summary)
|
| 140 |
st.audio(audio_file, format="audio/wav")
|
| 141 |
+
|
| 142 |
+
# **Auto-scroll to bottom (forcing UI refresh)**
|
| 143 |
+
st.markdown("""
|
| 144 |
+
<script>
|
| 145 |
+
var chatDiv = window.parent.document.querySelector('.chat-container');
|
| 146 |
+
if (chatDiv) {
|
| 147 |
+
chatDiv.scrollTop = chatDiv.scrollHeight;
|
| 148 |
+
}
|
| 149 |
+
</script>
|
| 150 |
+
""", unsafe_allow_html=True)
|
| 151 |
+
|
| 152 |
|