Private-Google / app.py
KingNish's picture
Update app.py
1713ae7 verified
raw
history blame
1.93 kB
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse, HTMLResponse # Import HTMLResponse here
from fastapi.encoders import jsonable_encoder
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import requests
import os
from webscout import WEBS, transcriber
app = FastAPI()
BASE_URL = "https://oevortex-webscout-api.hf.space"
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse) # HTMLResponse is now imported
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/api/suggestions")
async def get_suggestions(q: str):
api_endpoint = f"{BASE_URL}/api/suggestions?q={q}"
response = requests.get(api_endpoint)
return response.json()
@app.get("/api/search")
async def search(
q: str,
max_results: int = 10,
safesearch: str = "moderate",
region: str = "wt-wt",
backend: str = "api"
):
"""Perform a text search."""
try:
with WEBS() as webs:
results = webs.text(keywords=q, region=region, safesearch=safesearch, backend=backend, max_results=max_results)
# Extract the data you want to display
search_results = [
{
'title': result.title,
'description': result.description
} for result in results
]
return JSONResponse(content=jsonable_encoder(search_results))
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error during search: {e}")
@app.get("/api/answers")
async def get_people_also_search(q: str):
api_endpoint = f"{BASE_URL}/api/answers?q={q}"
response = requests.get(api_endpoint)
return response.json()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860, reload=True)