in server/main.py [0:0]
def get_image():
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}
return ("", 204, headers)
request_json = request.get_json(silent=True)
request_args = request.args
default_image_prompt = 'a picture of a cute cat jumping'
default_description_prompt = 'describe the image'
default_image_count = 1
image_prompt = (request_json or request_args).get('image_prompt', default_image_prompt)
input_prompt = (request_json or request_args).get('desc_prompt', default_description_prompt)
text_prompt = f"""Do this for each image separately: "{html.escape(input_prompt)}". We will call the result of it as the information about an image. Give each image a title. Return the result as a list of objects in json format; each object will correspond one image and the fields for the object will be "title" for the title and "info" for the information."""
image_count = int((request_json or request_args).get('image_count', default_image_count))
if image_count > MAX_IMAGE_COUNT:
return ("Invalid image_count. Maximum image count is 5.", 406)
try:
images = get_images_with_count(image_prompt, image_count)
image_strings = []
caption_input = []
for img in images:
temp_bytes = img._image_bytes
image_strings.append(base64.b64encode(temp_bytes).decode("ascii"))
temp_image=Part.from_data(
mime_type="image/png",
data=temp_bytes)
caption_input.append(temp_image)
captions = caption_model.generate_content(
caption_input + [text_prompt],
generation_config=caption_generation_config,
safety_settings=safety_settings,
)
captions_list = make_captions(captions)
except Exception as error:
return (jsonify({ "error": str(error) }), 500, { "Access-Control-Allow-Origin": "*" })
resp_images_dict = []
for img, cap in zip(image_strings, captions_list):
resp_images_dict.append({"image": img, "caption": cap["description"], "title": cap["title"]})
resp = jsonify(resp_images_dict)
resp.headers.set("Access-Control-Allow-Origin", "*")
return resp