KingNish commited on
Commit
97f6077
·
verified ·
1 Parent(s): 53e5a09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -1,38 +1,59 @@
1
- from flask import Flask, render_template, request, jsonify
 
 
 
 
2
  import requests
3
  import os
4
  from webscout import WEBS, transcriber
5
 
6
- app = Flask(__name__)
7
 
8
  BASE_URL = "https://oevortex-webscout-api.hf.space"
9
 
10
- @app.route("/", methods=["GET", "POST"])
11
- def index():
12
- return render_template("index.html")
13
-
14
- @app.route("/api/search", methods=["GET"])
15
- def search():
16
- q = request.args.get("q")
17
- max_results = int(request.args.get("max_results", 10))
18
- timelimit = request.args.get("timelimit") # Timelimit is optional
19
- safesearch = request.args.get("safesearch", "moderate")
20
- region = request.args.get("region", "wt-wt")
21
- backend = request.args.get("backend", "api")
 
 
 
 
 
 
 
 
 
 
 
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 jsonify(search_results) # Return as JSON
33
  except Exception as e:
34
- return jsonify({"error": f"Error during search: {e}"})
 
 
 
 
 
 
35
 
36
  if __name__ == "__main__":
37
- port = int(os.environ.get('PORT', 7860))
38
- app.run(host='0.0.0.0', port=port)
 
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)