Spaces:
Sleeping
Sleeping
Update chatbot.py
Browse files- chatbot.py +28 -74
chatbot.py
CHANGED
|
@@ -66,87 +66,41 @@ async def text_to_speech(text: str, filename: str):
|
|
| 66 |
|
| 67 |
|
| 68 |
def render_chatbot(code: str, output: str, error: str):
|
| 69 |
-
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if "audio_count" not in st.session_state:
|
| 75 |
-
st.session_state.audio_count = 0
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
.
|
| 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;
|
| 94 |
-
margin-bottom: 10px;
|
| 95 |
-
}
|
| 96 |
-
.user-message {
|
| 97 |
-
background-color: #e3f2fd;
|
| 98 |
-
}
|
| 99 |
-
.bot-message {
|
| 100 |
-
background-color: #f5f5f5;
|
| 101 |
-
}
|
| 102 |
-
</style>
|
| 103 |
-
""", unsafe_allow_html=True)
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
-
#
|
| 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 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
#
|
| 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 |
-
|
| 125 |
-
if user_input and send_clicked:
|
| 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 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
}
|
| 149 |
-
</script>
|
| 150 |
""", unsafe_allow_html=True)
|
| 151 |
|
| 152 |
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
def render_chatbot(code: str, output: str, error: str):
|
| 69 |
+
# … your imports, CSS, bot init …
|
| 70 |
|
| 71 |
+
# 1. Ensure session state keys exist
|
| 72 |
+
st.session_state.setdefault("conversation", [])
|
| 73 |
+
st.session_state.setdefault("audio_count", 0)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# 2. **Input area first**
|
| 76 |
+
col1, col2 = st.columns([4, 1])
|
| 77 |
+
with col1:
|
| 78 |
+
# give this a unique key so it doesn't reset on rerun
|
| 79 |
+
user_q = st.text_input("Ask your Question here", key="chat_input", placeholder="Type your question…")
|
| 80 |
+
with col2:
|
| 81 |
+
send = st.button("🚀")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# 3. **Handle send**
|
| 84 |
+
if send and user_q:
|
| 85 |
+
bot = CodeAssistantBot()
|
| 86 |
+
resp = bot.analyze_code(code, output, error, user_q)
|
| 87 |
+
st.session_state.conversation.append((user_q, resp))
|
| 88 |
|
| 89 |
+
# optional: summary+TTS logic…
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# 4. **Now** render the scrollable chat container
|
| 92 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 93 |
+
for q, a in st.session_state.conversation:
|
| 94 |
+
st.markdown(f'<div class="chat-message user-message">You: {q}</div>', unsafe_allow_html=True)
|
| 95 |
+
st.markdown(f'<div class="chat-message bot-message">Assistant: {a}</div>', unsafe_allow_html=True)
|
| 96 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 97 |
|
| 98 |
+
# 5. Auto‑scroll script (as you had it)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
st.markdown("""
|
| 100 |
+
<script>
|
| 101 |
+
const el = window.parent.document.querySelector('.chat-container');
|
| 102 |
+
if(el) el.scrollTop = el.scrollHeight;
|
| 103 |
+
</script>
|
|
|
|
|
|
|
| 104 |
""", unsafe_allow_html=True)
|
| 105 |
|
| 106 |
|