Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,41 @@
|
|
1 |
-
from
|
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 =
|
11 |
|
12 |
BASE_URL = "https://oevortex-webscout-api.hf.space"
|
13 |
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
@app.
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
response = requests.get(api_endpoint)
|
24 |
return response.json()
|
25 |
|
26 |
-
@app.
|
27 |
-
|
28 |
-
q
|
29 |
-
|
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 |
-
|
57 |
-
|
|
|
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)
|