From 4649a50452c21e931929bacd69d22dd531a49f02 Mon Sep 17 00:00:00 2001
From: ZeYi Lin <944270057@qq.com>
Date: Fri, 6 Sep 2024 15:13:34 +0800
Subject: [PATCH] feat: support gradio>=4.43.0
---
.gitignore | 1 +
app.py | 81 +++++++++++++++++++++++++++-----------------
assets/title.md | 11 ++++++
requirements-app.txt | 2 +-
4 files changed, 63 insertions(+), 32 deletions(-)
create mode 100644 assets/title.md
diff --git a/.gitignore b/.gitignore
index 36e150d..86c88fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
.vscode/*
.DS_Store
app/output/*.jpg
+demo/kb_output/*.jpg
# build outputs
dist
build
diff --git a/app.py b/app.py
index d35aa04..9ce7f57 100644
--- a/app.py
+++ b/app.py
@@ -11,7 +11,6 @@ import pathlib
import numpy as np
from demo.utils import csv_to_size_list
import argparse
-import onnxruntime
# 获取尺寸列表
root_dir = os.path.dirname(os.path.abspath(__file__))
@@ -31,11 +30,6 @@ color_list_dict_EN = {
}
-# 设置 Gradio examples
-def set_example_image(example: list) -> dict:
- return gr.Image.update(value=example[0])
-
-
# 检测 RGB 是否超出范围,如果超出则约束到 0~255 之间
def range_check(value, min_value=0, max_value=255):
value = int(value)
@@ -225,12 +219,15 @@ def idphoto_inference(
input_width=idphoto_json["size"][1],
)
- result_layout_image = generate_layout_image(
- result_image_standard,
- typography_arr,
- typography_rotate,
- height=idphoto_json["size"][0],
- width=idphoto_json["size"][1],
+ result_layout_image = gr.update(
+ value=generate_layout_image(
+ result_image_standard,
+ typography_arr,
+ typography_rotate,
+ height=idphoto_json["size"][0],
+ width=idphoto_json["size"][1],
+ ),
+ visible=True,
)
# 如果输出 KB 大小选择的是自定义
@@ -286,23 +283,49 @@ if __name__ == "__main__":
image_kb_CN = ["不设置", "自定义"]
image_kb_EN = ["Not Set", "Custom"]
- title = "
HivisionIDPhotos
"
- description = "😎9.2 Update: Add photo size KB adjustment
"
+ # title = "HivisionIDPhotos
"
+ # description = "😎9.2 Update: Add photo size KB adjustment
"
+ # css = """
+ # h1#title, h3 {
+ # text-align: center;
+ # }
+ # """
+
css = """
- h1#title, h3 {
- text-align: center;
- }
- """
+ #col-left {
+ margin: 0 auto;
+ max-width: 430px;
+ }
+ #col-mid {
+ margin: 0 auto;
+ max-width: 430px;
+ }
+ #col-right {
+ margin: 0 auto;
+ max-width: 430px;
+ }
+ #col-showcase {
+ margin: 0 auto;
+ max-width: 1100px;
+ }
+ #button {
+ color: blue;
+ }
+ """
+
+ def load_description(fp):
+ with open(fp, "r", encoding="utf-8") as f:
+ content = f.read()
+ return content
demo = gr.Blocks(css=css)
with demo:
- gr.Markdown(title)
- gr.Markdown(description)
+ gr.HTML(load_description(os.path.join(root_dir, "assets/title.md")))
with gr.Row():
# ------------ 左半边 UI ----------------
with gr.Column():
- img_input = gr.Image().style(height=350)
+ img_input = gr.Image(height=400)
language_options = gr.Dropdown(
choices=language, label="Language", value="中文", elem_id="language"
)
@@ -371,9 +394,9 @@ if __name__ == "__main__":
img_but = gr.Button("开始制作")
# 案例图片
- example_images = gr.Dataset(
- components=[img_input],
- samples=[
+ example_images = gr.Examples(
+ inputs=[img_input],
+ examples=[
[path.as_posix()]
for path in sorted(
pathlib.Path(os.path.join(root_dir, "demo/images")).rglob(
@@ -387,9 +410,9 @@ if __name__ == "__main__":
with gr.Column():
notification = gr.Text(label="状态", visible=False)
with gr.Row():
- img_output_standard = gr.Image(label="标准照").style(height=350)
- img_output_standard_hd = gr.Image(label="高清照").style(height=350)
- img_output_layout = gr.Image(label="六寸排版照").style(height=350)
+ img_output_standard = gr.Image(label="标准照", height=350)
+ img_output_standard_hd = gr.Image(label="高清照", height=350)
+ img_output_layout = gr.Image(label="六寸排版照", height=350)
file_download = gr.File(label="下载调整 KB 大小后的照片", visible=False)
# ---------------- 设置隐藏/显示组件 ----------------
@@ -563,10 +586,6 @@ if __name__ == "__main__":
],
)
- example_images.click(
- fn=set_example_image, inputs=[example_images], outputs=[img_input]
- )
-
argparser = argparse.ArgumentParser()
argparser.add_argument(
"--port", type=int, default=7860, help="The port number of the server"
diff --git a/assets/title.md b/assets/title.md
new file mode 100644
index 0000000..78b5cfd
--- /dev/null
+++ b/assets/title.md
@@ -0,0 +1,11 @@
+
\ No newline at end of file
diff --git a/requirements-app.txt b/requirements-app.txt
index 3974234..31e8e29 100644
--- a/requirements-app.txt
+++ b/requirements-app.txt
@@ -1,2 +1,2 @@
-gradio==3.38.0
+gradio>=4.43.0
fastapi
\ No newline at end of file