suppress asyncio cleanup warnings from DDGS library
Browse filesThe DuckDuckGo search library creates asyncio event loops that
produce harmless cleanup warnings. Added warnings filter and
simplified DDGS usage.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import time
|
|
| 3 |
import gc
|
| 4 |
import sys
|
| 5 |
import threading
|
|
|
|
| 6 |
from itertools import islice
|
| 7 |
from datetime import datetime
|
| 8 |
import re # for parsing <think> blocks
|
|
@@ -16,6 +17,9 @@ from torch.utils._pytree import tree_map
|
|
| 16 |
import numpy as np
|
| 17 |
from pocket_tts import TTSModel
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Global event to signal cancellation from the UI thread to the generation thread
|
| 20 |
cancel_event = threading.Event()
|
| 21 |
|
|
@@ -440,9 +444,10 @@ def retrieve_context(query, max_results=6, max_chars=50):
|
|
| 440 |
Returns a list of result strings.
|
| 441 |
"""
|
| 442 |
try:
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
| 446 |
except Exception:
|
| 447 |
return []
|
| 448 |
|
|
|
|
| 3 |
import gc
|
| 4 |
import sys
|
| 5 |
import threading
|
| 6 |
+
import warnings
|
| 7 |
from itertools import islice
|
| 8 |
from datetime import datetime
|
| 9 |
import re # for parsing <think> blocks
|
|
|
|
| 17 |
import numpy as np
|
| 18 |
from pocket_tts import TTSModel
|
| 19 |
|
| 20 |
+
# Suppress asyncio event loop cleanup warnings (harmless, from DDGS library)
|
| 21 |
+
warnings.filterwarnings("ignore", message=".*Invalid file descriptor.*")
|
| 22 |
+
|
| 23 |
# Global event to signal cancellation from the UI thread to the generation thread
|
| 24 |
cancel_event = threading.Event()
|
| 25 |
|
|
|
|
| 444 |
Returns a list of result strings.
|
| 445 |
"""
|
| 446 |
try:
|
| 447 |
+
ddgs = DDGS()
|
| 448 |
+
results = list(islice(ddgs.text(query, region="wt-wt", safesearch="off", timelimit="y"), max_results))
|
| 449 |
+
return [f"{i+1}. {r.get('title','No Title')} - {r.get('body','')[:max_chars]}"
|
| 450 |
+
for i, r in enumerate(results)]
|
| 451 |
except Exception:
|
| 452 |
return []
|
| 453 |
|