multiTurnT2I_app.py 13.1 KB
Newer Older
jerrrrry's avatar
jerrrrry committed
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
# -- coding: utf-8 --
#!/usr/bin/env python
import gradio as gr
from PIL import Image
import sys
import os

sys.path.append(os.getcwd())
import json
import numpy as np
from pathlib import Path
import io
import hashlib
import requests
import base64
import pandas as pd
from sample_t2i import inferencer
from mllm.dialoggen_demo import init_dialoggen_model, eval_model

SIZES = {
    "正方形(square, 1024x1024)": (1024, 1024),
    "风景(landscape, 1280x768)": (768, 1280),
    "人像(portrait, 768x1280)": (1280, 768),
}

global_seed = np.random.randint(0, 10000)


# Helper Functions
def image_to_base64(image_path):
    with open(image_path, "rb") as image_file:
        encoded_image = base64.b64encode(image_file.read()).decode()
    return encoded_image


def get_strings(lang):
    lang_file = Path(f"app/lang/{lang}.csv")
    strings = pd.read_csv(lang_file, header=0)
    strings = strings.set_index("key")["value"].to_dict()
    return strings


def get_image_md5(image):
    image_data = io.BytesIO()
    image.save(image_data, format="PNG")
    image_data = image_data.getvalue()
    md5_hash = hashlib.md5(image_data).hexdigest()
    return md5_hash


# mllm调用
def request_dialogGen(
    server_url="http://0.0.0.0:8080",
    history_messages=[],
    question="画一个木制的鸟",
    image="",
):
    if image != "":
        image = base64.b64encode(open(image, "rb").read()).decode()
    print("history_messages before request", history_messages)
    headers = {"accept": "application/json", "Content-Type": "application/json"}
    data = {
        "text": question,
        "image": image,  # "image为空字符串,则进行文本对话"
        "history": history_messages,
    }
    response = requests.post(server_url, headers=headers, json=data)
    print("response", response)
    response = response.json()
    print(response)
    response_text = response["result"]
    history_messages = response["history"]
    print("history_messages before request", history_messages)
    return history_messages, response_text


# 画图
def image_generation(prompt, infer_steps, seed, image_size):
    print(
        f"prompt sent to T2I model: {prompt}, infer_steps: {infer_steps}, seed: {seed}, size: {image_size}"
    )
    height, width = SIZES[image_size]
    results = gen.predict(
        prompt,
        height=height,
        width=width,
        seed=seed,
        infer_steps=infer_steps,
        batch_size=1,
    )
    image = results["images"][0]
    file_name = get_image_md5(image)
    # Save images
    save_dir = Path("results")
    save_dir.mkdir(exist_ok=True)
    save_path = f"results/multiRound_{file_name}.png"
    image.save(save_path)
    encoded_image = image_to_base64(save_path)

    return encoded_image


# 图文对话
def chat(history_messages, input_text):

    history_messages, response_text = request_dialogGen(
        history_messages=history_messages, question=input_text
    )
    return history_messages, response_text


#
def pipeline(input_text, state, infer_steps, seed, image_size):

    # 忽略空输入
    if len(input_text) == 0:
        return state, state[0]

    conversation = state[0]
    history_messages = state[1]

    system_prompt = "请先判断用户的意图,若为画图则在输出前加入<画图>:"
    print(f"input history:{history_messages}")
    if not isinstance(history_messages, list) and len(history_messages.messages) >= 2:
        response, history_messages = enhancer(
            input_text, return_history=True, history=history_messages, skip_special=True
        )
    else:
        response, history_messages = enhancer(
            input_text,
            return_history=True,
            history=history_messages,
            skip_special=False,
        )

    history_messages.messages[-1][-1] = response

    if "<画图>" in response:
        intention_draw = True
    else:
        intention_draw = False

    print(f"response:{response}")
    print("-" * 80)
    print(f"history_messages:{history_messages}")
    print(f"intention_draw:{intention_draw}")
    if intention_draw:
        prompt = response.split("<画图>")[-1]
        # 画图
        image_url = image_generation(prompt, infer_steps, seed, image_size)
        response = f'<img src="data:image/png;base64,{image_url}" style="display: inline-block;"><p style="font-size: 14px; color: #555; margin-top: 0;">{prompt}</p>'
    conversation += [((input_text, response))]
    return [conversation, history_messages], conversation


# 页面设计
def upload_image(state, image_input):
    conversation = state[0]
    history_messages = state[1]
    input_image = Image.open(image_input.name).resize((224, 224)).convert("RGB")
    input_image.save(image_input.name)  # Overwrite with smaller image.
    system_prompt = "请先判断用户的意图,若为画图则在输出前加入<画图>:"
    history_messages, response = request_dialogGen(
        question="这张图描述了什么?",
        history_messages=history_messages,
        image=image_input.name,
    )
    conversation += [
        (
            f'<img src="./file={image_input.name}"  style="display: inline-block;">',
            response,
        )
    ]
    print("conversation", conversation)
    print("history_messages after uploading image", history_messages)
    return [conversation, history_messages], conversation


def reset():
    global global_seed
    global_seed = np.random.randint(0, 10000)
    return [[], []], []


def reset_last(state):
    conversation, history = state[0], state[1]
    conversation = conversation[:-1]
    history.messages = history.messages[:-2]
    return [conversation, history], conversation


if __name__ == "__main__":

    # Initialize dialoggen and HunyuanDiT model
    args, gen, enhancer = inferencer()
    strings = get_strings(args.lang)

    css = """
        #chatbot { min-height: 800px; }
        #save-btn {
            background-image: linear-gradient(to right bottom, rgba(130,217,244, 0.9), rgba(158,231,214, 1.0));
        }
        #save-btn:hover {
            background-image: linear-gradient(to right bottom, rgba(110,197,224, 0.9), rgba(138,211,194, 1.0));
        }
        #share-btn {
            background-image: linear-gradient(to right bottom, rgba(130,217,244, 0.9), rgba(158,231,214, 1.0));
        }
        #share-btn:hover {
            background-image: linear-gradient(to right bottom, rgba(110,197,224, 0.9), rgba(138,211,194, 1.0));
        }
        #gallery { z-index: 999999; }
        #gallery img:hover {transform: scale(2.3); z-index: 999999; position: relative; padding-right: 30%; padding-bottom: 30%;}
        #gallery button img:hover {transform: none; z-index: 999999; position: relative; padding-right: 0; padding-bottom: 0;}
        @media (hover: none) {
            #gallery img:hover {transform: none; z-index: 999999; position: relative; padding-right: 0; 0;}
        }
        .html2canvas-container { width: 3000px !important; height: 3000px !important; }
    """

    with gr.Blocks(css=css) as demo:
        DESCRIPTION = """# <a style="color: black; text-decoration: none;">多轮对话绘图 Multi-turn Text2Image Generation</a>
            你可以参照[DialogGen](https://arxiv.org/abs/2403.08857),通过简单的交互式语句来进行历史图片的修改,例如:主体编辑、增加主体、删除主体、背景更换、风格转换、镜头转换、图像合并。

            (You can modify historical images through simple interactive statements referred to [DialogGen](https://arxiv.org/abs/2403.08857), such as: enity edit, add object, remove object, change background, change style, change lens, and combine images. )
            
            例如,主体编辑 (For example, enity edit) :
            ```none
            Round1: 画一个木制的鸟
            (Round1: draw a wooden bird)
            
            Round2: 变成玻璃的
            (Round2: turn into glass)
            ```
        """

        gr.Markdown(DESCRIPTION)
        gr_state = gr.State([[], []])  # conversation, chat_history

        with gr.Row():
            with gr.Column(scale=1, min_width=1000):
                with gr.Row():
                    chatbot = gr.Chatbot(
                        elem_id="chatbot", label="DialogGen&HunyuanDiT"
                    )
                with gr.Row():
                    infer_steps = gr.Slider(
                        label="采样步数(sampling steps)",
                        minimum=1,
                        maximum=200,
                        value=100,
                        step=1,
                    )
                    seed = gr.Number(
                        label="种子(seed)",
                        minimum=-1,
                        maximum=1_000_000_000,
                        value=666,
                        step=1,
                        precision=0,
                    )
                    size_dropdown = gr.Dropdown(
                        choices=[
                            "正方形(square, 1024x1024)",
                            "风景(landscape, 1280x768)",
                            "人像(portrait, 768x1280)",
                        ],
                        value="正方形(square, 1024x1024)",
                        label="图片尺寸(Image Size)",
                    )

                with gr.Row():
                    # image_btn = gr.UploadButton("🖼️ Upload Image", file_types=["image"])
                    text_input = gr.Textbox(
                        label="提示词(prompt)", placeholder="输入提示词(Type a prompt)"
                    )

                    with gr.Column():
                        submit_btn = gr.Button(
                            "提交(Submit)", interactive=True, variant="primary"
                        )
                        clear_last_btn = gr.Button("回退(Undo)")
                        clear_btn = gr.Button("全部重置(Reset All)")
                with gr.Row():
                    gr.Examples(
                        [
                            ["画一个木制的鸟"],
                            ["一只小猫"],
                            [
                                "现实主义风格,画面主要描述一个巴洛克风格的花瓶,带有金色的装饰边框,花瓶上盛开着各种色彩鲜艳的花,白色背景"
                            ],
                            [
                                "一只聪明的狐狸走在阔叶树林里, 旁边是一条小溪, 细节真实, 摄影"
                            ],
                            ["飞流直下三千尺,疑是银河落九天"],
                            [
                                "一只长靴猫手持亮银色的宝剑,身着铠甲,眼神坚毅,站在一堆金币上,背景是暗色调的洞穴,图像上有金币的光影点缀。"
                            ],
                            ["麻婆豆腐"],
                            ["苏州园林"],
                            [
                                "一颗新鲜的草莓特写,红色的外表,表面布满许多种子,背景是淡绿色的叶子"
                            ],
                            ["枯藤老树昏鸦,小桥流水人家"],
                            [
                                "湖水清澈,天空湛蓝,阳光灿烂。一只优雅的白天鹅在湖边游泳。它周围有几只小鸭子,看起来非常可爱,整个画面给人一种宁静祥和的感觉。"
                            ],
                            [
                                "一朵鲜艳的红色玫瑰花,花瓣撒有一些水珠,晶莹剔透,特写镜头"
                            ],
                            ["臭豆腐"],
                            ["九寨沟"],
                            ["俗语“鲤鱼跃龙门”"],
                            [
                                "风格是写实,画面主要描述一个亚洲戏曲艺术家正在表演,她穿着华丽的戏服,脸上戴着精致的面具,身姿优雅,背景是古色古香的舞台,镜头是近景"
                            ],
                        ],
                        [text_input],
                        label=strings["examples"],
                    )
                gr.Markdown(
                    """<p style="font-size: 20px; color: #888;">powered by <a href="https://github.com/Centaurusalpha/DialogGen" target="_blank">DialogGen</a> and <a href="https://github.com/Tencent/HunyuanDiT" target="_blank">HunyuanDiT</a></p>"""
                )

        text_input.submit(
            pipeline,
            [text_input, gr_state, infer_steps, seed, size_dropdown],
            [gr_state, chatbot],
        )
        text_input.submit(lambda: "", None, text_input)  # Reset chatbox.
        submit_btn.click(
            pipeline,
            [text_input, gr_state, infer_steps, seed, size_dropdown],
            [gr_state, chatbot],
        )
        submit_btn.click(lambda: "", None, text_input)  # Reset chatbox.

        # image_btn.upload(upload_image, [gr_state, image_btn], [gr_state, chatbot])
        clear_last_btn.click(reset_last, [gr_state], [gr_state, chatbot])
        clear_btn.click(reset, [], [gr_state, chatbot])

    interface = demo
    interface.launch(server_name="0.0.0.0", server_port=443, share=False)