File size: 6,544 Bytes
3cde043
4f16bf7
3cde043
ee35a78
b053537
03bee81
 
 
3cde043
ee35a78
03bee81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee35a78
 
 
03bee81
 
 
 
 
 
 
 
 
ee35a78
 
03bee81
ee35a78
03bee81
 
 
ee35a78
 
 
 
 
03bee81
 
ee35a78
03bee81
 
ee35a78
03bee81
ee35a78
03bee81
 
ee35a78
 
 
 
 
 
 
 
 
 
 
03bee81
ee35a78
03bee81
 
 
ee35a78
 
03bee81
ee35a78
 
 
 
 
 
 
 
 
 
 
 
 
 
52bc8d5
ee35a78
 
 
 
 
 
 
 
 
 
 
03bee81
ee35a78
03bee81
 
 
 
 
ee35a78
03bee81
ee35a78
 
03bee81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee35a78
 
 
 
 
 
 
03bee81
 
 
 
 
ee35a78
 
03bee81
 
ee35a78
 
 
03bee81
ee35a78
 
 
 
 
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
import gradio as gr
from PIL import Image
import time
import threading
import tqdm
# -------------------------------model--------------------------
import subprocess
import sys


def run_model(txt='wqq'):
    print("start running model")
    ckpt_path = '/home/zhutiantian/code/One-DM/One-DM-ckpt.pt'
    dir_path = '/home/zhutiantian/code/One-DM/Generated/English'
    code_path = '/home/zhutiantian/code/One-DM/test.py'

    # 构造 Conda 命令
    conda_path = "/home/zhutiantian/anaconda3/condabin/conda"
    command = f"export LD_LIBRARY_PATH=/usr/lib/wsl/lib:$LD_LIBRARY_PATH && cd /home/zhutiantian/code/One-DM && {conda_path} run -n torch13 CUDA_VISIBLE_DEVICES=0 torchrun --nproc_per_node=1 {code_path} --one_dm {ckpt_path} --generate_type oov_u --dir {dir_path} --input_text {txt}"

    subprocess.run(command, shell=True)


# -------------------------------gradio-------------------------

# 所有button响应的函数

# 任务处理函数
def process_task(txt, p1, p2, p3, p4, p5):
    # 这里需要添加对txt的检查!
    # txt为空、非英文字母时,返回错误,要求重新填写
    # p3、p5 非空
    # p1: 时间,p2:图片数量,p4:步数
    # p3:等于 "DDIM" 或 "DDPM",生成方式
    # p5:等于 "iv_s","iv_u","oov_s","oov_u",生成类型


    # 启动一个 15 秒的命令模拟
    def execute_command():
        # 这里用time.sleep(50)来模拟命令执行
        # get_wsl.open_wsl_and_run_model()  # 单独修改UI的时候把这里换成time.sleep(15)

        # run_model(txt)
        time.sleep(5)
        print("命令执行完毕!")

    # 启动执行命令的线程
    command_thread = threading.Thread(target=execute_command)
    command_thread.start()
    time.sleep(5)
    command_thread.join()  # 等待命令执行完成

    # img_path = f'/home/zhutiantian/code/One-DM/Generated/English/oov_u/168/{txt}.png'
    img_path = ["img/bg.png"]

    return img_path, gr.update(visible=True)


def func(txt, p1, p2, p3, p4, progress=gr.Progress()):
    progress(0, desc="Starting")
    time.sleep(1)
    progress(0.3, desc="Progressing")
    time.sleep(p1)
    progress(1, desc="Completed")
    img = Image.open('img/bg.png')

    time.sleep(2)
    # 返回显示评价区域
    return img, gr.update(visible=True)


def funcgal(txt, p1, p2, p3, p4, progress=gr.Progress()):
    # imggallery = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg", "img/6.jpg", "img/7.jpg", "img/8.jpg", "img/9.jpg", "img/10.jpg", "img/11.jpg", "img/12.jpg", "img/13.jpg", "img/14.jpg", "img/15.jpg", "img/16.jpg", "img/17.jpg", "img/18.jpg", "img/bg.png", "img/sample.png"]
    imggallery = ["img/1.jpg"]

    return imggallery, gr.update(visible=True)


# 主函数,调用demo

def main():
    with gr.Blocks(theme='NoCrypt/miku') as demo:
        feedback_visible = gr.State(False)

        def submit_feedback(feedback):
            # 隐藏评价区域,并返回提示信息
            gr.Info("感谢您的评价")
            return gr.update(visible=False)

        with gr.Column():
            introtext1 = gr.Markdown(  # 在此输入描述,使用 Markdown
                """
                ## “一眼临摹”*手写风格迁移*  
                该项目旨在以手写文本图像为基础,学习其手写字迹风格并生成对应风格的特定文本内容。  
                该项目以论文 [One-Shot Diffusion Mimicker for Handwritten Text Generation](https://arxiv.org/abs/2409.04004) 为基础。该论文详细介绍了如何通过提取单一参考样本的高频信息来改进样式提取,并在此基础上生成对应的手写文本图像。部分代码修改自该论文的 [源代码](https://github.com/dailenson/One-DM)。
                """
            )
            with gr.Group(visible=False) as feedback_group:
                feedback = gr.Textbox(label="请输入您的使用评价")
                submit_btn = gr.Button("提交")
            with gr.Row():
                with gr.Column():
                    textinfo = gr.Textbox(label="此处输入你要生成的文字")
                    with gr.Row():
                        # inputimage = gr.Image(label="此处上传你要生成的字迹风格图像")
                        with gr.Column():
                            para1 = gr.Slider(label="运行 GPU 卡数", minimum = 1, maximum = 4, step = 1)
                            para2 = gr.Slider(label="生成风格数量", minimum = 10, maximum = 150)
                            para3 = gr.Radio(["DDIM", "DDPM"], label = "生成方式", info = "解释")
                            para4 = gr.Slider(label="生成 sample 步数", minimum = 50, maximum = 1000)
                            para5 = gr.Radio(["iv_s", "iv_u", "oov_s", "oov_u"], label="生成类型", info = "解释")
                    with gr.Row():
                        # genebutton = gr.Button("生成用户字体风格")
                        genegallery = gr.Button("生成示例字体风格")
                with gr.Column():
                    # outputimage = gr.Image(label="用户字体风格图片")
                    outputgallery = gr.Gallery(label="示例字体风格图片", columns=5)
            with gr.Row():
                introtext2 = gr.Image(label="演示图片,来自 One-DM", value="img/intro1.jpg")  # 在此修改描述图片路径
                introtext3 = gr.Image(label="演示图片,来自 One-DM", value="img/intro2.png")
            with gr.Row():
                gr.Markdown("""
                <font size=4>
                这里写第一段。
                </font>
                """)
                gr.Markdown("""
                <font size=4>
                这里写第二段。
                </font>
                """)


            submit_btn.click(
                fn=submit_feedback,
                inputs=[feedback],
                outputs=[feedback_group]
            )

            # genebutton.click(
            #     fn=process_task,
            #     inputs=[textinfo, para1, para2, para3, para4],
            #     outputs=[outputimage, feedback_group]
            # )

            genegallery.click(
                fn=process_task,
                inputs=[textinfo, para1, para2, para3, para4, para5],
                outputs=[outputgallery, feedback_group]
            )

        # demo.launch(server_name="172.30.180.28", server_port=45632, debug=True, show_error=True)
        demo.launch()


if __name__ == "__main__":
    main()