Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,59 @@
|
|
1 |
-
from
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
import os
|
4 |
from webscout import WEBS, transcriber
|
5 |
|
6 |
-
app =
|
7 |
|
8 |
BASE_URL = "https://oevortex-webscout-api.hf.space"
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
try:
|
23 |
with WEBS() as webs:
|
24 |
results = webs.text(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, backend=backend, max_results=max_results)
|
25 |
-
# Extract data you want to display
|
26 |
search_results = [
|
27 |
{
|
28 |
'title': result.title,
|
29 |
'description': result.description
|
30 |
} for result in results
|
31 |
]
|
32 |
-
return
|
33 |
except Exception as e:
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if __name__ == "__main__":
|
37 |
-
|
38 |
-
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.encoders import jsonable_encoder
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
+
from fastapi.templating import Jinja2Templates
|
6 |
import requests
|
7 |
import os
|
8 |
from webscout import WEBS, transcriber
|
9 |
|
10 |
+
app = FastAPI()
|
11 |
|
12 |
BASE_URL = "https://oevortex-webscout-api.hf.space"
|
13 |
|
14 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
15 |
+
templates = Jinja2Templates(directory="templates")
|
16 |
+
|
17 |
+
@app.get("/", response_class=HTMLResponse)
|
18 |
+
async def index(request: Request):
|
19 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
20 |
+
|
21 |
+
@app.get("/api/suggestions")
|
22 |
+
async def get_suggestions(q: str):
|
23 |
+
api_endpoint = f"{BASE_URL}/api/suggestions?q={q}"
|
24 |
+
response = requests.get(api_endpoint)
|
25 |
+
return response.json()
|
26 |
+
|
27 |
+
@app.get("/api/search")
|
28 |
+
async def search(
|
29 |
+
q: str,
|
30 |
+
max_results: int = 10,
|
31 |
+
timelimit: Optional[str] = None,
|
32 |
+
safesearch: str = "moderate",
|
33 |
+
region: str = "wt-wt",
|
34 |
+
backend: str = "api"
|
35 |
+
):
|
36 |
+
"""Perform a text search."""
|
37 |
try:
|
38 |
with WEBS() as webs:
|
39 |
results = webs.text(keywords=q, region=region, safesearch=safesearch, timelimit=timelimit, backend=backend, max_results=max_results)
|
40 |
+
# Extract the data you want to display
|
41 |
search_results = [
|
42 |
{
|
43 |
'title': result.title,
|
44 |
'description': result.description
|
45 |
} for result in results
|
46 |
]
|
47 |
+
return JSONResponse(content=jsonable_encoder(search_results))
|
48 |
except Exception as e:
|
49 |
+
raise HTTPException(status_code=500, detail=f"Error during search: {e}")
|
50 |
+
|
51 |
+
@app.get("/api/answers")
|
52 |
+
async def get_people_also_search(q: str):
|
53 |
+
api_endpoint = f"{BASE_URL}/api/answers?q={q}"
|
54 |
+
response = requests.get(api_endpoint)
|
55 |
+
return response.json()
|
56 |
|
57 |
if __name__ == "__main__":
|
58 |
+
import uvicorn
|
59 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, reload=True)
|