Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import time,os
|
4 |
+
from pathlib import Path
|
5 |
+
base_dir = '猫咪表情包'
|
6 |
+
selected_dir = 'selected'
|
7 |
+
files = [str(x) for x in
|
8 |
+
Path(base_dir).rglob('*.jp*g')
|
9 |
+
if 'checkpoint' not in str(x)]
|
10 |
+
files_idx=0
|
11 |
+
selected_count=0
|
12 |
+
|
13 |
+
def show_img(path):
|
14 |
+
return Image.open(path)
|
15 |
+
def fn_before(done,todo):
|
16 |
+
global files_idx
|
17 |
+
if files_idx>0:
|
18 |
+
files_idx-=1
|
19 |
+
path=files[files_idx]
|
20 |
+
img=show_img(path)
|
21 |
+
return done,todo,path,img
|
22 |
+
def fn_next(done,todo):
|
23 |
+
global files_idx
|
24 |
+
if files_idx<len(files)-1:
|
25 |
+
files_idx+=1
|
26 |
+
path=files[files_idx]
|
27 |
+
img=show_img(path)
|
28 |
+
return done,todo,path,img
|
29 |
+
def save_selected(img_path,done,todo):
|
30 |
+
global msg,selected_count
|
31 |
+
if img_path not in msg:
|
32 |
+
msg=msg+' '+img_path
|
33 |
+
selected_count+=1
|
34 |
+
done=selected_count
|
35 |
+
todo=len(files)-selected_count
|
36 |
+
return msg,done,todo
|
37 |
+
def get_default_msg():
|
38 |
+
global msg
|
39 |
+
msg=''
|
40 |
+
return msg
|
41 |
+
|
42 |
+
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
with gr.Row():
|
45 |
+
total = gr.Number(len(files),label='总数量')
|
46 |
+
with gr.Row(scale = 1):
|
47 |
+
bn_before = gr.Button("上一张")
|
48 |
+
bn_next = gr.Button("下一张")
|
49 |
+
with gr.Row(scale = 2):
|
50 |
+
done = gr.Number(value=selected_count,label='已完成')
|
51 |
+
todo = gr.Number(value=len(files)-selected_count,label='待完成')
|
52 |
+
with gr.Row():
|
53 |
+
with gr.Column():
|
54 |
+
path = gr.Text(files[0],lines=1, label='当前图片路径')
|
55 |
+
feedback_button = gr.Button("选择图片",variant="primary")
|
56 |
+
msg = gr.TextArea(value=get_default_msg,lines=3,max_lines = 5)
|
57 |
+
with gr.Column():
|
58 |
+
img = gr.Image(value = show_img(files[0]),type='pil')
|
59 |
+
|
60 |
+
bn_before.click(fn_before,
|
61 |
+
inputs= [done,todo],
|
62 |
+
outputs=[done,todo,path,img])
|
63 |
+
bn_next.click(fn_next,
|
64 |
+
inputs= [done,todo],
|
65 |
+
outputs=[done,todo,path,img])
|
66 |
+
feedback_button.click(save_selected,
|
67 |
+
inputs = [path,done,todo],
|
68 |
+
outputs =[msg,done,todo]
|
69 |
+
)
|
70 |
+
gr.close_all()
|
71 |
+
demo.launch()
|