Spaces:
Sleeping
Sleeping
Limiting chatbot
Browse files- chatbot.py +26 -10
chatbot.py
CHANGED
|
@@ -15,11 +15,11 @@ class CodeAssistantBot:
|
|
| 15 |
self.client = Groq(api_key=GROQ_API_KEY)
|
| 16 |
self.model = ChatGroq(model="llama-3.3-70b-versatile", temperature=0.6)
|
| 17 |
self.analysis_prompt = ChatPromptTemplate.from_messages([
|
| 18 |
-
("system",
|
| 19 |
"You are a skilled coding assistant. Use the following context and user input to help."
|
| 20 |
-
" Refer to previous summary and recent interactions to make answers accurate.
|
| 21 |
-
"Keep your response short, relevant, and conversational — smaller is better if it covers what's needed."),
|
| 22 |
-
("user",
|
| 23 |
"Code: {code}\nOutput: {output}\nError: {error}\n"
|
| 24 |
"Summary: {summary}\nRecent: {recent}\nQuestion: {question}")
|
| 25 |
])
|
|
@@ -51,6 +51,7 @@ async def text_to_speech(text, filename):
|
|
| 51 |
def render_chatbot(code, output, error):
|
| 52 |
"""Render the chatbot UI with code-block support and a scrollable chat container."""
|
| 53 |
|
|
|
|
| 54 |
st.markdown("""
|
| 55 |
<style>
|
| 56 |
.chat-container {
|
|
@@ -80,16 +81,20 @@ def render_chatbot(code, output, error):
|
|
| 80 |
</style>
|
| 81 |
""", unsafe_allow_html=True)
|
| 82 |
|
|
|
|
| 83 |
st.session_state.setdefault('conversation', [])
|
| 84 |
st.session_state.setdefault('audio_count', 0)
|
| 85 |
st.session_state.setdefault('chat_summary', "")
|
|
|
|
| 86 |
|
|
|
|
| 87 |
c1, c2 = st.columns([4, 1], gap='small')
|
| 88 |
with c1:
|
| 89 |
question = st.text_input("Ask your question…", key="chat_input")
|
| 90 |
with c2:
|
| 91 |
send = st.button("🚀")
|
| 92 |
|
|
|
|
| 93 |
if send and question:
|
| 94 |
bot = CodeAssistantBot()
|
| 95 |
history = st.session_state.conversation[-4:]
|
|
@@ -98,18 +103,30 @@ def render_chatbot(code, output, error):
|
|
| 98 |
response = bot.analyze_code(code, output, error, question, summary, history)
|
| 99 |
|
| 100 |
st.session_state.conversation.append((question, response))
|
|
|
|
| 101 |
|
| 102 |
-
#
|
| 103 |
if len(st.session_state.conversation) >= 3:
|
| 104 |
-
full_chat = "\n".join([f"User: {q}\nBot: {a}" for q, a in st.session_state.conversation[-10:]])
|
| 105 |
try:
|
|
|
|
| 106 |
parser = StrOutputParser()
|
| 107 |
summarizer = bot.summary_prompt | bot.model | parser
|
| 108 |
st.session_state.chat_summary = summarizer.invoke({'conversation': full_chat})
|
| 109 |
except:
|
| 110 |
-
pass #
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
|
| 114 |
|
| 115 |
def format_response(text):
|
|
@@ -129,8 +146,7 @@ def render_chatbot(code, output, error):
|
|
| 129 |
formatted = format_response(a)
|
| 130 |
st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
st.markdown("""
|
| 135 |
<script>
|
| 136 |
const c = window.parent.document.querySelector('.chat-container');
|
|
|
|
| 15 |
self.client = Groq(api_key=GROQ_API_KEY)
|
| 16 |
self.model = ChatGroq(model="llama-3.3-70b-versatile", temperature=0.6)
|
| 17 |
self.analysis_prompt = ChatPromptTemplate.from_messages([
|
| 18 |
+
("system",
|
| 19 |
"You are a skilled coding assistant. Use the following context and user input to help."
|
| 20 |
+
" Refer to previous summary and recent interactions to make answers accurate."
|
| 21 |
+
" Keep your response short, relevant, and conversational — smaller is better if it covers what's needed."),
|
| 22 |
+
("user",
|
| 23 |
"Code: {code}\nOutput: {output}\nError: {error}\n"
|
| 24 |
"Summary: {summary}\nRecent: {recent}\nQuestion: {question}")
|
| 25 |
])
|
|
|
|
| 51 |
def render_chatbot(code, output, error):
|
| 52 |
"""Render the chatbot UI with code-block support and a scrollable chat container."""
|
| 53 |
|
| 54 |
+
# Style
|
| 55 |
st.markdown("""
|
| 56 |
<style>
|
| 57 |
.chat-container {
|
|
|
|
| 81 |
</style>
|
| 82 |
""", unsafe_allow_html=True)
|
| 83 |
|
| 84 |
+
# Session state setup
|
| 85 |
st.session_state.setdefault('conversation', [])
|
| 86 |
st.session_state.setdefault('audio_count', 0)
|
| 87 |
st.session_state.setdefault('chat_summary', "")
|
| 88 |
+
st.session_state.setdefault('chat_display_count', 5)
|
| 89 |
|
| 90 |
+
# Input UI
|
| 91 |
c1, c2 = st.columns([4, 1], gap='small')
|
| 92 |
with c1:
|
| 93 |
question = st.text_input("Ask your question…", key="chat_input")
|
| 94 |
with c2:
|
| 95 |
send = st.button("🚀")
|
| 96 |
|
| 97 |
+
# Process question
|
| 98 |
if send and question:
|
| 99 |
bot = CodeAssistantBot()
|
| 100 |
history = st.session_state.conversation[-4:]
|
|
|
|
| 103 |
response = bot.analyze_code(code, output, error, question, summary, history)
|
| 104 |
|
| 105 |
st.session_state.conversation.append((question, response))
|
| 106 |
+
st.session_state.chat_display_count = 5 # Reset visible count for new message
|
| 107 |
|
| 108 |
+
# Update summary (based on last 10 entries)
|
| 109 |
if len(st.session_state.conversation) >= 3:
|
|
|
|
| 110 |
try:
|
| 111 |
+
full_chat = "\n".join([f"User: {q}\nBot: {a}" for q, a in st.session_state.conversation[-10:]])
|
| 112 |
parser = StrOutputParser()
|
| 113 |
summarizer = bot.summary_prompt | bot.model | parser
|
| 114 |
st.session_state.chat_summary = summarizer.invoke({'conversation': full_chat})
|
| 115 |
except:
|
| 116 |
+
pass # Don't fail on summary issues
|
| 117 |
|
| 118 |
+
# Display conversation (newest at top, paginated)
|
| 119 |
+
total_messages = len(st.session_state.conversation)
|
| 120 |
+
start_idx = max(0, total_messages - st.session_state.chat_display_count)
|
| 121 |
+
visible_conversation = st.session_state.conversation[start_idx:]
|
| 122 |
+
|
| 123 |
+
if start_idx > 0:
|
| 124 |
+
if st.button("🔽 Show more"):
|
| 125 |
+
st.session_state.chat_display_count += 5
|
| 126 |
+
st.experimental_rerun()
|
| 127 |
+
|
| 128 |
+
# Newest first
|
| 129 |
+
for q, a in reversed(visible_conversation):
|
| 130 |
st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
|
| 131 |
|
| 132 |
def format_response(text):
|
|
|
|
| 146 |
formatted = format_response(a)
|
| 147 |
st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
|
| 148 |
|
| 149 |
+
# Auto-scroll (optional)
|
|
|
|
| 150 |
st.markdown("""
|
| 151 |
<script>
|
| 152 |
const c = window.parent.document.querySelector('.chat-container');
|