Spaces:
Running
Running
commit
Browse files- app.py +138 -4
- assets/person.jpg +0 -0
app.py
CHANGED
@@ -1,7 +1,141 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from gradio_client import Client, handle_file
|
4 |
+
from gradio_imageslider import ImageSlider
|
5 |
+
from pathlib import Path
|
6 |
+
from gradio.utils import get_cache_folder
|
7 |
|
|
|
|
|
8 |
|
9 |
+
class Examples(gr.helpers.Examples):
|
10 |
+
def __init__(self, *args, cached_folder=None, **kwargs):
|
11 |
+
super().__init__(*args, **kwargs, _initiated_directly=False)
|
12 |
+
if cached_folder is not None:
|
13 |
+
self.cached_folder = cached_folder
|
14 |
+
# self.cached_file = Path(self.cached_folder) / "log.csv"
|
15 |
+
self.create()
|
16 |
+
|
17 |
+
|
18 |
+
client = Client("Canyu/Diception",
|
19 |
+
max_workers=3)
|
20 |
+
|
21 |
+
|
22 |
+
def process_image_check(path_input):
|
23 |
+
if path_input is None:
|
24 |
+
raise gr.Error(
|
25 |
+
"Missing image in the left pane: please upload an image first."
|
26 |
+
)
|
27 |
+
|
28 |
+
def infer_image_matting(matting_image_input):
|
29 |
+
return client.predict(
|
30 |
+
file=handle_file(matting_image_input),
|
31 |
+
api_name="/infer_image_matting"
|
32 |
+
)
|
33 |
+
|
34 |
+
def clear_cache():
|
35 |
+
return None, None
|
36 |
+
|
37 |
+
def run_demo_server():
|
38 |
+
gradio_theme = gr.themes.Default()
|
39 |
+
with gr.Blocks(
|
40 |
+
theme=gradio_theme,
|
41 |
+
title="Matting",
|
42 |
+
) as demo:
|
43 |
+
with gr.Row():
|
44 |
+
gr.Markdown("# Matting Demo")
|
45 |
+
with gr.Row():
|
46 |
+
gr.Markdown("### Due to the GPU quota limit, if an error occurs, please wait for 5 minutes before retrying.")
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
matting_image_input = gr.Image(
|
50 |
+
label="Input Image",
|
51 |
+
type="filepath",
|
52 |
+
)
|
53 |
+
with gr.Row():
|
54 |
+
matting_image_submit_btn = gr.Button(
|
55 |
+
value="Estimate Matting", variant="primary"
|
56 |
+
)
|
57 |
+
matting_image_reset_btn = gr.Button(value="Reset")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
img_clear_button = gr.Button("Clear Cache")
|
61 |
+
|
62 |
+
with gr.Column():
|
63 |
+
# matting_image_output = gr.Image(label='Output')
|
64 |
+
matting_image_output = gr.Image(label='Matting Output')
|
65 |
+
|
66 |
+
# label="Matting Output",
|
67 |
+
# type="filepath",
|
68 |
+
# show_download_button=True,
|
69 |
+
# show_share_button=True,
|
70 |
+
# interactive=False,
|
71 |
+
# elem_classes="slider",
|
72 |
+
# position=0.25,
|
73 |
+
# )
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
img_clear_button.click(clear_cache, outputs=[matting_image_input, matting_image_output])
|
78 |
+
|
79 |
+
matting_image_submit_btn.click(
|
80 |
+
fn=process_image_check,
|
81 |
+
inputs=matting_image_input,
|
82 |
+
outputs=None,
|
83 |
+
preprocess=False,
|
84 |
+
queue=False,
|
85 |
+
).success(
|
86 |
+
# fn=process_pipe_matting,
|
87 |
+
fn=infer_image_matting,
|
88 |
+
inputs=[
|
89 |
+
matting_image_input,
|
90 |
+
],
|
91 |
+
outputs=[matting_image_output],
|
92 |
+
concurrency_limit=1,
|
93 |
+
)
|
94 |
+
|
95 |
+
matting_image_reset_btn.click(
|
96 |
+
fn=lambda: (
|
97 |
+
None,
|
98 |
+
None,
|
99 |
+
),
|
100 |
+
inputs=[],
|
101 |
+
outputs=[
|
102 |
+
matting_image_input,
|
103 |
+
matting_image_output,
|
104 |
+
],
|
105 |
+
queue=False,
|
106 |
+
)
|
107 |
+
|
108 |
+
gr.Examples(
|
109 |
+
fn=infer_image_matting,
|
110 |
+
examples=[
|
111 |
+
"assets/person.jpg",
|
112 |
+
],
|
113 |
+
inputs=[matting_image_input],
|
114 |
+
outputs=[matting_image_output],
|
115 |
+
cache_examples=True,
|
116 |
+
# cache_examples=False,
|
117 |
+
# cached_folder="cache_dir",
|
118 |
+
)
|
119 |
+
|
120 |
+
demo.queue(
|
121 |
+
api_open=False,
|
122 |
+
).launch()
|
123 |
+
|
124 |
+
|
125 |
+
if __name__ == '__main__':
|
126 |
+
|
127 |
+
# 在公共 Space 中,通过 gradio.client 调用私有 Space
|
128 |
+
def call_private_space(input_text):
|
129 |
+
# 加载私有 Space(假设是私有 Space 的名称)
|
130 |
+
app = gr.Interface.load("Canyu/Diception", use_auth_token=True)
|
131 |
+
|
132 |
+
# 调用私有 Space 中的函数
|
133 |
+
output = app(input_text)
|
134 |
+
|
135 |
+
return output
|
136 |
+
|
137 |
+
# 创建一个公共 Space 界面,允许用户输入数据并调用私有 Space 中的函数
|
138 |
+
interface = gr.Interface(fn=call_private_space, inputs="text", outputs="text")
|
139 |
+
|
140 |
+
# 启动公共 Space
|
141 |
+
interface.launch()
|
assets/person.jpg
ADDED
![]() |