-
Notifications
You must be signed in to change notification settings - Fork 6
/
websearch
53 lines (42 loc) · 1.76 KB
/
websearch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, render_template, request, jsonify
import requests
app = Flask(__name__)
# Be sure to replace with your actual Google API key and Custom Search Engine ID
GOOGLE_API_KEY = 'AIzaSyDttoppLRB7DITEWfoqm1vzphTvYy-mE2Q'
GOOGLE_CX = 'd7b987669bda24906'
@app.route("/")
def home():
return render_template("index1.html")
@app.route("/summarize", methods=["POST"])
def summarize():
# Assumes the client sends JSON data with a "summary" key
summarized_paragraph = request.json['summary']
# Construct the query for Google Custom Search API
query = f"autism care services {summarized_paragraph}"
try:
# Make the GET request to Google Custom Search API
response = requests.get(
'https://www.googleapis.com/customsearch/v1',
params={
'key': GOOGLE_API_KEY,
'cx': GOOGLE_CX,
'q': query,
}
)
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
search_results = response.json()
if 'error' in search_results:
raise Exception(search_results['error']['message'])
# Extract the search results and prepare a response
if 'items' in search_results:
resources = [
f"{item['title']} - {item['link']}" for item in search_results['items']
]
response_message = '\n'.join(resources)
else:
response_message = "No results found for your query."
except requests.exceptions.RequestException as e:
response_message = f"An error occurred while accessing Google Search API: {str(e)}"
return jsonify({"message": response_message})
if __name__ == "__main__":
app.run(debug=True)