-
Notifications
You must be signed in to change notification settings - Fork 25
/
prompt.py
124 lines (95 loc) · 5.92 KB
/
prompt.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
# The trailing space and "\n" is not a problem since they will be removed before making an API request.
templatev0_1 = """You are an intelligent bounding box generator. I will provide you with a caption for a photo, image, or painting. Your task is to generate the bounding boxes for the objects mentioned in the caption, along with a background prompt describing the scene. The images are of size 512x512. The top-left corner has coordinate [0, 0]. The bottom-right corner has coordinnate [512, 512]. The bounding boxes should not overlap or go beyond the image boundaries. Each bounding box should be in the format of (object name, [top-left x coordinate, top-left y coordinate, box width, box height]) and should not include more than one object. Do not put objects that are already provided in the bounding boxes into the background prompt. Do not include non-existing or excluded objects in the background prompt. Use "A realistic scene" as the background prompt if no background is given in the prompt. If needed, you can make reasonable guesses. Please refer to the example below for the desired format.
Caption: A realistic image of landscape scene depicting a green car parking on the left of a blue truck, with a red air balloon and a bird in the sky
Objects: [('a green car', [21, 281, 211, 159]), ('a blue truck', [269, 283, 209, 160]), ('a red air balloon', [66, 8, 145, 135]), ('a bird', [296, 42, 143, 100])]
Background prompt: A realistic landscape scene
Negative prompt:
Caption: A realistic top-down view of a wooden table with two apples on it
Objects: [('a wooden table', [20, 148, 472, 216]), ('an apple', [150, 226, 100, 100]), ('an apple', [280, 226, 100, 100])]
Background prompt: A realistic top-down view
Negative prompt:
Caption: A realistic scene of three skiers standing in a line on the snow near a palm tree
Objects: [('a skier', [5, 152, 139, 168]), ('a skier', [278, 192, 121, 158]), ('a skier', [148, 173, 124, 155]), ('a palm tree', [404, 105, 103, 251])]
Background prompt: A realistic outdoor scene with snow
Negative prompt:
Caption: An oil painting of a pink dolphin jumping on the left of a steam boat on the sea
Objects: [('a steam boat', [232, 225, 257, 149]), ('a jumping pink dolphin', [21, 249, 189, 123])]
Background prompt: An oil painting of the sea
Negative prompt:
Caption: A cute cat and an angry dog without birds
Objects: [('a cute cat', [51, 67, 271, 324]), ('an angry dog', [302, 119, 211, 228])]
Background prompt: A realistic scene
Negative prompt: birds
Caption: Two pandas in a forest without flowers
Objects: [('a panda', [30, 171, 212, 226]), ('a panda', [264, 173, 222, 221])]
Background prompt: A forest
Negative prompt: flowers
Caption: An oil painting of a living room scene without chairs with a painting mounted on the wall, a cabinet below the painting, and two flower vases on the cabinet
Objects: [('a painting', [88, 85, 335, 203]), ('a cabinet', [57, 308, 404, 201]), ('a flower vase', [166, 222, 92, 108]), ('a flower vase', [328, 222, 92, 108])]
Background prompt: An oil painting of a living room scene
Negative prompt: chairs
Caption: {prompt}
Objects:
"""
DEFAULT_SO_NEGATIVE_PROMPT = "artifacts, blurry, smooth texture, bad quality, distortions, unrealistic, distorted image, bad proportions, duplicate, two, many, group, occlusion, occluded, side, border, collate"
DEFAULT_OVERALL_NEGATIVE_PROMPT = "artifacts, blurry, smooth texture, bad quality, distortions, unrealistic, distorted image, bad proportions, duplicate"
templates = {"v0.1": templatev0_1}
template_versions = ["v0.1"]
stop = "\n\n"
prompts_demo_gpt4, prompts_demo_gpt3_5 = [], []
# Put what we want to generate when you query GPT-4 for demo here
prompts_demo_gpt4 = [
"In an indoor scene, a blue cube directly above a red cube with a vase on the left of them.",
"A realistic photo of a wooden table without bananas in an indoor scene",
"A realistic image of a white deer and a gray bear in an empty factory scene",
]
# Put what we want to generate when you query GPT-3.5 for demo here
prompts_demo_gpt3_5 = []
prompt_types = [
"demo",
"lmd_negation",
"lmd_numeracy",
"lmd_attribution",
"lmd_spatial",
"lmd",
]
def get_prompts(prompt_type, model, allow_non_exist=False):
"""
This function returns the text prompts according to the requested `prompt_type` and `model`. Set `model` to "all" to return all the text prompts in the current type. Otherwise we may want to have different prompts for gpt-3.5 and gpt-4 to prevent confusion.
"""
prompts_gpt4, prompts_gpt3_5 = {}, {}
if prompt_type.startswith("lmd"):
from utils.eval.lmd import get_lmd_prompts
prompts = get_lmd_prompts()
# We do not add to both dict to prevent duplicates when model is set to "all".
if "gpt-4" in model:
prompts_gpt4.update(prompts)
else:
prompts_gpt3_5.update(prompts)
elif prompt_type == "demo":
prompts_gpt4["demo"] = prompts_demo_gpt4
prompts_gpt3_5["demo"] = prompts_demo_gpt3_5
if "all" in model:
return prompts_gpt4.get(prompt_type, []) + prompts_gpt3_5.get(prompt_type, [])
elif "gpt-4" in model:
if allow_non_exist:
return prompts_gpt4.get(prompt_type, [])
return prompts_gpt4[prompt_type]
else:
# Default: gpt-3.5
if allow_non_exist:
return prompts_gpt3_5.get(prompt_type, [])
return prompts_gpt3_5[prompt_type]
if __name__ == "__main__":
# Print the full prompt for the latest prompt in prompts
# This allows pasting into an LLM web UI
prompt_type = "demo"
assert prompt_type in prompt_types, f"prompt_type {prompt_type} does not exist"
prompts = get_prompts(prompt_type, "all")
prompt = prompts[-1]
prompt_full = templatev0_1.format(prompt=prompt.strip().rstrip("."))
print(prompt_full)
if False:
# Useful if you want to query an LLM with JSON input
import json
print(json.dumps(prompt_full.strip("\n")))