forked from GoogleCloudPlatform/generative-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
405 lines (349 loc) · 11.3 KB
/
main.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Flask Web Server"""
import base64
import os
import re
from urllib.parse import urlparse
from consts import (
CUSTOM_UI_ENGINE_IDS,
LOCATION,
PROJECT_ID,
SUMMARY_MODELS,
VALID_LANGUAGES,
WIDGET_CONFIGS,
IMAGE_SEARCH_ENGINE_IDs,
RECOMMENDATIONS_DATASTORE_IDs,
)
from ekg_utils import search_public_kg
from flask import Flask, render_template, request
from vais_utils import (
list_documents,
recommend_personalize,
search_enterprise_search,
)
from google.api_core.exceptions import ResourceExhausted
import requests
from werkzeug.exceptions import HTTPException
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024 # Set maximum upload size to 16MB
FORM_OPTIONS = {
"language_list": VALID_LANGUAGES,
"default_language": VALID_LANGUAGES[0],
}
CUSTOM_UI_SEARCH_ENGINES = [d["name"] for d in CUSTOM_UI_ENGINE_IDS]
NAV_LINKS = [
{"link": "/", "name": "Widgets", "icon": "widgets"},
{
"link": "/search",
"name": "Custom UI",
"icon": "build",
},
{
"link": "/image-search",
"name": "Image Search",
"icon": "image",
},
{
"link": "/recommend",
"name": "Recommendations",
"icon": "recommend",
},
{"link": "/ekg", "name": "Enterprise Knowledge Graph", "icon": "scatter_plot"},
{
"link": "https://github.com/GoogleCloudPlatform/generative-ai/blob/main/search/retrieval-augmented-generation/examples/question_answering.ipynb", # noqa: E501
"name": "🦜️🔗 Retrieval Augmented Generation (RAG)",
},
{
"link": "https://github.com/GoogleCloudPlatform/generative-ai/tree/main/search/web-app",
"name": "Source Code",
"icon": "code",
},
]
RECOMMENDATIONS_DOCUMENTS = list_documents(
project_id=PROJECT_ID,
location=LOCATION,
datastore_id=RECOMMENDATIONS_DATASTORE_IDs[0]["datastore_id"],
)
VALID_IMAGE_MIMETYPES = {"image/jpeg", "image/png", "image/bmp"}
@app.route("/", methods=["GET"])
@app.route("/finance", methods=["GET"])
def index() -> str:
"""
Web Server, Homepage for Widgets
"""
return render_template(
"index.html",
title=NAV_LINKS[0]["name"],
nav_links=NAV_LINKS,
search_engine_options=WIDGET_CONFIGS,
)
@app.route("/search", methods=["GET"])
def search() -> str:
"""
Web Server, Homepage for Search - Custom UI
"""
return render_template(
"search.html",
title=NAV_LINKS[1]["name"],
nav_links=NAV_LINKS,
search_engines=CUSTOM_UI_SEARCH_ENGINES,
summary_models=SUMMARY_MODELS,
)
@app.route("/search_vais", methods=["POST"])
def search_vais() -> str:
"""
Handle Search Vertex AI Search Request
"""
search_query = request.form.get("search_query", "")
# Check if POST Request includes search query
if not search_query:
return render_template(
"search.html",
title=NAV_LINKS[1]["name"],
nav_links=NAV_LINKS,
search_engines=CUSTOM_UI_SEARCH_ENGINES,
summary_models=SUMMARY_MODELS,
message_error="No query provided",
)
search_engine = request.form.get("search_engine", "")
if not search_engine:
return render_template(
"search.html",
title=NAV_LINKS[1]["name"],
nav_links=NAV_LINKS,
search_engines=CUSTOM_UI_SEARCH_ENGINES,
summary_models=SUMMARY_MODELS,
message_error="No search engine selected",
)
summary_model = request.form.get("summary_model")
summary_preamble = request.form.get("summary_preamble")
results, summary, request_url, raw_request, raw_response = search_enterprise_search(
project_id=PROJECT_ID,
location=LOCATION,
engine_id=CUSTOM_UI_ENGINE_IDS[int(search_engine)]["engine_id"],
search_query=search_query,
summary_model=summary_model,
summary_preamble=summary_preamble,
)
return render_template(
"search.html",
title=NAV_LINKS[1]["name"],
nav_links=NAV_LINKS,
search_engines=CUSTOM_UI_SEARCH_ENGINES,
summary_models=SUMMARY_MODELS,
message_success=search_query,
results=results,
summary=summary,
request_url=request_url,
raw_request=raw_request,
raw_response=raw_response,
)
@app.route("/image-search", methods=["GET"])
def image_search() -> str:
"""
Web Server, Homepage for Image Search - Custom UI
"""
return render_template(
"image-search.html",
title=NAV_LINKS[2]["name"],
nav_links=NAV_LINKS,
)
@app.route("/imagesearch_vais", methods=["POST"])
def imagesearch_vais() -> str:
"""
Handle Image Search Vertex AI Search Request
"""
search_query = request.form.get("search_query", "")
image_file = request.files["image"]
image_content = None
image_bytes = None
# Check if POST Request includes search query
if not search_query and not image_file:
return render_template(
"image-search.html",
nav_links=NAV_LINKS,
message_error="No query provided",
)
if image_file:
image_content = image_file.read()
elif search_query:
# Check if text is a url
image_url = urlparse(search_query)
if all([image_url.scheme, image_url.netloc, image_url.path]):
image_response = requests.get(
image_url.geturl(), allow_redirects=True, timeout=5
)
mime_type = image_response.headers["Content-Type"]
if mime_type not in VALID_IMAGE_MIMETYPES:
return render_template(
"image-search.html",
nav_links=NAV_LINKS,
message_error=f"Invalid image format - {mime_type}. Valid types {VALID_IMAGE_MIMETYPES}",
)
image_content = image_response.content
if image_content:
search_query = None
image_bytes = base64.b64encode(image_content)
try:
results, _, request_url, raw_request, raw_response = search_enterprise_search(
project_id=PROJECT_ID,
location=LOCATION,
engine_id=IMAGE_SEARCH_ENGINE_IDs[0]["engine_id"],
search_query=search_query,
image_bytes=image_bytes,
params={"search_type": 1},
)
except Exception as e:
return render_template(
"image-search.html",
nav_links=NAV_LINKS,
message_error=e.args[0],
)
return render_template(
"image-search.html",
title=NAV_LINKS[2]["name"],
nav_links=NAV_LINKS,
message_success="Success",
results=results,
request_url=request_url,
raw_request=raw_request,
raw_response=raw_response,
)
@app.route("/recommend", methods=["GET"])
def recommend() -> str:
"""
Web Server, Homepage for Recommendations - Custom UI
"""
return render_template(
"recommend.html",
nav_links=NAV_LINKS,
title=NAV_LINKS[3]["name"],
documents=RECOMMENDATIONS_DOCUMENTS,
attribution_token="",
)
@app.route("/recommend_vais", methods=["POST"])
def recommend_vais() -> str:
"""
Handle Recommend Vertex AI Search Request
"""
document_id = request.form.get("document_id", "")
attribution_token = request.form.get("attribution_token", "")
# Check if POST Request includes document id
if not document_id:
return render_template(
"recommend.html",
title=NAV_LINKS[3]["name"],
nav_links=NAV_LINKS,
documents=RECOMMENDATIONS_DOCUMENTS,
attribution_token=attribution_token,
message_error="No document provided",
)
(
results,
attribution_token,
request_url,
raw_request,
raw_response,
) = recommend_personalize(
project_id=PROJECT_ID,
location=LOCATION,
datastore_id=RECOMMENDATIONS_DATASTORE_IDs[0]["datastore_id"],
serving_config_id=RECOMMENDATIONS_DATASTORE_IDs[0]["engine_id"],
document_id=document_id,
attribution_token=attribution_token,
)
return render_template(
"recommend.html",
title=NAV_LINKS[3]["name"],
nav_links=NAV_LINKS,
documents=RECOMMENDATIONS_DOCUMENTS,
message_success=document_id,
results=results,
attribution_token=attribution_token,
request_url=request_url,
raw_request=raw_request,
raw_response=raw_response,
)
@app.route("/ekg", methods=["GET"])
def ekg() -> str:
"""
Web Server, Homepage for EKG
"""
return render_template(
"ekg.html",
title=NAV_LINKS[4]["name"],
nav_links=NAV_LINKS,
form_options=FORM_OPTIONS,
)
@app.route("/search_ekg", methods=["POST"])
def search_ekg() -> str:
"""
Handle Search EKG Request
"""
search_query = request.form.get("search_query", "")
# Check if POST Request includes search query
if not search_query:
return render_template(
"ekg.html",
title=NAV_LINKS[4]["name"],
nav_links=NAV_LINKS,
form_options=FORM_OPTIONS,
message_error="No query provided",
)
languages = request.form.getlist("languages")
form_types = request.form.get("types", "")
types = re.split(r"[\s,]", form_types) if form_types else []
entities, request_url, raw_request, raw_response = search_public_kg(
project_id=PROJECT_ID,
location=LOCATION,
search_query=search_query,
languages=languages,
types=types,
)
return render_template(
"ekg.html",
title=NAV_LINKS[4]["name"],
nav_links=NAV_LINKS,
form_options=FORM_OPTIONS,
message_success=search_query,
entities=entities,
request_url=request_url,
raw_request=raw_request,
raw_response=raw_response,
)
@app.errorhandler(Exception)
def handle_exception(ex: Exception):
"""
Handle Application Exceptions
"""
message_error = "An Unknown Error Occurred"
# Pass through HTTP errors
if isinstance(ex, HTTPException):
message_error = ex.get_description()
elif isinstance(ex, ResourceExhausted):
message_error = ex.message
else:
message_error = str(ex)
return render_template(
"search.html",
title=NAV_LINKS[1]["name"],
form_options=FORM_OPTIONS,
nav_links=NAV_LINKS,
search_engines=CUSTOM_UI_SEARCH_ENGINES,
summary_models=SUMMARY_MODELS,
message_error=message_error,
)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))