vsj0702 commited on
Commit
f4f6df9
·
verified ·
1 Parent(s): c913bf6

Update chatbot.py

Browse files
Files changed (1) hide show
  1. 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
- """Render the chatbot UI in a fixed-height, scrollable panel"""
70
 
71
- # Initialize session state
72
- if "conversation" not in st.session_state:
73
- st.session_state.conversation = []
74
- if "audio_count" not in st.session_state:
75
- st.session_state.audio_count = 0
76
 
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;
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
- # 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
-
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
- <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
 
 
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