File size: 4,416 Bytes
fd3a517
53a6bc7
fd3a517
 
 
53a6bc7
 
fd3a517
 
 
 
 
 
 
 
 
5fcc3eb
 
fd3a517
5fcc3eb
 
fd3a517
 
 
 
 
 
 
 
 
 
5fcc3eb
 
fd3a517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from gradio_client import Client, handle_file
from pathlib import Path
from gradio.utils import get_cache_folder


class Examples(gr.helpers.Examples):
    def __init__(self, *args, cached_folder=None, **kwargs):
        super().__init__(*args, **kwargs, _initiated_directly=False)
        if cached_folder is not None:
            self.cached_folder = cached_folder
            # self.cached_file = Path(self.cached_folder) / "log.csv"
        self.create()


HF_TOKEN = os.environ.get('HF_KEY')

client = Client("Canyu/Diception",
                max_workers=3,
                hf_token=HF_TOKEN)


def process_image_check(path_input):
    if path_input is None:
        raise gr.Error(
            "Missing image in the left pane: please upload an image first."
        )

def infer_image_matting(matting_image_input):
    return client.predict(
      prompt=handle_file(matting_image_input),
      api_name="/test"
    )

def clear_cache():
    return None, None

def run_demo_server():
    gradio_theme = gr.themes.Default()
    with gr.Blocks(
        theme=gradio_theme,
        title="Matting",
    ) as demo:
        with gr.Row():
            gr.Markdown("# Matting Demo")
        with gr.Row():
            gr.Markdown("### Due to the GPU quota limit, if an error occurs, please wait for 5 minutes before retrying.")
        with gr.Row():
            with gr.Column():
                matting_image_input = gr.Image(
                    label="Input Image",
                    type="filepath",
                )
                with gr.Row():
                    matting_image_submit_btn = gr.Button(
                        value="Estimate Matting", variant="primary"
                    )
                    matting_image_reset_btn = gr.Button(value="Reset")
                
                with gr.Row():
                    img_clear_button = gr.Button("Clear Cache")
                
            with gr.Column():
                # matting_image_output = gr.Image(label='Output')
                matting_image_output =  gr.Image(label='Matting Output')
                    
                        #     label="Matting Output",
                        #     type="filepath",
                        #     show_download_button=True,
                        #     show_share_button=True,
                        #     interactive=False,
                        #     elem_classes="slider",
                        #     position=0.25,
                        # )
                
            

        img_clear_button.click(clear_cache, outputs=[matting_image_input, matting_image_output])

        matting_image_submit_btn.click(
            fn=process_image_check,
            inputs=matting_image_input,
            outputs=None,
            preprocess=False,
            queue=False,
        ).success(
            # fn=process_pipe_matting,
            fn=infer_image_matting,
            inputs=[
                matting_image_input,
            ],
            outputs=[matting_image_output],
            concurrency_limit=1,
        )

        matting_image_reset_btn.click(
            fn=lambda: (
                None,
                None,
            ),
            inputs=[],
            outputs=[
                matting_image_input,
                matting_image_output,
            ],
            queue=False,
        )

        gr.Examples(
            fn=infer_image_matting,
            examples=[
                "assets/person.jpg",              
            ],
            inputs=[matting_image_input],
            outputs=[matting_image_output],
            cache_examples=True,
            # cache_examples=False,
            # cached_folder="cache_dir",
        )
        
    demo.queue(
        api_open=False,
    ).launch()


if __name__ == '__main__':

    # 在公共 Space 中,通过 gradio.client 调用私有 Space
    def call_private_space(input_text):
        # 加载私有 Space(假设是私有 Space 的名称)
        app = gr.Interface.load("Canyu/Diception", use_auth_token=True)

        # 调用私有 Space 中的函数
        output = app(input_text)
        
        return output

    # 创建一个公共 Space 界面,允许用户输入数据并调用私有 Space 中的函数
    interface = gr.Interface(fn=call_private_space, inputs="text", outputs="text")

    # 启动公共 Space
    interface.launch()