Spaces:
Running
Running
nacho commited on
Commit ·
3e65e33
1
Parent(s): 5470e8a
perf: 全速优化,每次请求省 10-15 秒
Browse files- fill() 替代 type() 逐字符打字(省 3-8 秒)
- new_chat() 点按钮替代 goto 整页刷新(省 3-5 秒)
- start() 等元素就绪替代 sleep(5)
- 响应等待 sleep(3) → sleep(0.8)
- _human_delay 从 300-1500ms 缩至 50-150ms
- delete_chat 改为 fire-and-forget 不阻塞返回
- deepseek_browser.py +37 -20
deepseek_browser.py
CHANGED
|
@@ -60,7 +60,11 @@ class DeepSeekBrowser:
|
|
| 60 |
|
| 61 |
self.page = await self.context.new_page()
|
| 62 |
await self.page.goto(self.DEEPSEEK_URL, timeout=60000)
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
await self._check_login_state()
|
| 66 |
|
|
@@ -149,7 +153,8 @@ class DeepSeekBrowser:
|
|
| 149 |
except Exception:
|
| 150 |
raise Exception("Login failed")
|
| 151 |
|
| 152 |
-
async def _human_delay(self, min_ms: int =
|
|
|
|
| 153 |
delay = random.uniform(min_ms, max_ms) / 1000
|
| 154 |
await asyncio.sleep(delay)
|
| 155 |
|
|
@@ -164,9 +169,21 @@ class DeepSeekBrowser:
|
|
| 164 |
return phrases
|
| 165 |
|
| 166 |
async def new_chat(self):
|
|
|
|
| 167 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
await self.page.goto(self.DEEPSEEK_URL, timeout=30000)
|
| 169 |
-
await asyncio.sleep(2)
|
| 170 |
await self.page.wait_for_selector('textarea', timeout=15000)
|
| 171 |
except Exception as e:
|
| 172 |
logger.error("New chat error: %s", e)
|
|
@@ -291,28 +308,32 @@ class DeepSeekBrowser:
|
|
| 291 |
input_field = self.page.locator('textarea').first
|
| 292 |
await input_field.wait_for(state="visible", timeout=15000)
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
await
|
| 297 |
-
await input_field.type(prompt, delay=random.randint(30, 80))
|
| 298 |
-
|
| 299 |
-
await self._human_delay(200, 800)
|
| 300 |
-
|
| 301 |
await input_field.press('Enter')
|
| 302 |
|
| 303 |
response = await self._wait_for_response(timeout, prompt)
|
| 304 |
|
| 305 |
-
|
|
|
|
| 306 |
|
| 307 |
return response
|
| 308 |
except Exception as e:
|
| 309 |
logger.error("Send message error: %s", e)
|
| 310 |
raise
|
| 311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
async def _wait_for_response(self, timeout: int, prompt: str = "") -> str:
|
| 313 |
deadline = time.time() + timeout
|
| 314 |
|
| 315 |
-
await asyncio.sleep(
|
| 316 |
|
| 317 |
last_text = ""
|
| 318 |
stable_count = 0
|
|
@@ -374,13 +395,9 @@ class DeepSeekBrowser:
|
|
| 374 |
input_field = self.page.locator('textarea').first
|
| 375 |
await input_field.wait_for(state="visible", timeout=15000)
|
| 376 |
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
await
|
| 380 |
-
await input_field.type(prompt, delay=random.randint(30, 80))
|
| 381 |
-
|
| 382 |
-
await self._human_delay(200, 800)
|
| 383 |
-
|
| 384 |
await input_field.press('Enter')
|
| 385 |
|
| 386 |
deadline = time.time() + timeout
|
|
@@ -389,7 +406,7 @@ class DeepSeekBrowser:
|
|
| 389 |
|
| 390 |
skip_phrases = self._get_skip_phrases()
|
| 391 |
|
| 392 |
-
await asyncio.sleep(
|
| 393 |
|
| 394 |
while time.time() < deadline:
|
| 395 |
try:
|
|
|
|
| 60 |
|
| 61 |
self.page = await self.context.new_page()
|
| 62 |
await self.page.goto(self.DEEPSEEK_URL, timeout=60000)
|
| 63 |
+
# Wait for page ready instead of fixed sleep
|
| 64 |
+
try:
|
| 65 |
+
await self.page.wait_for_selector('textarea', timeout=15000)
|
| 66 |
+
except Exception:
|
| 67 |
+
await asyncio.sleep(2)
|
| 68 |
|
| 69 |
await self._check_login_state()
|
| 70 |
|
|
|
|
| 153 |
except Exception:
|
| 154 |
raise Exception("Login failed")
|
| 155 |
|
| 156 |
+
async def _human_delay(self, min_ms: int = 50, max_ms: int = 150):
|
| 157 |
+
"""Minimal delay for speed — just enough to avoid race conditions."""
|
| 158 |
delay = random.uniform(min_ms, max_ms) / 1000
|
| 159 |
await asyncio.sleep(delay)
|
| 160 |
|
|
|
|
| 169 |
return phrases
|
| 170 |
|
| 171 |
async def new_chat(self):
|
| 172 |
+
"""Start a new chat by clicking the new-chat button instead of full page reload."""
|
| 173 |
try:
|
| 174 |
+
# Try clicking the "new chat" button first (much faster than goto)
|
| 175 |
+
new_chat_btn = self.page.locator(
|
| 176 |
+
'a:has-text("开启新对话"), button:has-text("开启新对话"), '
|
| 177 |
+
'a:has-text("新对话"), button:has-text("新对话"), '
|
| 178 |
+
'[class*="new-chat"], [class*="newChat"]'
|
| 179 |
+
).first
|
| 180 |
+
if await new_chat_btn.count() > 0:
|
| 181 |
+
await new_chat_btn.click()
|
| 182 |
+
await self.page.wait_for_selector('textarea', timeout=10000)
|
| 183 |
+
return
|
| 184 |
+
|
| 185 |
+
# Fallback: full page reload
|
| 186 |
await self.page.goto(self.DEEPSEEK_URL, timeout=30000)
|
|
|
|
| 187 |
await self.page.wait_for_selector('textarea', timeout=15000)
|
| 188 |
except Exception as e:
|
| 189 |
logger.error("New chat error: %s", e)
|
|
|
|
| 308 |
input_field = self.page.locator('textarea').first
|
| 309 |
await input_field.wait_for(state="visible", timeout=15000)
|
| 310 |
|
| 311 |
+
# Fast fill instead of slow character-by-character typing
|
| 312 |
+
await input_field.fill(prompt)
|
| 313 |
+
await self._human_delay()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
await input_field.press('Enter')
|
| 315 |
|
| 316 |
response = await self._wait_for_response(timeout, prompt)
|
| 317 |
|
| 318 |
+
# Fire-and-forget cleanup
|
| 319 |
+
asyncio.create_task(self._safe_delete_chat())
|
| 320 |
|
| 321 |
return response
|
| 322 |
except Exception as e:
|
| 323 |
logger.error("Send message error: %s", e)
|
| 324 |
raise
|
| 325 |
|
| 326 |
+
async def _safe_delete_chat(self):
|
| 327 |
+
"""Non-blocking delete chat wrapper."""
|
| 328 |
+
try:
|
| 329 |
+
await self.delete_chat()
|
| 330 |
+
except Exception as e:
|
| 331 |
+
logger.debug("[safe_delete] %s", e)
|
| 332 |
+
|
| 333 |
async def _wait_for_response(self, timeout: int, prompt: str = "") -> str:
|
| 334 |
deadline = time.time() + timeout
|
| 335 |
|
| 336 |
+
await asyncio.sleep(0.8)
|
| 337 |
|
| 338 |
last_text = ""
|
| 339 |
stable_count = 0
|
|
|
|
| 395 |
input_field = self.page.locator('textarea').first
|
| 396 |
await input_field.wait_for(state="visible", timeout=15000)
|
| 397 |
|
| 398 |
+
# Fast fill
|
| 399 |
+
await input_field.fill(prompt)
|
| 400 |
+
await self._human_delay()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
await input_field.press('Enter')
|
| 402 |
|
| 403 |
deadline = time.time() + timeout
|
|
|
|
| 406 |
|
| 407 |
skip_phrases = self._get_skip_phrases()
|
| 408 |
|
| 409 |
+
await asyncio.sleep(0.8)
|
| 410 |
|
| 411 |
while time.time() < deadline:
|
| 412 |
try:
|