geyongtao commited on
Commit
19fbfe2
·
verified ·
1 Parent(s): e43fdb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py CHANGED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from gradio_client import Client, handle_file
4
+
5
+ HF_TOKEN = os.environ.get('HF_KEY')
6
+
7
+ client = Client("geyongtao/video-matting",
8
+ max_workers=3,
9
+ hf_token=HF_TOKEN)
10
+
11
+ def process_image_check(path_input):
12
+ if path_input is None:
13
+ raise gr.Error(
14
+ "Missing image in the left pane: please upload an image first."
15
+ )
16
+
17
+ def process_pipe_matting(matting_image_input):
18
+ return client.predict(
19
+ path_input=handle_file(matting_image_input),
20
+ api_name="/gradio_handler"
21
+ )
22
+
23
+ def run_demo_server():
24
+ gradio_theme = gr.themes.Default()
25
+ with gr.Blocks(
26
+ theme=gradio_theme,
27
+ title="Matting",
28
+ ) as demo:
29
+ with gr.Row():
30
+ gr.Markdown("# Matting Demo")
31
+ with gr.Row():
32
+ gr.Markdown("### Due to the GPU quota limit, if an error occurs, please wait for 5 minutes before retrying.")
33
+ with gr.Row():
34
+ with gr.Column():
35
+ matting_image_input = gr.Image(
36
+ label="Input Image",
37
+ type="filepath",
38
+ )
39
+ with gr.Row():
40
+ matting_image_submit_btn = gr.Button(
41
+ value="Estimate Matting", variant="primary"
42
+ )
43
+ matting_image_reset_btn = gr.Button(value="Reset")
44
+ with gr.Column():
45
+ matting_image_output = gr.Image(label='Output')
46
+
47
+
48
+ matting_image_submit_btn.click(
49
+ fn=process_image_check,
50
+ inputs=matting_image_input,
51
+ outputs=None,
52
+ preprocess=False,
53
+ queue=False,
54
+ ).success(
55
+ fn=process_pipe_matting,
56
+ inputs=[
57
+ matting_image_input,
58
+ ],
59
+ outputs=[matting_image_output],
60
+ concurrency_limit=1,
61
+ )
62
+
63
+ matting_image_reset_btn.click(
64
+ fn=lambda: (
65
+ None,
66
+ None,
67
+ ),
68
+ inputs=[],
69
+ outputs=[
70
+ matting_image_input,
71
+ matting_image_output,
72
+ ],
73
+ queue=False,
74
+ )
75
+
76
+ demo.queue(
77
+ api_open=False,
78
+ ).launch()
79
+
80
+
81
+ if __name__ == '__main__':
82
+ run_demo_server()