SWHL commited on
Commit
60c966b
·
verified ·
1 Parent(s): f977a26

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- encoding: utf-8 -*-
2
+ # @Author: SWHL
3
+ # @Contact: [email protected]
4
+ import gradio as gr
5
+
6
+ from rapid_layout import ModelType, RapidLayout, RapidLayoutInput
7
+
8
+
9
+ def get_layout_res(img_input, model_type, conf_thresh, iou_thresh):
10
+ cfg = RapidLayoutInput(
11
+ model_type=ModelType(model_type), conf_thresh=conf_thresh, iou_thresh=iou_thresh
12
+ )
13
+ engine = RapidLayout(cfg=cfg)
14
+ result = engine(img_input)
15
+ vised_img = result.vis("layout_vis.jpg")
16
+ return vised_img, f"{result.elapse:.4f}"
17
+
18
+
19
+ custom_css = """
20
+ body {font-family: body {font-family: 'Helvetica Neue', Helvetica;}
21
+ .gr-button {background-color: #4CAF50; color: white; border: none; padding: 10px 20px; border-radius: 5px;}
22
+ .gr-button:hover {background-color: #45a049;}
23
+ .gr-textbox {margin-bottom: 15px;}
24
+ .example-button {background-color: #1E90FF; color: white; border: none; padding: 8px 15px; border-radius: 5px; margin: 5px;}
25
+ .example-button:hover {background-color: #FF4500;}
26
+ .tall-radio .gr-radio-item {padding: 15px 0; min-height: 50px; display: flex; align-items: center;}
27
+ .tall-radio label {font-size: 16px;}
28
+ .output-image, .input-image, .image-preview {height: 300px !important}
29
+ """
30
+
31
+ with gr.Blocks(title="Rapid Layout", css="custom_css", theme=gr.themes.Soft()) as demo:
32
+ gr.HTML(
33
+ """
34
+ <h1 style='text-align: center;font-size:40px'>Rapid Layout v1</h1>
35
+
36
+ <div style="display: flex; justify-content: center; gap: 10px;">
37
+ <a><img src="https://img.shields.io/badge/DeployVersion-v1.0.0-brightgree"></a>
38
+ <a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.13-aff.svg"></a>
39
+ <a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
40
+ <a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout"></a>
41
+ <a href="https://pepy.tech/project/rapid-layout"><img src="https://static.pepy.tech/personalized-badge/rapid-layout?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
42
+ <a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
43
+ <a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
44
+ </div>
45
+ """
46
+ )
47
+ with gr.Row():
48
+ model_type = gr.Dropdown(
49
+ choices=[v.value for v in ModelType],
50
+ label="版面分析模型",
51
+ value=ModelType.PP_LAYOUT_CDLA.value,
52
+ interactive=True,
53
+ )
54
+ conf_thresh = gr.Slider(
55
+ label="conf_thresh",
56
+ minimum=0,
57
+ maximum=1.0,
58
+ value=0.5,
59
+ step=0.1,
60
+ info="置信度",
61
+ interactive=True,
62
+ )
63
+ iou_thresh = gr.Slider(
64
+ label="IoU Threshold",
65
+ minimum=0,
66
+ maximum=1.0,
67
+ value=0.5,
68
+ step=0.1,
69
+ info="IoU阈值设定",
70
+ interactive=True,
71
+ )
72
+
73
+ with gr.Row():
74
+ run_btn = gr.Button("Run")
75
+
76
+ with gr.Row():
77
+ img_input = gr.Image(label="Upload or Select Image", sources="upload")
78
+ img_output = gr.Image(label="Output Image", show_download_button=True)
79
+ elapse = gr.Textbox(label="Elapse(s)")
80
+
81
+ run_btn.click(
82
+ get_layout_res,
83
+ inputs=[img_input, model_type, conf_thresh, iou_thresh],
84
+ outputs=[img_output, elapse],
85
+ )
86
+
87
+ examples = gr.Examples(
88
+ examples=[
89
+ ["tests/test_files/layout.jpg", ModelType.PP_LAYOUT_CDLA.value, 0.5, 0.5],
90
+ ],
91
+ examples_per_page=5,
92
+ inputs=[img_input, model_type, conf_thresh, iou_thresh],
93
+ fn=get_layout_res,
94
+ outputs=[img_output, elapse],
95
+ cache_examples=False,
96
+ )
97
+
98
+ if __name__ == "__main__":
99
+ demo.launch(debug=True)