vsj0702 commited on
Commit
474cc40
·
verified ·
1 Parent(s): 87a51c6
Files changed (1) hide show
  1. 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
- def render_chatbot(code, output, error):
45
- """Render the chatbot UI with code-block support and no deprecated APIs."""
46
- + # ---- begin: make chat area its own scrollable box ----
47
- + st.markdown("""
48
- + <style>
49
- + .chat-container {
50
- + max-height: 60vh; /* or a fixed px value like 400px */
51
- + overflow-y: auto;
52
- + padding-right: 0.5rem; /* prevent text clipping */
53
- + }
54
- + </style>
55
- + """, unsafe_allow_html=True)
56
- + # ---- end: scrollable chat CSS ----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- st.session_state.setdefault('conversation', [])
59
- st.session_state.setdefault('audio_count', 0)
60
-
61
- # Chat container
62
- st.markdown('<div class="chat-container">', unsafe_allow_html=True)
63
-
64
- st.markdown('</div>', unsafe_allow_html=True)
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)