Spaces:
Running
Running
fix: update to ddgs package and use FastAPI lifespan events
Browse files- Replace deprecated duckduckgo-search with ddgs package
- Replace deprecated @app .on_event with lifespan context manager
- Fixes deprecation warnings in HF Spaces deployment
- requirements.txt +1 -1
- src/infrastructure/adapters/duckduckgo_adapter.py +8 -3
- src/main.py +17 -9
requirements.txt
CHANGED
|
@@ -22,5 +22,5 @@ passlib[bcrypt]>=1.7.4
|
|
| 22 |
python-multipart>=0.0.9
|
| 23 |
httpx>=0.27.0
|
| 24 |
aiohttp>=3.9.0
|
| 25 |
-
|
| 26 |
python-dateutil>=2.8.2 # Date parsing for live results
|
|
|
|
| 22 |
python-multipart>=0.0.9
|
| 23 |
httpx>=0.27.0
|
| 24 |
aiohttp>=3.9.0
|
| 25 |
+
ddgs>=1.0.0 # Live search for hybrid RAG (renamed from duckduckgo-search)
|
| 26 |
python-dateutil>=2.8.2 # Date parsing for live results
|
src/infrastructure/adapters/duckduckgo_adapter.py
CHANGED
|
@@ -21,11 +21,16 @@ import traceback
|
|
| 21 |
logger = logging.getLogger(__name__)
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
from
|
| 25 |
HAS_DDGS = True
|
| 26 |
except ImportError:
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
class DuckDuckGoAdapter:
|
|
|
|
| 21 |
logger = logging.getLogger(__name__)
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
from ddgs import DDGS
|
| 25 |
HAS_DDGS = True
|
| 26 |
except ImportError:
|
| 27 |
+
# Fallback to old package name for backward compatibility
|
| 28 |
+
try:
|
| 29 |
+
from duckduckgo_search import DDGS
|
| 30 |
+
HAS_DDGS = True
|
| 31 |
+
except ImportError:
|
| 32 |
+
HAS_DDGS = False
|
| 33 |
+
logger.warning("ddgs (duckduckgo-search) not installed. Live search disabled.")
|
| 34 |
|
| 35 |
|
| 36 |
class DuckDuckGoAdapter:
|
src/main.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
from pathlib import Path
|
|
|
|
| 4 |
|
| 5 |
# Add project root and local src to path to allow direct execution
|
| 6 |
current_dir = Path(__file__).resolve().parent
|
|
@@ -17,9 +18,24 @@ from src.api.dependencies import prewarm_models
|
|
| 17 |
import threading
|
| 18 |
import uvicorn
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
app = FastAPI(
|
| 21 |
title=settings.PROJECT_NAME,
|
| 22 |
-
openapi_url=f"{settings.API_V1_STR}/openapi.json"
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
# Set all CORS enabled origins
|
|
@@ -31,14 +47,6 @@ app.add_middleware(
|
|
| 31 |
allow_headers=["*"],
|
| 32 |
)
|
| 33 |
|
| 34 |
-
@app.on_event("startup")
|
| 35 |
-
def on_startup():
|
| 36 |
-
init_db()
|
| 37 |
-
# Pre-warming moved to lazy loading or manual script to prevent
|
| 38 |
-
# initialization timeouts/crashes on the first run.
|
| 39 |
-
# thread = threading.Thread(target=prewarm_models)
|
| 40 |
-
# thread.start()
|
| 41 |
-
|
| 42 |
app.include_router(rag.router, prefix=f"{settings.API_V1_STR}/rag", tags=["RAG"])
|
| 43 |
app.include_router(analytics.router, prefix=f"{settings.API_V1_STR}/analytics", tags=["Analytics"])
|
| 44 |
app.include_router(interactions.router, prefix=f"{settings.API_V1_STR}/interactions", tags=["Interactions"])
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
from pathlib import Path
|
| 4 |
+
from contextlib import asynccontextmanager
|
| 5 |
|
| 6 |
# Add project root and local src to path to allow direct execution
|
| 7 |
current_dir = Path(__file__).resolve().parent
|
|
|
|
| 18 |
import threading
|
| 19 |
import uvicorn
|
| 20 |
|
| 21 |
+
|
| 22 |
+
@asynccontextmanager
|
| 23 |
+
async def lifespan(app: FastAPI):
|
| 24 |
+
# Startup
|
| 25 |
+
init_db()
|
| 26 |
+
# Pre-warming moved to lazy loading or manual script to prevent
|
| 27 |
+
# initialization timeouts/crashes on the first run.
|
| 28 |
+
# thread = threading.Thread(target=prewarm_models)
|
| 29 |
+
# thread.start()
|
| 30 |
+
yield
|
| 31 |
+
# Shutdown (if needed)
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
|
| 35 |
app = FastAPI(
|
| 36 |
title=settings.PROJECT_NAME,
|
| 37 |
+
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
| 38 |
+
lifespan=lifespan
|
| 39 |
)
|
| 40 |
|
| 41 |
# Set all CORS enabled origins
|
|
|
|
| 47 |
allow_headers=["*"],
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
app.include_router(rag.router, prefix=f"{settings.API_V1_STR}/rag", tags=["RAG"])
|
| 51 |
app.include_router(analytics.router, prefix=f"{settings.API_V1_STR}/analytics", tags=["Analytics"])
|
| 52 |
app.include_router(interactions.router, prefix=f"{settings.API_V1_STR}/interactions", tags=["Interactions"])
|