Spaces:
Runtime error
Runtime error
File size: 661 Bytes
11ee355 10da29a 11ee355 10da29a 11ee355 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
from duckduckgo_search import DDGS
app = FastAPI()
# Enable CORS for all origins (allows API to be used from any website)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/search")
def search(query: str = Query(..., title="Search Query"), max_results: int = 10):
""" DuckDuckGo Search API - Returns top search results. """
with DDGS() as ddgs:
results = list(ddgs.text(query, max_results=max_results))
return {"query": query, "results": results}
|