Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,31 +3,60 @@ from PIL import Image
|
|
| 3 |
import time
|
| 4 |
import threading
|
| 5 |
import tqdm
|
| 6 |
-
#
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# 所有button响应的函数
|
| 10 |
|
| 11 |
# 任务处理函数
|
| 12 |
-
def process_task(txt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# 启动一个 15 秒的命令模拟
|
| 14 |
def execute_command():
|
| 15 |
-
# 这里用time.sleep(
|
| 16 |
# get_wsl.open_wsl_and_run_model() # 单独修改UI的时候把这里换成time.sleep(15)
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
print("命令执行完毕!")
|
| 19 |
|
| 20 |
# 启动执行命令的线程
|
| 21 |
command_thread = threading.Thread(target=execute_command)
|
| 22 |
command_thread.start()
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
return Image.open('img/bg.png'), gr.update(visible=True)
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
progress(0, desc="Starting")
|
| 32 |
time.sleep(1)
|
| 33 |
progress(0.3, desc="Progressing")
|
|
@@ -39,10 +68,14 @@ def func(txt, inimage, p1, p2, p3, p4, progress=gr.Progress()):
|
|
| 39 |
# 返回显示评价区域
|
| 40 |
return img, gr.update(visible=True)
|
| 41 |
|
|
|
|
| 42 |
def funcgal(txt, p1, p2, p3, p4, progress=gr.Progress()):
|
| 43 |
-
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"]
|
|
|
|
|
|
|
| 44 |
return imggallery, gr.update(visible=True)
|
| 45 |
|
|
|
|
| 46 |
# 主函数,调用demo
|
| 47 |
|
| 48 |
def main():
|
|
@@ -69,19 +102,34 @@ def main():
|
|
| 69 |
with gr.Column():
|
| 70 |
textinfo = gr.Textbox(label="此处输入你要生成的文字")
|
| 71 |
with gr.Row():
|
| 72 |
-
inputimage = gr.Image(label="此处上传你要生成的字迹风格图像")
|
| 73 |
with gr.Column():
|
| 74 |
-
para1 = gr.Slider(label="
|
| 75 |
-
para2 = gr.Slider(label="
|
| 76 |
-
para3 = gr.
|
| 77 |
-
para4 = gr.Slider(label="
|
|
|
|
| 78 |
with gr.Row():
|
| 79 |
-
genebutton = gr.Button("生成用户字体风格")
|
| 80 |
genegallery = gr.Button("生成示例字体风格")
|
| 81 |
with gr.Column():
|
| 82 |
-
outputimage = gr.Image(label="用户字体风格图片")
|
| 83 |
-
outputgallery = gr.Gallery(label="示例字体风格图片", columns
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
submit_btn.click(
|
| 87 |
fn=submit_feedback,
|
|
@@ -89,18 +137,19 @@ def main():
|
|
| 89 |
outputs=[feedback_group]
|
| 90 |
)
|
| 91 |
|
| 92 |
-
genebutton.click(
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
| 97 |
|
| 98 |
genegallery.click(
|
| 99 |
-
fn=
|
| 100 |
-
inputs=[textinfo, para1, para2, para3, para4],
|
| 101 |
outputs=[outputgallery, feedback_group]
|
| 102 |
)
|
| 103 |
|
|
|
|
| 104 |
demo.launch()
|
| 105 |
|
| 106 |
|
|
|
|
| 3 |
import time
|
| 4 |
import threading
|
| 5 |
import tqdm
|
| 6 |
+
# -------------------------------model--------------------------
|
| 7 |
+
import subprocess
|
| 8 |
+
import sys
|
| 9 |
|
| 10 |
|
| 11 |
+
def run_model(txt='wqq'):
|
| 12 |
+
print("start running model")
|
| 13 |
+
ckpt_path = '/home/zhutiantian/code/One-DM/One-DM-ckpt.pt'
|
| 14 |
+
dir_path = '/home/zhutiantian/code/One-DM/Generated/English'
|
| 15 |
+
code_path = '/home/zhutiantian/code/One-DM/test.py'
|
| 16 |
+
|
| 17 |
+
# 构造 Conda 命令
|
| 18 |
+
conda_path = "/home/zhutiantian/anaconda3/condabin/conda"
|
| 19 |
+
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}"
|
| 20 |
+
|
| 21 |
+
subprocess.run(command, shell=True)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# -------------------------------gradio-------------------------
|
| 25 |
+
|
| 26 |
# 所有button响应的函数
|
| 27 |
|
| 28 |
# 任务处理函数
|
| 29 |
+
def process_task(txt, p1, p2, p3, p4, p5):
|
| 30 |
+
# 这里需要添加对txt的检查!
|
| 31 |
+
# txt为空、非英文字母时,返回错误,要求重新填写
|
| 32 |
+
# p3、p5 非空
|
| 33 |
+
# p1: 时间,p2:图片数量,p4:步数
|
| 34 |
+
# p3:等于 "DDIM" 或 "DDPM",生成方式
|
| 35 |
+
# p5:等于 "iv_s","iv_u","oov_s","oov_u",生成类型
|
| 36 |
+
|
| 37 |
+
|
| 38 |
# 启动一个 15 秒的命令模拟
|
| 39 |
def execute_command():
|
| 40 |
+
# 这里用time.sleep(50)来模拟命令执行
|
| 41 |
# get_wsl.open_wsl_and_run_model() # 单独修改UI的时候把这里换成time.sleep(15)
|
| 42 |
+
|
| 43 |
+
# run_model(txt)
|
| 44 |
+
time.sleep(5)
|
| 45 |
print("命令执行完毕!")
|
| 46 |
|
| 47 |
# 启动执行命令的线程
|
| 48 |
command_thread = threading.Thread(target=execute_command)
|
| 49 |
command_thread.start()
|
| 50 |
+
time.sleep(5)
|
| 51 |
+
command_thread.join() # 等待命令执行完成
|
| 52 |
|
| 53 |
+
# img_path = f'/home/zhutiantian/code/One-DM/Generated/English/oov_u/168/{txt}.png'
|
| 54 |
+
img_path = ["img/bg.png"]
|
| 55 |
|
| 56 |
+
return img_path, gr.update(visible=True)
|
|
|
|
| 57 |
|
| 58 |
+
|
| 59 |
+
def func(txt, p1, p2, p3, p4, progress=gr.Progress()):
|
| 60 |
progress(0, desc="Starting")
|
| 61 |
time.sleep(1)
|
| 62 |
progress(0.3, desc="Progressing")
|
|
|
|
| 68 |
# 返回显示评价区域
|
| 69 |
return img, gr.update(visible=True)
|
| 70 |
|
| 71 |
+
|
| 72 |
def funcgal(txt, p1, p2, p3, p4, progress=gr.Progress()):
|
| 73 |
+
# 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"]
|
| 74 |
+
imggallery = ["img/1.jpg"]
|
| 75 |
+
|
| 76 |
return imggallery, gr.update(visible=True)
|
| 77 |
|
| 78 |
+
|
| 79 |
# 主函数,调用demo
|
| 80 |
|
| 81 |
def main():
|
|
|
|
| 102 |
with gr.Column():
|
| 103 |
textinfo = gr.Textbox(label="此处输入你要生成的文字")
|
| 104 |
with gr.Row():
|
| 105 |
+
# inputimage = gr.Image(label="此处上传你要生成的字迹风格图像")
|
| 106 |
with gr.Column():
|
| 107 |
+
para1 = gr.Slider(label="运行 GPU 卡数", minimum = 1, maximum = 4, step = 1)
|
| 108 |
+
para2 = gr.Slider(label="生成风格数量", minimum = 10, maximum = 150)
|
| 109 |
+
para3 = gr.Radio(["DDIM", "DDPM"], label = "生成方式", info = "解释")
|
| 110 |
+
para4 = gr.Slider(label="生成 sample 步数", minimum = 50, maximum = 1000)
|
| 111 |
+
para5 = gr.Radio(["iv_s", "iv_u", "oov_s", "oov_u"], label="生成类型", info = "解释")
|
| 112 |
with gr.Row():
|
| 113 |
+
# genebutton = gr.Button("生成用户字体风格")
|
| 114 |
genegallery = gr.Button("生成示例字体风格")
|
| 115 |
with gr.Column():
|
| 116 |
+
# outputimage = gr.Image(label="用户字体风格图片")
|
| 117 |
+
outputgallery = gr.Gallery(label="示例字体风格图片", columns=5)
|
| 118 |
+
with gr.Row():
|
| 119 |
+
introtext2 = gr.Image(label="演示图片,来自 One-DM", value="img/intro1.jpg") # 在此修改描述图片路径
|
| 120 |
+
introtext3 = gr.Image(label="演示图片,来自 One-DM", value="img/intro2.png")
|
| 121 |
+
with gr.Row():
|
| 122 |
+
gr.Markdown("""
|
| 123 |
+
<font size=4>
|
| 124 |
+
这里写第一段。
|
| 125 |
+
</font>
|
| 126 |
+
""")
|
| 127 |
+
gr.Markdown("""
|
| 128 |
+
<font size=4>
|
| 129 |
+
这里写第二段。
|
| 130 |
+
</font>
|
| 131 |
+
""")
|
| 132 |
+
|
| 133 |
|
| 134 |
submit_btn.click(
|
| 135 |
fn=submit_feedback,
|
|
|
|
| 137 |
outputs=[feedback_group]
|
| 138 |
)
|
| 139 |
|
| 140 |
+
# genebutton.click(
|
| 141 |
+
# fn=process_task,
|
| 142 |
+
# inputs=[textinfo, para1, para2, para3, para4],
|
| 143 |
+
# outputs=[outputimage, feedback_group]
|
| 144 |
+
# )
|
| 145 |
|
| 146 |
genegallery.click(
|
| 147 |
+
fn=process_task,
|
| 148 |
+
inputs=[textinfo, para1, para2, para3, para4, para5],
|
| 149 |
outputs=[outputgallery, feedback_group]
|
| 150 |
)
|
| 151 |
|
| 152 |
+
# demo.launch(server_name="172.30.180.28", server_port=45632, debug=True, show_error=True)
|
| 153 |
demo.launch()
|
| 154 |
|
| 155 |
|