Spaces:
Sleeping
Sleeping
Commit
·
edd54ad
1
Parent(s):
d18bd01
change for AI thinking
Browse files
app.py
CHANGED
@@ -40,11 +40,9 @@ def respond(
|
|
40 |
if token:
|
41 |
response += token
|
42 |
# Store intermediate responses in thinking process
|
43 |
-
if len(response) %
|
44 |
thinking_process.append(response)
|
45 |
-
|
46 |
-
if message.choices[0].finish_reason == "stop":
|
47 |
-
yield response
|
48 |
|
49 |
|
50 |
# Custom CSS for Plant Wisdom.AI styling
|
@@ -69,7 +67,6 @@ custom_css = """
|
|
69 |
margin: 12px 0;
|
70 |
font-size: 16px;
|
71 |
line-height: 1.5;
|
72 |
-
position: relative;
|
73 |
}
|
74 |
|
75 |
.message.user {
|
@@ -85,58 +82,6 @@ custom_css = """
|
|
85 |
color: #2c3338;
|
86 |
}
|
87 |
|
88 |
-
.message.in-progress {
|
89 |
-
opacity: 0.7;
|
90 |
-
}
|
91 |
-
|
92 |
-
.thinking-indicator {
|
93 |
-
position: absolute;
|
94 |
-
top: 8px;
|
95 |
-
right: 8px;
|
96 |
-
font-size: 12px;
|
97 |
-
color: #666;
|
98 |
-
background: rgba(0,0,0,0.05);
|
99 |
-
padding: 4px 8px;
|
100 |
-
border-radius: 4px;
|
101 |
-
display: none;
|
102 |
-
}
|
103 |
-
|
104 |
-
.message.in-progress .thinking-indicator {
|
105 |
-
display: block;
|
106 |
-
}
|
107 |
-
|
108 |
-
.thinking-process {
|
109 |
-
display: none;
|
110 |
-
margin-top: 8px;
|
111 |
-
padding: 8px;
|
112 |
-
background: #f8f9fa;
|
113 |
-
border-radius: 4px;
|
114 |
-
font-size: 14px;
|
115 |
-
color: #666;
|
116 |
-
}
|
117 |
-
|
118 |
-
.show-thinking .thinking-process {
|
119 |
-
display: block;
|
120 |
-
}
|
121 |
-
|
122 |
-
.toggle-thinking {
|
123 |
-
position: absolute;
|
124 |
-
bottom: 8px;
|
125 |
-
right: 8px;
|
126 |
-
background: none;
|
127 |
-
border: none;
|
128 |
-
color: #2e7d32;
|
129 |
-
cursor: pointer;
|
130 |
-
font-size: 12px;
|
131 |
-
padding: 4px 8px;
|
132 |
-
border-radius: 4px;
|
133 |
-
transition: all 0.2s ease;
|
134 |
-
}
|
135 |
-
|
136 |
-
.toggle-thinking:hover {
|
137 |
-
background: rgba(46,125,50,0.1);
|
138 |
-
}
|
139 |
-
|
140 |
.submit-btn {
|
141 |
background-color: #2e7d32 !important;
|
142 |
color: white !important;
|
@@ -209,6 +154,41 @@ custom_css = """
|
|
209 |
border-radius: 12px;
|
210 |
background-color: #ffffff;
|
211 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
"""
|
213 |
|
214 |
"""
|
@@ -252,26 +232,43 @@ demo = gr.ChatInterface(
|
|
252 |
),
|
253 |
css=custom_css,
|
254 |
js="""
|
255 |
-
function
|
256 |
-
|
257 |
-
|
258 |
-
|
|
|
259 |
const btn = document.createElement('button');
|
260 |
-
btn.className = 'toggle-thinking';
|
261 |
-
btn.textContent = 'Show thinking';
|
262 |
-
btn.onclick = function() {
|
|
|
263 |
msg.classList.toggle('show-thinking');
|
264 |
-
btn.textContent = msg.classList.contains('show-thinking')
|
|
|
|
|
265 |
};
|
266 |
msg.appendChild(btn);
|
267 |
}
|
268 |
});
|
269 |
}
|
270 |
-
|
271 |
-
//
|
272 |
-
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
""",
|
276 |
additional_inputs=[
|
277 |
gr.Textbox(
|
|
|
40 |
if token:
|
41 |
response += token
|
42 |
# Store intermediate responses in thinking process
|
43 |
+
if len(response) % 100 == 0: # Increased to 100 characters to reduce frequency
|
44 |
thinking_process.append(response)
|
45 |
+
yield f"{response}<thinking>{','.join(thinking_process)}</thinking>"
|
|
|
|
|
46 |
|
47 |
|
48 |
# Custom CSS for Plant Wisdom.AI styling
|
|
|
67 |
margin: 12px 0;
|
68 |
font-size: 16px;
|
69 |
line-height: 1.5;
|
|
|
70 |
}
|
71 |
|
72 |
.message.user {
|
|
|
82 |
color: #2c3338;
|
83 |
}
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
.submit-btn {
|
86 |
background-color: #2e7d32 !important;
|
87 |
color: white !important;
|
|
|
154 |
border-radius: 12px;
|
155 |
background-color: #ffffff;
|
156 |
}
|
157 |
+
|
158 |
+
.message thinking {
|
159 |
+
display: none;
|
160 |
+
}
|
161 |
+
|
162 |
+
.message.show-thinking thinking {
|
163 |
+
display: block;
|
164 |
+
margin-top: 8px;
|
165 |
+
padding: 8px;
|
166 |
+
background: #f8f9fa;
|
167 |
+
border-radius: 4px;
|
168 |
+
font-size: 14px;
|
169 |
+
color: #666;
|
170 |
+
border: 1px solid #e0ede0;
|
171 |
+
}
|
172 |
+
|
173 |
+
.toggle-thinking-btn {
|
174 |
+
position: absolute;
|
175 |
+
bottom: 8px;
|
176 |
+
right: 8px;
|
177 |
+
background: none;
|
178 |
+
border: none;
|
179 |
+
color: #2e7d32;
|
180 |
+
cursor: pointer;
|
181 |
+
font-size: 12px;
|
182 |
+
padding: 4px 8px;
|
183 |
+
border-radius: 4px;
|
184 |
+
opacity: 0.7;
|
185 |
+
transition: all 0.2s ease;
|
186 |
+
}
|
187 |
+
|
188 |
+
.toggle-thinking-btn:hover {
|
189 |
+
opacity: 1;
|
190 |
+
background: rgba(46,125,50,0.1);
|
191 |
+
}
|
192 |
"""
|
193 |
|
194 |
"""
|
|
|
232 |
),
|
233 |
css=custom_css,
|
234 |
js="""
|
235 |
+
function addThinkingButtons() {
|
236 |
+
document.querySelectorAll('.message.assistant').forEach(msg => {
|
237 |
+
// Only add button if message has thinking content and doesn't already have a button
|
238 |
+
const thinking = msg.querySelector('thinking');
|
239 |
+
if (thinking && !msg.querySelector('.toggle-thinking-btn')) {
|
240 |
const btn = document.createElement('button');
|
241 |
+
btn.className = 'toggle-thinking-btn';
|
242 |
+
btn.textContent = 'Show thinking process';
|
243 |
+
btn.onclick = function(e) {
|
244 |
+
e.preventDefault();
|
245 |
msg.classList.toggle('show-thinking');
|
246 |
+
btn.textContent = msg.classList.contains('show-thinking')
|
247 |
+
? 'Hide thinking process'
|
248 |
+
: 'Show thinking process';
|
249 |
};
|
250 |
msg.appendChild(btn);
|
251 |
}
|
252 |
});
|
253 |
}
|
254 |
+
|
255 |
+
// Add buttons when new messages appear
|
256 |
+
const observer = new MutationObserver((mutations) => {
|
257 |
+
mutations.forEach((mutation) => {
|
258 |
+
if (mutation.addedNodes.length) {
|
259 |
+
addThinkingButtons();
|
260 |
+
}
|
261 |
+
});
|
262 |
+
});
|
263 |
+
|
264 |
+
// Start observing after the page loads
|
265 |
+
document.addEventListener('DOMContentLoaded', () => {
|
266 |
+
observer.observe(document.body, {
|
267 |
+
childList: true,
|
268 |
+
subtree: true
|
269 |
+
});
|
270 |
+
addThinkingButtons();
|
271 |
+
});
|
272 |
""",
|
273 |
additional_inputs=[
|
274 |
gr.Textbox(
|