Spaces:
Sleeping
Sleeping
File size: 880 Bytes
cd20a25 67654d1 cd20a25 cccf5d6 cd20a25 cccf5d6 cd20a25 cccf5d6 cd20a25 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
from fastapi import FastAPI, Query, HTTPException
app = FastAPI()
@app.get("/search")
def search(query: str, n: int = Query(5, ge=1)):
"""
Endpoint for querying the search engine.
Args:
query (str): The search query.
n (int): Number of results to return (default: 5).
Returns:
dict: Query results.
"""
if not query.strip():
raise HTTPException(status_code=400, detail="Query cannot be empty.")
search_engine = app.state.search_engine
if not search_engine:
raise HTTPException(status_code=500, detail="Search engine not initialized.")
try:
results = search_engine.most_similar(query, n)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"An unexpected error occurred: {str(e)}"
)
return {"query": query, "results": results}
|