Spaces:
Sleeping
Sleeping
Commit
·
9abd2f2
1
Parent(s):
cde7d1a
change for AI thinking
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
@@ -25,10 +27,9 @@ def respond(
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
full_response = ""
|
30 |
-
|
31 |
-
generation_complete = False
|
32 |
|
33 |
# Use chat completion instead of text generation
|
34 |
for message in client.chat_completion(
|
@@ -41,19 +42,26 @@ def respond(
|
|
41 |
token = message.choices[0].delta.content
|
42 |
if token:
|
43 |
full_response += token
|
44 |
-
|
45 |
-
# Check if generation is complete
|
46 |
-
if message.choices[0].finish_reason == "stop" or not token:
|
47 |
-
generation_complete = True
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
|
59 |
# Custom CSS for Plant Wisdom.AI styling
|
@@ -166,6 +174,50 @@ custom_css = """
|
|
166 |
border-radius: 12px;
|
167 |
background-color: #ffffff;
|
168 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
"""
|
170 |
|
171 |
"""
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import time
|
4 |
+
import html
|
5 |
|
6 |
"""
|
7 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
|
27 |
|
28 |
messages.append({"role": "user", "content": message})
|
29 |
|
30 |
+
thinking_steps = []
|
31 |
full_response = ""
|
32 |
+
start_time = time.time()
|
|
|
33 |
|
34 |
# Use chat completion instead of text generation
|
35 |
for message in client.chat_completion(
|
|
|
42 |
token = message.choices[0].delta.content
|
43 |
if token:
|
44 |
full_response += token
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Save thinking steps every 2 seconds or every 100 characters
|
47 |
+
current_time = time.time()
|
48 |
+
if current_time - start_time > 2 or len(full_response) % 100 == 0:
|
49 |
+
start_time = current_time
|
50 |
+
thinking_steps.append(full_response)
|
51 |
+
|
52 |
+
# Format with thinking history as HTML
|
53 |
+
if thinking_steps:
|
54 |
+
thinking_html = '<div class="thinking-wrapper"><details><summary>Show thinking process</summary><div class="thinking-steps">'
|
55 |
+
for i, step in enumerate(thinking_steps):
|
56 |
+
# Escape HTML to prevent rendering issues
|
57 |
+
safe_step = html.escape(step)
|
58 |
+
thinking_html += f'<div class="thinking-step">Step {i+1}: {safe_step}</div>'
|
59 |
+
thinking_html += '</div></details></div>'
|
60 |
+
|
61 |
+
# Yield both thinking and current response
|
62 |
+
yield f"{thinking_html}{full_response}"
|
63 |
+
else:
|
64 |
+
yield full_response
|
65 |
|
66 |
|
67 |
# Custom CSS for Plant Wisdom.AI styling
|
|
|
174 |
border-radius: 12px;
|
175 |
background-color: #ffffff;
|
176 |
}
|
177 |
+
|
178 |
+
/* Thinking process styling */
|
179 |
+
.thinking-wrapper {
|
180 |
+
margin-bottom: 12px;
|
181 |
+
}
|
182 |
+
|
183 |
+
details {
|
184 |
+
background-color: #f8faf8;
|
185 |
+
border: 1px solid #e0ede0;
|
186 |
+
border-radius: 8px;
|
187 |
+
padding: 8px;
|
188 |
+
margin-bottom: 16px;
|
189 |
+
}
|
190 |
+
|
191 |
+
summary {
|
192 |
+
cursor: pointer;
|
193 |
+
color: #2e7d32;
|
194 |
+
font-weight: 500;
|
195 |
+
padding: 4px 8px;
|
196 |
+
}
|
197 |
+
|
198 |
+
summary:hover {
|
199 |
+
background-color: rgba(46,125,50,0.1);
|
200 |
+
border-radius: 4px;
|
201 |
+
}
|
202 |
+
|
203 |
+
.thinking-steps {
|
204 |
+
margin-top: 8px;
|
205 |
+
padding: 8px;
|
206 |
+
border-top: 1px solid #e0ede0;
|
207 |
+
max-height: 200px;
|
208 |
+
overflow-y: auto;
|
209 |
+
}
|
210 |
+
|
211 |
+
.thinking-step {
|
212 |
+
padding: 4px 8px;
|
213 |
+
font-size: 14px;
|
214 |
+
color: #666;
|
215 |
+
border-bottom: 1px dashed #eee;
|
216 |
+
}
|
217 |
+
|
218 |
+
.thinking-step:last-child {
|
219 |
+
border-bottom: none;
|
220 |
+
}
|
221 |
"""
|
222 |
|
223 |
"""
|