Aniquel yinshengming commited on
Commit
fe55e95
0 Parent(s):

Duplicate from microsoft/visual_chatgpt

Browse files

Co-authored-by: yinshengming <yinsming@users.noreply.huggingface.co>

Files changed (7) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +269 -0
  4. image/placeholder.txt +0 -0
  5. packages.txt +1 -0
  6. requirements.txt +32 -0
  7. visual_foundation_models.py +892 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Visual Chatgpt
3
+ emoji: 🎨
4
+ colorFrom: yellow
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.20.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: osl-3.0
11
+ duplicated_from: microsoft/visual_chatgpt
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ VISUAL_CHATGPT_PREFIX = """Visual ChatGPT is designed to be able to assist with a wide range of text and visual related tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. Visual ChatGPT is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
2
+
3
+ Visual ChatGPT is able to process and understand large amounts of text and image. As a language model, Visual ChatGPT can not directly read images, but it has a list of tools to finish different visual tasks. Each image will have a file name formed as "image/xxx.png", and Visual ChatGPT can invoke different tools to indirectly understand pictures. When talking about images, Visual ChatGPT is very strict to the file name and will never fabricate nonexistent files. When using tools to generate new image files, Visual ChatGPT is also known that the image may not be the same as user's demand, and will use other visual question answering tools or description tools to observe the real image. Visual ChatGPT is able to use tools in a sequence, and is loyal to the tool observation outputs rather than faking the image content and image file name. It will remember to provide the file name from the last tool observation, if a new image is generated.
4
+
5
+ Human may provide new figures to Visual ChatGPT with a description. The description helps Visual ChatGPT to understand this image, but Visual ChatGPT should use tools to finish following tasks, rather than directly imagine from the description.
6
+
7
+ Overall, Visual ChatGPT is a powerful visual dialogue assistant tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics.
8
+
9
+
10
+ TOOLS:
11
+ ------
12
+
13
+ Visual ChatGPT has access to the following tools:"""
14
+
15
+ VISUAL_CHATGPT_FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:
16
+
17
+ ```
18
+ Thought: Do I need to use a tool? Yes
19
+ Action: the action to take, should be one of [{tool_names}]
20
+ Action Input: the input to the action
21
+ Observation: the result of the action
22
+ ```
23
+
24
+ When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
25
+
26
+ ```
27
+ Thought: Do I need to use a tool? No
28
+ {ai_prefix}: [your response here]
29
+ ```
30
+ """
31
+
32
+ VISUAL_CHATGPT_SUFFIX = """You are very strict to the filename correctness and will never fake a file name if not exists.
33
+ You will remember to provide the image file name loyally if it's provided in the last tool observation.
34
+
35
+ Begin!
36
+
37
+ Previous conversation history:
38
+ {chat_history}
39
+
40
+ New input: {input}
41
+ Since Visual ChatGPT is a text language model, Visual ChatGPT must use tools to observe images rather than imagination.
42
+ The thoughts and observations are only visible for Visual ChatGPT, Visual ChatGPT should remember to repeat important information in the final response for Human.
43
+ Thought: Do I need to use a tool? {agent_scratchpad}"""
44
+
45
+ VISUAL_CHATGPT_PREFIX_CN = """Visual ChatGPT 旨在能够协助完成范围广泛的文本和视觉相关任务,从回答简单的问题到提供对广泛主题的深入解释和讨论。 Visual ChatGPT 能够根据收到的输入生成类似人类的文本,使其能够进行听起来自然的对话,并提供连贯且与手头主题相关的响应。
46
+
47
+ Visual ChatGPT 能够处理和理解大量文本和图像。作为一种语言模型,Visual ChatGPT 不能直接读取图像,但它有一系列工具来完成不同的视觉任务。每张图片都会有一个文件名,格式为“image/xxx.png”,Visual ChatGPT可以调用不同的工具来间接理解图片。在谈论图片时,Visual ChatGPT 对文件名的要求非常严格,绝不会伪造不存在的文件。在使用工具生成新的图像文件时,Visual ChatGPT也知道图像可能与用户需求不一样,会使用其他视觉问答工具或描述工具来观察真实图像。 Visual ChatGPT 能够按顺序使用工具,并且忠于工具观察输出,而不是伪造图像内容和图像文件名。如果生成新图像,它将记得提供上次工具观察的文件名。
48
+
49
+ Human 可能会向 Visual ChatGPT 提供带有描述的新图形。描述帮助 Visual ChatGPT 理解这个图像,但 Visual ChatGPT 应该使用工具来完成以下任务,而不是直接从描述中想象。有些工具将会返回英文描述,但你对用户的聊天应当采用中文。
50
+
51
+ 总的来说,Visual ChatGPT 是一个强大的可视化对话辅助工具,可以帮助处理范围广泛的任务,并提供关于范围广泛的主题的有价值的见解和信息。
52
+
53
+ 工具列表:
54
+ ------
55
+
56
+ Visual ChatGPT 可以使用这些工具:"""
57
+
58
+ VISUAL_CHATGPT_FORMAT_INSTRUCTIONS_CN = """用户使用中文和你进行聊天,但是工具的参数应当使用英文。如果要调用工具,你必须遵循如下格式:
59
+
60
+ ```
61
+ Thought: Do I need to use a tool? Yes
62
+ Action: the action to take, should be one of [{tool_names}]
63
+ Action Input: the input to the action
64
+ Observation: the result of the action
65
+ ```
66
+
67
+ 当你不再需要继续调用工具,而是对观察结果进行总结回复时,你必须使用如下格式:
68
+
69
+
70
+ ```
71
+ Thought: Do I need to use a tool? No
72
+ {ai_prefix}: [your response here]
73
+ ```
74
+ """
75
+
76
+ VISUAL_CHATGPT_SUFFIX_CN = """你对文件名的正确性非常严格,而且永远不会伪造不存在的文件。
77
+
78
+ 开始!
79
+
80
+ 因为Visual ChatGPT是一个文本语言模型,必须使用工具去观察图片而不是依靠想象。
81
+ 推理想法和观察结果只对Visual ChatGPT可见,需要记得在最终回复时把重要的信息重复给用户,你只能给用户返回中文句子。我们一步一步思考。在你使用工具时,工具的参数只能是英文。
82
+
83
+ 聊天历史:
84
+ {chat_history}
85
+
86
+ 新输入: {input}
87
+ Thought: Do I need to use a tool? {agent_scratchpad}
88
+ """
89
+
90
+ from visual_foundation_models import *
91
+ from langchain.agents.initialize import initialize_agent
92
+ from langchain.agents.tools import Tool
93
+ from langchain.chains.conversation.memory import ConversationBufferMemory
94
+ from langchain.llms.openai import OpenAI
95
+ import re
96
+ import gradio as gr
97
+ import inspect
98
+
99
+
100
+ def cut_dialogue_history(history_memory, keep_last_n_words=400):
101
+ if history_memory is None or len(history_memory) == 0:
102
+ return history_memory
103
+ tokens = history_memory.split()
104
+ n_tokens = len(tokens)
105
+ print(f"history_memory:{history_memory}, n_tokens: {n_tokens}")
106
+ if n_tokens < keep_last_n_words:
107
+ return history_memory
108
+ paragraphs = history_memory.split('\n')
109
+ last_n_tokens = n_tokens
110
+ while last_n_tokens >= keep_last_n_words:
111
+ last_n_tokens -= len(paragraphs[0].split(' '))
112
+ paragraphs = paragraphs[1:]
113
+ return '\n' + '\n'.join(paragraphs)
114
+
115
+
116
+ class ConversationBot:
117
+ def __init__(self, load_dict):
118
+ # load_dict = {'VisualQuestionAnswering':'cuda:0', 'ImageCaptioning':'cuda:1',...}
119
+ print(f"Initializing VisualChatGPT, load_dict={load_dict}")
120
+ if 'ImageCaptioning' not in load_dict:
121
+ raise ValueError("You have to load ImageCaptioning as a basic function for VisualChatGPT")
122
+
123
+ self.models = {}
124
+ # Load Basic Foundation Models
125
+ for class_name, device in load_dict.items():
126
+ self.models[class_name] = globals()[class_name](device=device)
127
+
128
+ # Load Template Foundation Models
129
+ for class_name, module in globals().items():
130
+ if getattr(module, 'template_model', False):
131
+ template_required_names = {k for k in inspect.signature(module.__init__).parameters.keys() if
132
+ k != 'self'}
133
+ loaded_names = set([type(e).__name__ for e in self.models.values()])
134
+ if template_required_names.issubset(loaded_names):
135
+ self.models[class_name] = globals()[class_name](
136
+ **{name: self.models[name] for name in template_required_names})
137
+ self.tools = []
138
+ for instance in self.models.values():
139
+ for e in dir(instance):
140
+ if e.startswith('inference'):
141
+ func = getattr(instance, e)
142
+ self.tools.append(Tool(name=func.name, description=func.description, func=func))
143
+ self.memory = ConversationBufferMemory(memory_key="chat_history", output_key='output')
144
+
145
+ def run_text(self, text, state):
146
+ self.agent.memory.buffer = cut_dialogue_history(self.agent.memory.buffer, keep_last_n_words=500)
147
+ res = self.agent({"input": text.strip()})
148
+ res['output'] = res['output'].replace("\\", "/")
149
+ response = re.sub('(image/\S*png)', lambda m: f'![](/file={m.group(0)})*{m.group(0)}*', res['output'])
150
+ state = state + [(text, response)]
151
+ print(f"\nProcessed run_text, Input text: {text}\nCurrent state: {state}\n"
152
+ f"Current Memory: {self.agent.memory.buffer}")
153
+ return state, state
154
+
155
+ def run_image(self, image, state, txt):
156
+ image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.png")
157
+ print("======>Auto Resize Image...")
158
+ img = Image.open(image.name)
159
+ width, height = img.size
160
+ ratio = min(512 / width, 512 / height)
161
+ width_new, height_new = (round(width * ratio), round(height * ratio))
162
+ width_new = int(np.round(width_new / 64.0)) * 64
163
+ height_new = int(np.round(height_new / 64.0)) * 64
164
+ img = img.resize((width_new, height_new))
165
+ img = img.convert('RGB')
166
+ img.save(image_filename, "PNG")
167
+ print(f"Resize image form {width}x{height} to {width_new}x{height_new}")
168
+ description = self.models['ImageCaptioning'].inference(image_filename)
169
+ Human_prompt = f'\nHuman: provide a figure named {image_filename}. The description is: {description}. This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n'
170
+ AI_prompt = "Received. "
171
+ self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + 'AI: ' + AI_prompt
172
+ state = state + [(f"![](/file={image_filename})*{image_filename}*", AI_prompt)]
173
+ print(f"\nProcessed run_image, Input image: {image_filename}\nCurrent state: {state}\n"
174
+ f"Current Memory: {self.agent.memory.buffer}")
175
+ return state, state, f'{txt} {image_filename} '
176
+
177
+ def init_agent(self, openai_api_key, lang):
178
+ self.memory.clear()
179
+ if lang=='English':
180
+ PREFIX, FORMAT_INSTRUCTIONS, SUFFIX = VISUAL_CHATGPT_PREFIX, VISUAL_CHATGPT_FORMAT_INSTRUCTIONS, VISUAL_CHATGPT_SUFFIX
181
+ place = "Enter text and press enter, or upload an image"
182
+ label_clear = "Clear"
183
+ else:
184
+ PREFIX, FORMAT_INSTRUCTIONS, SUFFIX = VISUAL_CHATGPT_PREFIX_CN, VISUAL_CHATGPT_FORMAT_INSTRUCTIONS_CN, VISUAL_CHATGPT_SUFFIX_CN
185
+ place = "输入文字并回车,或者上传图片"
186
+ label_clear = "清除"
187
+ self.llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
188
+ self.agent = initialize_agent(
189
+ self.tools,
190
+ self.llm,
191
+ agent="conversational-react-description",
192
+ verbose=True,
193
+ memory=self.memory,
194
+ return_intermediate_steps=True,
195
+ agent_kwargs={'prefix': PREFIX, 'format_instructions': FORMAT_INSTRUCTIONS, 'suffix': SUFFIX}, )
196
+
197
+ return gr.update(visible = True)
198
+
199
+ bot = ConversationBot({'Text2Image': 'cuda:0',
200
+ 'ImageCaptioning': 'cuda:0',
201
+ 'ImageEditing': 'cuda:0',
202
+ 'VisualQuestionAnswering': 'cuda:0',
203
+ 'Image2Canny': 'cpu',
204
+ 'CannyText2Image': 'cuda:0',
205
+ 'InstructPix2Pix': 'cuda:0',
206
+ 'Image2Depth': 'cpu',
207
+ 'DepthText2Image': 'cuda:0',
208
+ })
209
+
210
+ with gr.Blocks(css="#chatbot {overflow:auto; height:500px;}") as demo:
211
+ gr.Markdown("<h3><center>Visual ChatGPT</center></h3>")
212
+ gr.Markdown(
213
+ """This is a demo to the work [Visual ChatGPT: Talking, Drawing and Editing with Visual Foundation Models](https://github.com/microsoft/visual-chatgpt).<br>
214
+ This space connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting.<br>
215
+ """
216
+ )
217
+
218
+ with gr.Row():
219
+ lang = gr.Radio(choices=['Chinese', 'English'], value='English', label='Language')
220
+ openai_api_key_textbox = gr.Textbox(
221
+ placeholder="Paste your OpenAI API key here to start Visual ChatGPT(sk-...) and press Enter ↵️",
222
+ show_label=False,
223
+ lines=1,
224
+ type="password",
225
+ )
226
+
227
+ chatbot = gr.Chatbot(elem_id="chatbot", label="Visual ChatGPT")
228
+ state = gr.State([])
229
+
230
+ with gr.Row(visible=False) as input_raws:
231
+ with gr.Column(scale=0.7):
232
+ txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False)
233
+ with gr.Column(scale=0.10, min_width=0):
234
+ run = gr.Button("🏃‍♂️Run")
235
+ with gr.Column(scale=0.10, min_width=0):
236
+ clear = gr.Button("🔄Clear️")
237
+ with gr.Column(scale=0.10, min_width=0):
238
+ btn = gr.UploadButton("🖼️Upload", file_types=["image"])
239
+
240
+ gr.Examples(
241
+ examples=["Generate a figure of a cat running in the garden",
242
+ "Replace the cat with a dog",
243
+ "Remove the dog in this image",
244
+ "Can you detect the canny edge of this image?",
245
+ "Can you use this canny image to generate an oil painting of a dog",
246
+ "Make it like water-color painting",
247
+ "What is the background color",
248
+ "Describe this image",
249
+ "please detect the depth of this image",
250
+ "Can you use this depth image to generate a cute dog",
251
+ ],
252
+ inputs=txt
253
+ )
254
+
255
+ gr.HTML('''<br><br><br><center>You can duplicate this Space to skip the queue:
256
+ <a href="https://huggingface.co/spaces/microsoft/visual_chatgpt?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><br>
257
+ </center>''')
258
+
259
+ openai_api_key_textbox.submit(bot.init_agent, [openai_api_key_textbox, lang], [input_raws])
260
+ txt.submit(bot.run_text, [txt, state], [chatbot, state])
261
+ txt.submit(lambda: "", None, txt)
262
+ run.click(bot.run_text, [txt, state], [chatbot, state])
263
+ run.click(lambda: "", None, txt)
264
+ btn.upload(bot.run_image, [btn, state, txt], [chatbot, state, txt])
265
+ clear.click(bot.memory.clear)
266
+ clear.click(lambda: [], None, chatbot)
267
+ clear.click(lambda: [], None, state)
268
+
269
+ demo.queue(concurrency_count=10).launch(server_name="0.0.0.0", server_port=7860)
image/placeholder.txt ADDED
File without changes
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ python3-opencv
requirements.txt ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ torch==1.12.1
3
+ torchvision==0.13.1
4
+ numpy==1.23.1
5
+ transformers==4.26.1
6
+ albumentations==1.3.0
7
+ opencv-contrib-python==4.3.0.36
8
+ imageio==2.9.0
9
+ imageio-ffmpeg==0.4.2
10
+ pytorch-lightning==1.5.0
11
+ omegaconf==2.1.1
12
+ test-tube>=0.7.5
13
+ streamlit==1.12.1
14
+ einops==0.3.0
15
+ webdataset==0.2.5
16
+ kornia==0.6
17
+ open_clip_torch==2.0.2
18
+ invisible-watermark>=0.1.5
19
+ streamlit-drawable-canvas==0.8.0
20
+ torchmetrics==0.6.0
21
+ timm==0.6.12
22
+ addict==2.4.0
23
+ yapf==0.32.0
24
+ prettytable==3.6.0
25
+ safetensors==0.2.7
26
+ basicsr==1.4.2
27
+ langchain==0.0.101
28
+ diffusers==0.14.0
29
+ gradio
30
+ openai
31
+ accelerate
32
+ controlnet-aux==0.0.1
visual_foundation_models.py ADDED
@@ -0,0 +1,892 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline, StableDiffusionInstructPix2PixPipeline
2
+ from diffusers import EulerAncestralDiscreteScheduler
3
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
4
+ from controlnet_aux import OpenposeDetector, MLSDdetector, HEDdetector
5
+
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, CLIPSegProcessor, CLIPSegForImageSegmentation
7
+ from transformers import pipeline, BlipProcessor, BlipForConditionalGeneration, BlipForQuestionAnswering
8
+ from transformers import AutoImageProcessor, UperNetForSemanticSegmentation
9
+
10
+ import os
11
+ import random
12
+ import torch
13
+ import cv2
14
+ import uuid
15
+ from PIL import Image, ImageOps
16
+ import numpy as np
17
+ from pytorch_lightning import seed_everything
18
+ import math
19
+
20
+ from langchain.llms.openai import OpenAI
21
+
22
+ def prompts(name, description):
23
+ def decorator(func):
24
+ func.name = name
25
+ func.description = description
26
+ return func
27
+
28
+ return decorator
29
+
30
+ def blend_gt2pt(old_image, new_image, sigma=0.15, steps=100):
31
+ new_size = new_image.size
32
+ old_size = old_image.size
33
+ easy_img = np.array(new_image)
34
+ gt_img_array = np.array(old_image)
35
+ pos_w = (new_size[0] - old_size[0]) // 2
36
+ pos_h = (new_size[1] - old_size[1]) // 2
37
+
38
+ kernel_h = cv2.getGaussianKernel(old_size[1], old_size[1] * sigma)
39
+ kernel_w = cv2.getGaussianKernel(old_size[0], old_size[0] * sigma)
40
+ kernel = np.multiply(kernel_h, np.transpose(kernel_w))
41
+
42
+ kernel[steps:-steps, steps:-steps] = 1
43
+ kernel[:steps, :steps] = kernel[:steps, :steps] / kernel[steps - 1, steps - 1]
44
+ kernel[:steps, -steps:] = kernel[:steps, -steps:] / kernel[steps - 1, -(steps)]
45
+ kernel[-steps:, :steps] = kernel[-steps:, :steps] / kernel[-steps, steps - 1]
46
+ kernel[-steps:, -steps:] = kernel[-steps:, -steps:] / kernel[-steps, -steps]
47
+ kernel = np.expand_dims(kernel, 2)
48
+ kernel = np.repeat(kernel, 3, 2)
49
+
50
+ weight = np.linspace(0, 1, steps)
51
+ top = np.expand_dims(weight, 1)
52
+ top = np.repeat(top, old_size[0] - 2 * steps, 1)
53
+ top = np.expand_dims(top, 2)
54
+ top = np.repeat(top, 3, 2)
55
+
56
+ weight = np.linspace(1, 0, steps)
57
+ down = np.expand_dims(weight, 1)
58
+ down = np.repeat(down, old_size[0] - 2 * steps, 1)
59
+ down = np.expand_dims(down, 2)
60
+ down = np.repeat(down, 3, 2)
61
+
62
+ weight = np.linspace(0, 1, steps)
63
+ left = np.expand_dims(weight, 0)
64
+ left = np.repeat(left, old_size[1] - 2 * steps, 0)
65
+ left = np.expand_dims(left, 2)
66
+ left = np.repeat(left, 3, 2)
67
+
68
+ weight = np.linspace(1, 0, steps)
69
+ right = np.expand_dims(weight, 0)
70
+ right = np.repeat(right, old_size[1] - 2 * steps, 0)
71
+ right = np.expand_dims(right, 2)
72
+ right = np.repeat(right, 3, 2)
73
+
74
+ kernel[:steps, steps:-steps] = top
75
+ kernel[-steps:, steps:-steps] = down
76
+ kernel[steps:-steps, :steps] = left
77
+ kernel[steps:-steps, -steps:] = right
78
+
79
+ pt_gt_img = easy_img[pos_h:pos_h + old_size[1], pos_w:pos_w + old_size[0]]
80
+ gaussian_gt_img = kernel * gt_img_array + (1 - kernel) * pt_gt_img # gt img with blur img
81
+ gaussian_gt_img = gaussian_gt_img.astype(np.int64)
82
+ easy_img[pos_h:pos_h + old_size[1], pos_w:pos_w + old_size[0]] = gaussian_gt_img
83
+ gaussian_img = Image.fromarray(easy_img)
84
+ return gaussian_img
85
+
86
+ def get_new_image_name(org_img_name, func_name="update"):
87
+ head_tail = os.path.split(org_img_name)
88
+ head = head_tail[0]
89
+ tail = head_tail[1]
90
+ name_split = tail.split('.')[0].split('_')
91
+ this_new_uuid = str(uuid.uuid4())[0:4]
92
+ if len(name_split) == 1:
93
+ most_org_file_name = name_split[0]
94
+ recent_prev_file_name = name_split[0]
95
+ new_file_name = '{}_{}_{}_{}.png'.format(this_new_uuid, func_name, recent_prev_file_name, most_org_file_name)
96
+ else:
97
+ assert len(name_split) == 4
98
+ most_org_file_name = name_split[3]
99
+ recent_prev_file_name = name_split[0]
100
+ new_file_name = '{}_{}_{}_{}.png'.format(this_new_uuid, func_name, recent_prev_file_name, most_org_file_name)
101
+ return os.path.join(head, new_file_name)
102
+
103
+
104
+ class MaskFormer:
105
+ def __init__(self, device):
106
+ print(f"Initializing MaskFormer to {device}")
107
+ self.device = device
108
+ self.processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
109
+ self.model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined").to(device)
110
+
111
+ def inference(self, image_path, text):
112
+ threshold = 0.5
113
+ min_area = 0.02
114
+ padding = 20
115
+ original_image = Image.open(image_path)
116
+ image = original_image.resize((512, 512))
117
+ inputs = self.processor(text=text, images=image, padding="max_length", return_tensors="pt").to(self.device)
118
+ with torch.no_grad():
119
+ outputs = self.model(**inputs)
120
+ mask = torch.sigmoid(outputs[0]).squeeze().cpu().numpy() > threshold
121
+ area_ratio = len(np.argwhere(mask)) / (mask.shape[0] * mask.shape[1])
122
+ if area_ratio < min_area:
123
+ return None
124
+ true_indices = np.argwhere(mask)
125
+ mask_array = np.zeros_like(mask, dtype=bool)
126
+ for idx in true_indices:
127
+ padded_slice = tuple(slice(max(0, i - padding), i + padding + 1) for i in idx)
128
+ mask_array[padded_slice] = True
129
+ visual_mask = (mask_array * 255).astype(np.uint8)
130
+ image_mask = Image.fromarray(visual_mask)
131
+ return image_mask.resize(original_image.size)
132
+
133
+
134
+ class ImageEditing:
135
+ def __init__(self, device):
136
+ print(f"Initializing ImageEditing to {device}")
137
+ self.device = device
138
+ self.mask_former = MaskFormer(device=self.device)
139
+ self.revision = 'fp16' if 'cuda' in device else None
140
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
141
+ self.inpaint = StableDiffusionInpaintPipeline.from_pretrained(
142
+ "runwayml/stable-diffusion-inpainting", revision=self.revision, torch_dtype=self.torch_dtype).to(device)
143
+
144
+ @prompts(name="Remove Something From The Photo",
145
+ description="useful when you want to remove and object or something from the photo "
146
+ "from its description or location. "
147
+ "The input to this tool should be a comma separated string of two, "
148
+ "representing the image_path and the object need to be removed. ")
149
+ def inference_remove(self, inputs):
150
+ image_path, to_be_removed_txt = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
151
+ return self.inference_replace(f"{image_path},{to_be_removed_txt},background")
152
+
153
+ @prompts(name="Replace Something From The Photo",
154
+ description="useful when you want to replace an object from the object description or "
155
+ "location with another object from its description. "
156
+ "The input to this tool should be a comma separated string of three, "
157
+ "representing the image_path, the object to be replaced, the object to be replaced with ")
158
+ def inference_replace(self, inputs):
159
+ image_path, to_be_replaced_txt, replace_with_txt = inputs.split(",")
160
+ original_image = Image.open(image_path)
161
+ original_size = original_image.size
162
+ mask_image = self.mask_former.inference(image_path, to_be_replaced_txt)
163
+ updated_image = self.inpaint(prompt=replace_with_txt, image=original_image.resize((512, 512)),
164
+ mask_image=mask_image.resize((512, 512))).images[0]
165
+ updated_image_path = get_new_image_name(image_path, func_name="replace-something")
166
+ updated_image = updated_image.resize(original_size)
167
+ updated_image.save(updated_image_path)
168
+ print(
169
+ f"\nProcessed ImageEditing, Input Image: {image_path}, Replace {to_be_replaced_txt} to {replace_with_txt}, "
170
+ f"Output Image: {updated_image_path}")
171
+ return updated_image_path
172
+
173
+
174
+ class InstructPix2Pix:
175
+ def __init__(self, device):
176
+ print(f"Initializing InstructPix2Pix to {device}")
177
+ self.device = device
178
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
179
+ self.pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix",
180
+ safety_checker=None,
181
+ torch_dtype=self.torch_dtype).to(device)
182
+ self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)
183
+
184
+ @prompts(name="Instruct Image Using Text",
185
+ description="useful when you want to the style of the image to be like the text. "
186
+ "like: make it look like a painting. or make it like a robot. "
187
+ "The input to this tool should be a comma separated string of two, "
188
+ "representing the image_path and the text. ")
189
+ def inference(self, inputs):
190
+ """Change style of image."""
191
+ print("===>Starting InstructPix2Pix Inference")
192
+ image_path, text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
193
+ original_image = Image.open(image_path)
194
+ image = self.pipe(text, image=original_image, num_inference_steps=40, image_guidance_scale=1.2).images[0]
195
+ updated_image_path = get_new_image_name(image_path, func_name="pix2pix")
196
+ image.save(updated_image_path)
197
+ print(f"\nProcessed InstructPix2Pix, Input Image: {image_path}, Instruct Text: {text}, "
198
+ f"Output Image: {updated_image_path}")
199
+ return updated_image_path
200
+
201
+
202
+ class Text2Image:
203
+ def __init__(self, device):
204
+ print(f"Initializing Text2Image to {device}")
205
+ self.device = device
206
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
207
+ self.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",
208
+ torch_dtype=self.torch_dtype)
209
+ self.pipe.to(device)
210
+ self.a_prompt = 'best quality, extremely detailed'
211
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
212
+ 'fewer digits, cropped, worst quality, low quality'
213
+
214
+ @prompts(name="Generate Image From User Input Text",
215
+ description="useful when you want to generate an image from a user input text and save it to a file. "
216
+ "like: generate an image of an object or something, or generate an image that includes some objects. "
217
+ "The input to this tool should be a string, representing the text used to generate image. ")
218
+ def inference(self, text):
219
+ image_filename = os.path.join('image', f"{str(uuid.uuid4())[:8]}.png")
220
+ prompt = text + ', ' + self.a_prompt
221
+ image = self.pipe(prompt, negative_prompt=self.n_prompt).images[0]
222
+ image.save(image_filename)
223
+ print(
224
+ f"\nProcessed Text2Image, Input Text: {text}, Output Image: {image_filename}")
225
+ return image_filename
226
+
227
+
228
+ class ImageCaptioning:
229
+ def __init__(self, device):
230
+ print(f"Initializing ImageCaptioning to {device}")
231
+ self.device = device
232
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
233
+ self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
234
+ self.model = BlipForConditionalGeneration.from_pretrained(
235
+ "Salesforce/blip-image-captioning-base", torch_dtype=self.torch_dtype).to(self.device)
236
+
237
+ @prompts(name="Get Photo Description",
238
+ description="useful when you want to know what is inside the photo. receives image_path as input. "
239
+ "The input to this tool should be a string, representing the image_path. ")
240
+ def inference(self, image_path):
241
+ inputs = self.processor(Image.open(image_path), return_tensors="pt").to(self.device, self.torch_dtype)
242
+ out = self.model.generate(**inputs)
243
+ captions = self.processor.decode(out[0], skip_special_tokens=True)
244
+ print(f"\nProcessed ImageCaptioning, Input Image: {image_path}, Output Text: {captions}")
245
+ return captions
246
+
247
+
248
+ class Image2Canny:
249
+ def __init__(self, device):
250
+ print("Initializing Image2Canny")
251
+ self.low_threshold = 100
252
+ self.high_threshold = 200
253
+
254
+ @prompts(name="Edge Detection On Image",
255
+ description="useful when you want to detect the edge of the image. "
256
+ "like: detect the edges of this image, or canny detection on image, "
257
+ "or perform edge detection on this image, or detect the canny image of this image. "
258
+ "The input to this tool should be a string, representing the image_path")
259
+ def inference(self, inputs):
260
+ image = Image.open(inputs)
261
+ image = np.array(image)
262
+ canny = cv2.Canny(image, self.low_threshold, self.high_threshold)
263
+ canny = canny[:, :, None]
264
+ canny = np.concatenate([canny, canny, canny], axis=2)
265
+ canny = Image.fromarray(canny)
266
+ updated_image_path = get_new_image_name(inputs, func_name="edge")
267
+ canny.save(updated_image_path)
268
+ print(f"\nProcessed Image2Canny, Input Image: {inputs}, Output Text: {updated_image_path}")
269
+ return updated_image_path
270
+
271
+
272
+ class CannyText2Image:
273
+ def __init__(self, device):
274
+ print(f"Initializing CannyText2Image to {device}")
275
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
276
+ self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-canny",
277
+ torch_dtype=self.torch_dtype)
278
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
279
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
280
+ torch_dtype=self.torch_dtype)
281
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
282
+ self.pipe.to(device)
283
+ self.seed = -1
284
+ self.a_prompt = 'best quality, extremely detailed'
285
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
286
+ 'fewer digits, cropped, worst quality, low quality'
287
+
288
+ @prompts(name="Generate Image Condition On Canny Image",
289
+ description="useful when you want to generate a new real image from both the user description and a canny image."
290
+ " like: generate a real image of a object or something from this canny image,"
291
+ " or generate a new real image of a object or something from this edge image. "
292
+ "The input to this tool should be a comma separated string of two, "
293
+ "representing the image_path and the user description. ")
294
+ def inference(self, inputs):
295
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
296
+ image = Image.open(image_path)
297
+ self.seed = random.randint(0, 65535)
298
+ seed_everything(self.seed)
299
+ prompt = f'{instruct_text}, {self.a_prompt}'
300
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
301
+ guidance_scale=9.0).images[0]
302
+ updated_image_path = get_new_image_name(image_path, func_name="canny2image")
303
+ image.save(updated_image_path)
304
+ print(f"\nProcessed CannyText2Image, Input Canny: {image_path}, Input Text: {instruct_text}, "
305
+ f"Output Text: {updated_image_path}")
306
+ return updated_image_path
307
+
308
+
309
+ class Image2Line:
310
+ def __init__(self, device):
311
+ print("Initializing Image2Line")
312
+ self.detector = MLSDdetector.from_pretrained('lllyasviel/ControlNet')
313
+
314
+ @prompts(name="Line Detection On Image",
315
+ description="useful when you want to detect the straight line of the image. "
316
+ "like: detect the straight lines of this image, or straight line detection on image, "
317
+ "or perform straight line detection on this image, or detect the straight line image of this image. "
318
+ "The input to this tool should be a string, representing the image_path")
319
+ def inference(self, inputs):
320
+ image = Image.open(inputs)
321
+ mlsd = self.detector(image)
322
+ updated_image_path = get_new_image_name(inputs, func_name="line-of")
323
+ mlsd.save(updated_image_path)
324
+ print(f"\nProcessed Image2Line, Input Image: {inputs}, Output Line: {updated_image_path}")
325
+ return updated_image_path
326
+
327
+
328
+ class LineText2Image:
329
+ def __init__(self, device):
330
+ print(f"Initializing LineText2Image to {device}")
331
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
332
+ self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-mlsd",
333
+ torch_dtype=self.torch_dtype)
334
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
335
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
336
+ torch_dtype=self.torch_dtype
337
+ )
338
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
339
+ self.pipe.to(device)
340
+ self.seed = -1
341
+ self.a_prompt = 'best quality, extremely detailed'
342
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
343
+ 'fewer digits, cropped, worst quality, low quality'
344
+
345
+ @prompts(name="Generate Image Condition On Line Image",
346
+ description="useful when you want to generate a new real image from both the user description "
347
+ "and a straight line image. "
348
+ "like: generate a real image of a object or something from this straight line image, "
349
+ "or generate a new real image of a object or something from this straight lines. "
350
+ "The input to this tool should be a comma separated string of two, "
351
+ "representing the image_path and the user description. ")
352
+ def inference(self, inputs):
353
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
354
+ image = Image.open(image_path)
355
+ self.seed = random.randint(0, 65535)
356
+ seed_everything(self.seed)
357
+ prompt = f'{instruct_text}, {self.a_prompt}'
358
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
359
+ guidance_scale=9.0).images[0]
360
+ updated_image_path = get_new_image_name(image_path, func_name="line2image")
361
+ image.save(updated_image_path)
362
+ print(f"\nProcessed LineText2Image, Input Line: {image_path}, Input Text: {instruct_text}, "
363
+ f"Output Text: {updated_image_path}")
364
+ return updated_image_path
365
+
366
+
367
+ class Image2Hed:
368
+ def __init__(self, device):
369
+ print("Initializing Image2Hed")
370
+ self.detector = HEDdetector.from_pretrained('lllyasviel/ControlNet')
371
+
372
+ @prompts(name="Hed Detection On Image",
373
+ description="useful when you want to detect the soft hed boundary of the image. "
374
+ "like: detect the soft hed boundary of this image, or hed boundary detection on image, "
375
+ "or perform hed boundary detection on this image, or detect soft hed boundary image of this image. "
376
+ "The input to this tool should be a string, representing the image_path")
377
+ def inference(self, inputs):
378
+ image = Image.open(inputs)
379
+ hed = self.detector(image)
380
+ updated_image_path = get_new_image_name(inputs, func_name="hed-boundary")
381
+ hed.save(updated_image_path)
382
+ print(f"\nProcessed Image2Hed, Input Image: {inputs}, Output Hed: {updated_image_path}")
383
+ return updated_image_path
384
+
385
+
386
+ class HedText2Image:
387
+ def __init__(self, device):
388
+ print(f"Initializing HedText2Image to {device}")
389
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
390
+ self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-hed",
391
+ torch_dtype=self.torch_dtype)
392
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
393
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
394
+ torch_dtype=self.torch_dtype
395
+ )
396
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
397
+ self.pipe.to(device)
398
+ self.seed = -1
399
+ self.a_prompt = 'best quality, extremely detailed'
400
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
401
+ 'fewer digits, cropped, worst quality, low quality'
402
+
403
+ @prompts(name="Generate Image Condition On Soft Hed Boundary Image",
404
+ description="useful when you want to generate a new real image from both the user description "
405
+ "and a soft hed boundary image. "
406
+ "like: generate a real image of a object or something from this soft hed boundary image, "
407
+ "or generate a new real image of a object or something from this hed boundary. "
408
+ "The input to this tool should be a comma separated string of two, "
409
+ "representing the image_path and the user description")
410
+ def inference(self, inputs):
411
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
412
+ image = Image.open(image_path)
413
+ self.seed = random.randint(0, 65535)
414
+ seed_everything(self.seed)
415
+ prompt = f'{instruct_text}, {self.a_prompt}'
416
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
417
+ guidance_scale=9.0).images[0]
418
+ updated_image_path = get_new_image_name(image_path, func_name="hed2image")
419
+ image.save(updated_image_path)
420
+ print(f"\nProcessed HedText2Image, Input Hed: {image_path}, Input Text: {instruct_text}, "
421
+ f"Output Image: {updated_image_path}")
422
+ return updated_image_path
423
+
424
+
425
+ class Image2Scribble:
426
+ def __init__(self, device):
427
+ print("Initializing Image2Scribble")
428
+ self.detector = HEDdetector.from_pretrained('lllyasviel/ControlNet')
429
+
430
+ @prompts(name="Sketch Detection On Image",
431
+ description="useful when you want to generate a scribble of the image. "
432
+ "like: generate a scribble of this image, or generate a sketch from this image, "
433
+ "detect the sketch from this image. "
434
+ "The input to this tool should be a string, representing the image_path")
435
+ def inference(self, inputs):
436
+ image = Image.open(inputs)
437
+ scribble = self.detector(image, scribble=True)
438
+ updated_image_path = get_new_image_name(inputs, func_name="scribble")
439
+ scribble.save(updated_image_path)
440
+ print(f"\nProcessed Image2Scribble, Input Image: {inputs}, Output Scribble: {updated_image_path}")
441
+ return updated_image_path
442
+
443
+
444
+ class ScribbleText2Image:
445
+ def __init__(self, device):
446
+ print(f"Initializing ScribbleText2Image to {device}")
447
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
448
+ self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-scribble",
449
+ torch_dtype=self.torch_dtype)
450
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
451
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
452
+ torch_dtype=self.torch_dtype
453
+ )
454
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
455
+ self.pipe.to(device)
456
+ self.seed = -1
457
+ self.a_prompt = 'best quality, extremely detailed'
458
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
459
+ 'fewer digits, cropped, worst quality, low quality'
460
+
461
+ @prompts(name="Generate Image Condition On Sketch Image",
462
+ description="useful when you want to generate a new real image from both the user description and "
463
+ "a scribble image or a sketch image. "
464
+ "The input to this tool should be a comma separated string of two, "
465
+ "representing the image_path and the user description")
466
+ def inference(self, inputs):
467
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
468
+ image = Image.open(image_path)
469
+ self.seed = random.randint(0, 65535)
470
+ seed_everything(self.seed)
471
+ prompt = f'{instruct_text}, {self.a_prompt}'
472
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
473
+ guidance_scale=9.0).images[0]
474
+ updated_image_path = get_new_image_name(image_path, func_name="scribble2image")
475
+ image.save(updated_image_path)
476
+ print(f"\nProcessed ScribbleText2Image, Input Scribble: {image_path}, Input Text: {instruct_text}, "
477
+ f"Output Image: {updated_image_path}")
478
+ return updated_image_path
479
+
480
+
481
+ class Image2Pose:
482
+ def __init__(self, device):
483
+ print("Initializing Image2Pose")
484
+ self.detector = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
485
+
486
+ @prompts(name="Pose Detection On Image",
487
+ description="useful when you want to detect the human pose of the image. "
488
+ "like: generate human poses of this image, or generate a pose image from this image. "
489
+ "The input to this tool should be a string, representing the image_path")
490
+ def inference(self, inputs):
491
+ image = Image.open(inputs)
492
+ pose = self.detector(image)
493
+ updated_image_path = get_new_image_name(inputs, func_name="human-pose")
494
+ pose.save(updated_image_path)
495
+ print(f"\nProcessed Image2Pose, Input Image: {inputs}, Output Pose: {updated_image_path}")
496
+ return updated_image_path
497
+
498
+
499
+ class PoseText2Image:
500
+ def __init__(self, device):
501
+ print(f"Initializing PoseText2Image to {device}")
502
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
503
+ self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-openpose",
504
+ torch_dtype=self.torch_dtype)
505
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
506
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
507
+ torch_dtype=self.torch_dtype)
508
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
509
+ self.pipe.to(device)
510
+ self.num_inference_steps = 20
511
+ self.seed = -1
512
+ self.unconditional_guidance_scale = 9.0
513
+ self.a_prompt = 'best quality, extremely detailed'
514
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
515
+ ' fewer digits, cropped, worst quality, low quality'
516
+
517
+ @prompts(name="Generate Image Condition On Pose Image",
518
+ description="useful when you want to generate a new real image from both the user description "
519
+ "and a human pose image. "
520
+ "like: generate a real image of a human from this human pose image, "
521
+ "or generate a new real image of a human from this pose. "
522
+ "The input to this tool should be a comma separated string of two, "
523
+ "representing the image_path and the user description")
524
+ def inference(self, inputs):
525
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
526
+ image = Image.open(image_path)
527
+ self.seed = random.randint(0, 65535)
528
+ seed_everything(self.seed)
529
+ prompt = f'{instruct_text}, {self.a_prompt}'
530
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
531
+ guidance_scale=9.0).images[0]
532
+ updated_image_path = get_new_image_name(image_path, func_name="pose2image")
533
+ image.save(updated_image_path)
534
+ print(f"\nProcessed PoseText2Image, Input Pose: {image_path}, Input Text: {instruct_text}, "
535
+ f"Output Image: {updated_image_path}")
536
+ return updated_image_path
537
+
538
+
539
+ class Image2Seg:
540
+ def __init__(self, device):
541
+ print("Initializing Image2Seg")
542
+ self.image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-small")
543
+ self.image_segmentor = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-small")
544
+ self.ade_palette = [[120, 120, 120], [180, 120, 120], [6, 230, 230], [80, 50, 50],
545
+ [4, 200, 3], [120, 120, 80], [140, 140, 140], [204, 5, 255],
546
+ [230, 230, 230], [4, 250, 7], [224, 5, 255], [235, 255, 7],
547
+ [150, 5, 61], [120, 120, 70], [8, 255, 51], [255, 6, 82],
548
+ [143, 255, 140], [204, 255, 4], [255, 51, 7], [204, 70, 3],
549
+ [0, 102, 200], [61, 230, 250], [255, 6, 51], [11, 102, 255],
550
+ [255, 7, 71], [255, 9, 224], [9, 7, 230], [220, 220, 220],
551
+ [255, 9, 92], [112, 9, 255], [8, 255, 214], [7, 255, 224],
552
+ [255, 184, 6], [10, 255, 71], [255, 41, 10], [7, 255, 255],
553
+ [224, 255, 8], [102, 8, 255], [255, 61, 6], [255, 194, 7],
554
+ [255, 122, 8], [0, 255, 20], [255, 8, 41], [255, 5, 153],
555
+ [6, 51, 255], [235, 12, 255], [160, 150, 20], [0, 163, 255],
556
+ [140, 140, 140], [250, 10, 15], [20, 255, 0], [31, 255, 0],
557
+ [255, 31, 0], [255, 224, 0], [153, 255, 0], [0, 0, 255],
558
+ [255, 71, 0], [0, 235, 255], [0, 173, 255], [31, 0, 255],
559
+ [11, 200, 200], [255, 82, 0], [0, 255, 245], [0, 61, 255],
560
+ [0, 255, 112], [0, 255, 133], [255, 0, 0], [255, 163, 0],
561
+ [255, 102, 0], [194, 255, 0], [0, 143, 255], [51, 255, 0],
562
+ [0, 82, 255], [0, 255, 41], [0, 255, 173], [10, 0, 255],
563
+ [173, 255, 0], [0, 255, 153], [255, 92, 0], [255, 0, 255],
564
+ [255, 0, 245], [255, 0, 102], [255, 173, 0], [255, 0, 20],
565
+ [255, 184, 184], [0, 31, 255], [0, 255, 61], [0, 71, 255],
566
+ [255, 0, 204], [0, 255, 194], [0, 255, 82], [0, 10, 255],
567
+ [0, 112, 255], [51, 0, 255], [0, 194, 255], [0, 122, 255],
568
+ [0, 255, 163], [255, 153, 0], [0, 255, 10], [255, 112, 0],
569
+ [143, 255, 0], [82, 0, 255], [163, 255, 0], [255, 235, 0],
570
+ [8, 184, 170], [133, 0, 255], [0, 255, 92], [184, 0, 255],
571
+ [255, 0, 31], [0, 184, 255], [0, 214, 255], [255, 0, 112],
572
+ [92, 255, 0], [0, 224, 255], [112, 224, 255], [70, 184, 160],
573
+ [163, 0, 255], [153, 0, 255], [71, 255, 0], [255, 0, 163],
574
+ [255, 204, 0], [255, 0, 143], [0, 255, 235], [133, 255, 0],
575
+ [255, 0, 235], [245, 0, 255], [255, 0, 122], [255, 245, 0],
576
+ [10, 190, 212], [214, 255, 0], [0, 204, 255], [20, 0, 255],
577
+ [255, 255, 0], [0, 153, 255], [0, 41, 255], [0, 255, 204],
578
+ [41, 0, 255], [41, 255, 0], [173, 0, 255], [0, 245, 255],
579
+ [71, 0, 255], [122, 0, 255], [0, 255, 184], [0, 92, 255],
580
+ [184, 255, 0], [0, 133, 255], [255, 214, 0], [25, 194, 194],
581
+ [102, 255, 0], [92, 0, 255]]
582
+
583
+ @prompts(name="Segmentation On Image",
584
+ description="useful when you want to detect segmentations of the image. "
585
+ "like: segment this image, or generate segmentations on this image, "
586
+ "or perform segmentation on this image. "
587
+ "The input to this tool should be a string, representing the image_path")
588
+ def inference(self, inputs):
589
+ image = Image.open(inputs)
590
+ pixel_values = self.image_processor(image, return_tensors="pt").pixel_values
591
+ with torch.no_grad():
592
+ outputs = self.image_segmentor(pixel_values)
593
+ seg = self.image_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
594
+ color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3
595
+ palette = np.array(self.ade_palette)
596
+ for label, color in enumerate(palette):
597
+ color_seg[seg == label, :] = color
598
+ color_seg = color_seg.astype(np.uint8)
599
+ segmentation = Image.fromarray(color_seg)
600
+ updated_image_path = get_new_image_name(inputs, func_name="segmentation")
601
+ segmentation.save(updated_image_path)
602
+ print(f"\nProcessed Image2Seg, Input Image: {inputs}, Output Pose: {updated_image_path}")
603
+ return updated_image_path
604
+
605
+
606
+ class SegText2Image:
607
+ def __init__(self, device):
608
+ print(f"Initializing SegText2Image to {device}")
609
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
610
+ self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-seg",
611
+ torch_dtype=self.torch_dtype)
612
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
613
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
614
+ torch_dtype=self.torch_dtype)
615
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
616
+ self.pipe.to(device)
617
+ self.seed = -1
618
+ self.a_prompt = 'best quality, extremely detailed'
619
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
620
+ ' fewer digits, cropped, worst quality, low quality'
621
+
622
+ @prompts(name="Generate Image Condition On Segmentations",
623
+ description="useful when you want to generate a new real image from both the user description and segmentations. "
624
+ "like: generate a real image of a object or something from this segmentation image, "
625
+ "or generate a new real image of a object or something from these segmentations. "
626
+ "The input to this tool should be a comma separated string of two, "
627
+ "representing the image_path and the user description")
628
+ def inference(self, inputs):
629
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
630
+ image = Image.open(image_path)
631
+ self.seed = random.randint(0, 65535)
632
+ seed_everything(self.seed)
633
+ prompt = f'{instruct_text}, {self.a_prompt}'
634
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
635
+ guidance_scale=9.0).images[0]
636
+ updated_image_path = get_new_image_name(image_path, func_name="segment2image")
637
+ image.save(updated_image_path)
638
+ print(f"\nProcessed SegText2Image, Input Seg: {image_path}, Input Text: {instruct_text}, "
639
+ f"Output Image: {updated_image_path}")
640
+ return updated_image_path
641
+
642
+
643
+ class Image2Depth:
644
+ def __init__(self, device):
645
+ print("Initializing Image2Depth")
646
+ self.depth_estimator = pipeline('depth-estimation')
647
+
648
+ @prompts(name="Predict Depth On Image",
649
+ description="useful when you want to detect depth of the image. like: generate the depth from this image, "
650
+ "or detect the depth map on this image, or predict the depth for this image. "
651
+ "The input to this tool should be a string, representing the image_path")
652
+ def inference(self, inputs):
653
+ image = Image.open(inputs)
654
+ depth = self.depth_estimator(image)['depth']
655
+ depth = np.array(depth)
656
+ depth = depth[:, :, None]
657
+ depth = np.concatenate([depth, depth, depth], axis=2)
658
+ depth = Image.fromarray(depth)
659
+ updated_image_path = get_new_image_name(inputs, func_name="depth")
660
+ depth.save(updated_image_path)
661
+ print(f"\nProcessed Image2Depth, Input Image: {inputs}, Output Depth: {updated_image_path}")
662
+ return updated_image_path
663
+
664
+
665
+ class DepthText2Image:
666
+ def __init__(self, device):
667
+ print(f"Initializing DepthText2Image to {device}")
668
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
669
+ self.controlnet = ControlNetModel.from_pretrained(
670
+ "fusing/stable-diffusion-v1-5-controlnet-depth", torch_dtype=self.torch_dtype)
671
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
672
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
673
+ torch_dtype=self.torch_dtype)
674
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
675
+ self.pipe.to(device)
676
+ self.seed = -1
677
+ self.a_prompt = 'best quality, extremely detailed'
678
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
679
+ ' fewer digits, cropped, worst quality, low quality'
680
+
681
+ @prompts(name="Generate Image Condition On Depth",
682
+ description="useful when you want to generate a new real image from both the user description and depth image. "
683
+ "like: generate a real image of a object or something from this depth image, "
684
+ "or generate a new real image of a object or something from the depth map. "
685
+ "The input to this tool should be a comma separated string of two, "
686
+ "representing the image_path and the user description")
687
+ def inference(self, inputs):
688
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
689
+ image = Image.open(image_path)
690
+ self.seed = random.randint(0, 65535)
691
+ seed_everything(self.seed)
692
+ prompt = f'{instruct_text}, {self.a_prompt}'
693
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
694
+ guidance_scale=9.0).images[0]
695
+ updated_image_path = get_new_image_name(image_path, func_name="depth2image")
696
+ image.save(updated_image_path)
697
+ print(f"\nProcessed DepthText2Image, Input Depth: {image_path}, Input Text: {instruct_text}, "
698
+ f"Output Image: {updated_image_path}")
699
+ return updated_image_path
700
+
701
+
702
+ class Image2Normal:
703
+ def __init__(self, device):
704
+ print("Initializing Image2Normal")
705
+ self.depth_estimator = pipeline("depth-estimation", model="Intel/dpt-hybrid-midas")
706
+ self.bg_threhold = 0.4
707
+
708
+ @prompts(name="Predict Normal Map On Image",
709
+ description="useful when you want to detect norm map of the image. "
710
+ "like: generate normal map from this image, or predict normal map of this image. "
711
+ "The input to this tool should be a string, representing the image_path")
712
+ def inference(self, inputs):
713
+ image = Image.open(inputs)
714
+ original_size = image.size
715
+ image = self.depth_estimator(image)['predicted_depth'][0]
716
+ image = image.numpy()
717
+ image_depth = image.copy()
718
+ image_depth -= np.min(image_depth)
719
+ image_depth /= np.max(image_depth)
720
+ x = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3)
721
+ x[image_depth < self.bg_threhold] = 0
722
+ y = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3)
723
+ y[image_depth < self.bg_threhold] = 0
724
+ z = np.ones_like(x) * np.pi * 2.0
725
+ image = np.stack([x, y, z], axis=2)
726
+ image /= np.sum(image ** 2.0, axis=2, keepdims=True) ** 0.5
727
+ image = (image * 127.5 + 127.5).clip(0, 255).astype(np.uint8)
728
+ image = Image.fromarray(image)
729
+ image = image.resize(original_size)
730
+ updated_image_path = get_new_image_name(inputs, func_name="normal-map")
731
+ image.save(updated_image_path)
732
+ print(f"\nProcessed Image2Normal, Input Image: {inputs}, Output Depth: {updated_image_path}")
733
+ return updated_image_path
734
+
735
+
736
+ class NormalText2Image:
737
+ def __init__(self, device):
738
+ print(f"Initializing NormalText2Image to {device}")
739
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
740
+ self.controlnet = ControlNetModel.from_pretrained(
741
+ "fusing/stable-diffusion-v1-5-controlnet-normal", torch_dtype=self.torch_dtype)
742
+ self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
743
+ "runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None,
744
+ torch_dtype=self.torch_dtype)
745
+ self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)
746
+ self.pipe.to(device)
747
+ self.seed = -1
748
+ self.a_prompt = 'best quality, extremely detailed'
749
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit,' \
750
+ ' fewer digits, cropped, worst quality, low quality'
751
+
752
+ @prompts(name="Generate Image Condition On Normal Map",
753
+ description="useful when you want to generate a new real image from both the user description and normal map. "
754
+ "like: generate a real image of a object or something from this normal map, "
755
+ "or generate a new real image of a object or something from the normal map. "
756
+ "The input to this tool should be a comma separated string of two, "
757
+ "representing the image_path and the user description")
758
+ def inference(self, inputs):
759
+ image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
760
+ image = Image.open(image_path)
761
+ self.seed = random.randint(0, 65535)
762
+ seed_everything(self.seed)
763
+ prompt = f'{instruct_text}, {self.a_prompt}'
764
+ image = self.pipe(prompt, image, num_inference_steps=20, eta=0.0, negative_prompt=self.n_prompt,
765
+ guidance_scale=9.0).images[0]
766
+ updated_image_path = get_new_image_name(image_path, func_name="normal2image")
767
+ image.save(updated_image_path)
768
+ print(f"\nProcessed NormalText2Image, Input Normal: {image_path}, Input Text: {instruct_text}, "
769
+ f"Output Image: {updated_image_path}")
770
+ return updated_image_path
771
+
772
+
773
+ class VisualQuestionAnswering:
774
+ def __init__(self, device):
775
+ print(f"Initializing VisualQuestionAnswering to {device}")
776
+ self.torch_dtype = torch.float16 if 'cuda' in device else torch.float32
777
+ self.device = device
778
+ self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
779
+ self.model = BlipForQuestionAnswering.from_pretrained(
780
+ "Salesforce/blip-vqa-base", torch_dtype=self.torch_dtype).to(self.device)
781
+
782
+ @prompts(name="Answer Question About The Image",
783
+ description="useful when you need an answer for a question based on an image. "
784
+ "like: what is the background color of the last image, how many cats in this figure, what is in this figure. "
785
+ "The input to this tool should be a comma separated string of two, representing the image_path and the question")
786
+ def inference(self, inputs):
787
+ image_path, question = inputs.split(",")[0], ','.join(inputs.split(',')[1:])
788
+ raw_image = Image.open(image_path).convert('RGB')
789
+ inputs = self.processor(raw_image, question, return_tensors="pt").to(self.device, self.torch_dtype)
790
+ out = self.model.generate(**inputs)
791
+ answer = self.processor.decode(out[0], skip_special_tokens=True)
792
+ print(f"\nProcessed VisualQuestionAnswering, Input Image: {image_path}, Input Question: {question}, "
793
+ f"Output Answer: {answer}")
794
+ return answer
795
+
796
+ class InfinityOutPainting:
797
+ template_model = True # Add this line to show this is a template model.
798
+ def __init__(self, ImageCaptioning, ImageEditing, VisualQuestionAnswering):
799
+ # self.llm = OpenAI(temperature=0)
800
+ self.ImageCaption = ImageCaptioning
801
+ self.ImageEditing = ImageEditing
802
+ self.ImageVQA = VisualQuestionAnswering
803
+ self.a_prompt = 'best quality, extremely detailed'
804
+ self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
805
+ 'fewer digits, cropped, worst quality, low quality'
806
+
807
+ def get_BLIP_vqa(self, image, question):
808
+ inputs = self.ImageVQA.processor(image, question, return_tensors="pt").to(self.ImageVQA.device,
809
+ self.ImageVQA.torch_dtype)
810
+ out = self.ImageVQA.model.generate(**inputs)
811
+ answer = self.ImageVQA.processor.decode(out[0], skip_special_tokens=True)
812
+ print(f"\nProcessed VisualQuestionAnswering, Input Question: {question}, Output Answer: {answer}")
813
+ return answer
814
+
815
+ def get_BLIP_caption(self, image):
816
+ inputs = self.ImageCaption.processor(image, return_tensors="pt").to(self.ImageCaption.device,
817
+ self.ImageCaption.torch_dtype)
818
+ out = self.ImageCaption.model.generate(**inputs)
819
+ BLIP_caption = self.ImageCaption.processor.decode(out[0], skip_special_tokens=True)
820
+ return BLIP_caption
821
+
822
+ # def check_prompt(self, prompt):
823
+ # check = f"Here is a paragraph with adjectives. " \
824
+ # f"{prompt} " \
825
+ # f"Please change all plural forms in the adjectives to singular forms. "
826
+ # return self.llm(check)
827
+
828
+ def get_imagine_caption(self, image, imagine):
829
+ BLIP_caption = self.get_BLIP_caption(image)
830
+ background_color = self.get_BLIP_vqa(image, 'what is the background color of this image')
831
+ style = self.get_BLIP_vqa(image, 'what is the style of this image')
832
+ imagine_prompt = f"let's pretend you are an excellent painter and now " \
833
+ f"there is an incomplete painting with {BLIP_caption} in the center, " \
834
+ f"please imagine the complete painting and describe it" \
835
+ f"you should consider the background color is {background_color}, the style is {style}" \
836
+ f"You should make the painting as vivid and realistic as possible" \
837
+ f"You can not use words like painting or picture" \
838
+ f"and you should use no more than 50 words to describe it"
839
+ # caption = self.llm(imagine_prompt) if imagine else BLIP_caption
840
+ caption = BLIP_caption
841
+ # caption = self.check_prompt(caption)
842
+ print(f'BLIP observation: {BLIP_caption}, ChatGPT imagine to {caption}') if imagine else print(
843
+ f'Prompt: {caption}')
844
+ return caption
845
+
846
+ def resize_image(self, image, max_size=100000, multiple=8):
847
+ aspect_ratio = image.size[0] / image.size[1]
848
+ new_width = int(math.sqrt(max_size * aspect_ratio))
849
+ new_height = int(new_width / aspect_ratio)
850
+ new_width, new_height = new_width - (new_width % multiple), new_height - (new_height % multiple)
851
+ return image.resize((new_width, new_height))
852
+
853
+ def dowhile(self, original_img, tosize, expand_ratio, imagine, usr_prompt):
854
+ old_img = original_img
855
+ while (old_img.size != tosize):
856
+ prompt = self.check_prompt(usr_prompt) if usr_prompt else self.get_imagine_caption(old_img, imagine)
857
+ crop_w = 15 if old_img.size[0] != tosize[0] else 0
858
+ crop_h = 15 if old_img.size[1] != tosize[1] else 0
859
+ old_img = ImageOps.crop(old_img, (crop_w, crop_h, crop_w, crop_h))
860
+ temp_canvas_size = (expand_ratio * old_img.width if expand_ratio * old_img.width < tosize[0] else tosize[0],
861
+ expand_ratio * old_img.height if expand_ratio * old_img.height < tosize[1] else tosize[
862
+ 1])
863
+ temp_canvas, temp_mask = Image.new("RGB", temp_canvas_size, color="white"), Image.new("L", temp_canvas_size,
864
+ color="white")
865
+ x, y = (temp_canvas.width - old_img.width) // 2, (temp_canvas.height - old_img.height) // 2
866
+ temp_canvas.paste(old_img, (x, y))
867
+ temp_mask.paste(0, (x, y, x + old_img.width, y + old_img.height))
868
+ resized_temp_canvas, resized_temp_mask = self.resize_image(temp_canvas), self.resize_image(temp_mask)
869
+ image = self.ImageEditing.inpaint(prompt=prompt, image=resized_temp_canvas, mask_image=resized_temp_mask,
870
+ height=resized_temp_canvas.height, width=resized_temp_canvas.width,
871
+ num_inference_steps=50).images[0].resize(
872
+ (temp_canvas.width, temp_canvas.height), Image.ANTIALIAS)
873
+ image = blend_gt2pt(old_img, image)
874
+ old_img = image
875
+ return old_img
876
+
877
+ @prompts(name="Extend An Image",
878
+ description="useful when you need to extend an image into a larger image."
879
+ "like: extend the image into a resolution of 2048x1024, extend the image into 2048x1024. "
880
+ "The input to this tool should be a comma separated string of two, representing the image_path and the resolution of widthxheight")
881
+ def inference(self, inputs):
882
+ image_path, resolution = inputs.split(',')
883
+ width, height = resolution.split('x')
884
+ tosize = (int(width), int(height))
885
+ image = Image.open(image_path)
886
+ image = ImageOps.crop(image, (10, 10, 10, 10))
887
+ out_painted_image = self.dowhile(image, tosize, 4, True, False)
888
+ updated_image_path = get_new_image_name(image_path, func_name="outpainting")
889
+ out_painted_image.save(updated_image_path)
890
+ print(f"\nProcessed InfinityOutPainting, Input Image: {image_path}, Input Resolution: {resolution}, "
891
+ f"Output Image: {updated_image_path}")
892
+ return updated_image_path