Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,21 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
@app.get("/")
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
data = await request.json()
|
| 16 |
-
prompt = data.get("prompt", "")
|
| 17 |
-
response = generator(prompt, max_length=200, do_sample=True)
|
| 18 |
-
return {"response": response[0]['generated_text']}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from duckduckgo_search import DDGS
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# Enable CORS for all origins (allows API to be used from any website)
|
| 8 |
+
app.add_middleware(
|
| 9 |
+
CORSMiddleware,
|
| 10 |
+
allow_origins=["*"],
|
| 11 |
+
allow_credentials=True,
|
| 12 |
+
allow_methods=["*"],
|
| 13 |
+
allow_headers=["*"],
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
@app.get("/search")
|
| 17 |
+
def search(query: str = Query(..., title="Search Query"), max_results: int = 10):
|
| 18 |
+
""" DuckDuckGo Search API - Returns top search results. """
|
| 19 |
+
with DDGS() as ddgs:
|
| 20 |
+
results = list(ddgs.text(query, max_results=max_results))
|
| 21 |
+
return {"query": query, "results": results}
|
|
|
|
|
|
|
|
|
|
|
|