KingNish commited on
Commit
e3f629e
·
verified ·
1 Parent(s): d24576b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -42
app.py CHANGED
@@ -1,57 +1,41 @@
1
- from fastapi import FastAPI, HTTPException, Request
2
- from fastapi.responses import JSONResponse, HTMLResponse # Import HTMLResponse here
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
- templates = Jinja2Templates(directory="templates")
 
 
15
 
16
- @app.get("/", response_class=HTMLResponse) # HTMLResponse is now imported
17
- async def index(request: Request):
18
- return templates.TemplateResponse("index.html", {"request": request})
 
 
 
19
 
20
- @app.get("/api/suggestions")
21
- async def get_suggestions(q: str):
22
- api_endpoint = f"{BASE_URL}/api/suggestions?q={q}"
 
 
 
 
 
23
  response = requests.get(api_endpoint)
24
  return response.json()
25
 
26
- @app.get("/api/search")
27
- async def search(
28
- q: str,
29
- max_results: int = 10,
30
- safesearch: str = "moderate",
31
- region: str = "wt-wt",
32
- backend: str = "api"
33
- ):
34
- """Perform a text search."""
35
- try:
36
- with WEBS() as webs:
37
- results = webs.text(keywords=q, region=region, safesearch=safesearch, backend=backend, max_results=max_results)
38
- # Extract the data you want to display
39
- search_results = [
40
- {
41
- 'title': result.title,
42
- 'description': result.description
43
- } for result in results
44
- ]
45
- return JSONResponse(content=jsonable_encoder(search_results))
46
- except Exception as e:
47
- raise HTTPException(status_code=500, detail=f"Error during search: {e}")
48
-
49
- @app.get("/api/answers")
50
- async def get_people_also_search(q: str):
51
- api_endpoint = f"{BASE_URL}/api/answers?q={q}"
52
  response = requests.get(api_endpoint)
53
  return response.json()
54
 
 
55
  if __name__ == "__main__":
56
- import uvicorn
57
- uvicorn.run(app, host="0.0.0.0", port=7860, reload=True)
 
1
+ from flask import Flask, render_template, request
 
 
 
 
2
  import requests
3
  import os
 
4
 
5
+ app = Flask(__name__)
6
 
7
  BASE_URL = "https://oevortex-webscout-api.hf.space"
8
 
9
+ @app.route("/", methods=["GET", "POST"])
10
+ def index():
11
+ return render_template("index.html")
12
 
13
+ @app.route("/api/suggestions", methods=["GET"])
14
+ def get_suggestions():
15
+ search_query = request.args.get("q")
16
+ api_endpoint = f"{BASE_URL}/api/suggestions?q={search_query}"
17
+ response = requests.get(api_endpoint)
18
+ return response.json()
19
 
20
+
21
+ @app.route("/api/search", methods=["GET"])
22
+ def perform_search():
23
+ search_query = request.args.get("q")
24
+ results = request.args.get("max_results", "10")
25
+ time_limit = request.args.get("timelimit", "none")
26
+ safe_search = request.args.get("safesearch", "off")
27
+ api_endpoint = f"{BASE_URL}/api/search?q={search_query}&max_results={results}&timelimit={time_limit}&safesearch={safe_search}"
28
  response = requests.get(api_endpoint)
29
  return response.json()
30
 
31
+ @app.route("/api/answers", methods=["GET"])
32
+ def get_people_also_search():
33
+ search_query = request.args.get("q")
34
+ api_endpoint = f"{BASE_URL}/api/answers?q={search_query}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  response = requests.get(api_endpoint)
36
  return response.json()
37
 
38
+
39
  if __name__ == "__main__":
40
+ port = int(os.environ.get('PORT', 7860))
41
+ app.run(host='0.0.0.0', port=port)