chansung commited on
Commit
1d5e796
·
verified ·
1 Parent(s): 4a2e011

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import requests
3
+ import gradio as gr
4
+
5
+ def download_fn(url):
6
+ resp = requests.get(url)
7
+ data = json.loads(resp.content)
8
+
9
+ for idx, data_item in enumerate(data[:]):
10
+ for conv in data_item:
11
+ if len(conv) != 2:
12
+ del data[idx]
13
+ break
14
+
15
+ first_data = []
16
+ if len(data) > 0:
17
+ for first_conv in data[0]:
18
+ piece = []
19
+ for key in first_conv:
20
+ piece.append(first_conv[key])
21
+
22
+ first_data.append(piece)
23
+
24
+ return (
25
+ data,
26
+ first_data,
27
+ 0,
28
+ gr.Button(interactive=True) if len(data) > 0 else gr.Button(interactive=False),
29
+ gr.Button(interactive=True) if len(data) > 0 else gr.Button(interactive=False)
30
+ )
31
+
32
+ def move_next_fn(data, cursor):
33
+ cursor = cursor + 1
34
+ next_data = []
35
+
36
+ for conv in data[cursor]:
37
+ piece = []
38
+ for key in conv:
39
+ piece.append(conv[key])
40
+
41
+ next_data.append(piece)
42
+
43
+ return (
44
+ next_data,
45
+ cursor,
46
+ gr.Button(interactive=True),
47
+ gr.Button(interactive=True) if cursor < len(data)-1 else gr.Button(interactive=False)
48
+ )
49
+
50
+ def move_prev_fn(data, cursor):
51
+ cursor = cursor -1
52
+ prev_data = []
53
+
54
+ for conv in data[cursor]:
55
+ piece = []
56
+ for key in conv:
57
+ piece.append(conv[key])
58
+
59
+ prev_data.append(piece)
60
+
61
+ return (
62
+ prev_data,
63
+ cursor,
64
+ gr.Button(interactive=True) if cursor > 0 else gr.Button(interactive=False),
65
+ gr.Button(interactive=True)
66
+ )
67
+
68
+ with gr.Blocks() as demo:
69
+ data = gr.State([])
70
+ data_to_be_removed = gr.State(set())
71
+ cursor = gr.State(-1)
72
+
73
+ with gr.Row():
74
+ url = gr.Textbox(label="URL", placeholder="URL to download conversation recorded JSON", scale=9)
75
+ download = gr.Button("Download", scale=1)
76
+
77
+ chatbot = gr.Chatbot()
78
+
79
+ with gr.Row():
80
+ prev_btn = gr.Button("Prev", interactive=False)
81
+ mark_btn = gr.Button("✅ Mark to remove ", interactive=False)
82
+ next_btn = gr.Button("Next", interactive=False)
83
+
84
+ download.click(
85
+ download_fn,
86
+ inputs=[url],
87
+ outputs=[data, chatbot, cursor, next_btn, mark_btn]
88
+ )
89
+
90
+ prev_btn.click(
91
+ move_prev_fn,
92
+ inputs=[data, cursor],
93
+ outputs=[chatbot, cursor, prev_btn, next_btn]
94
+ )
95
+
96
+ next_btn.click(
97
+ move_next_fn,
98
+ inputs=[data, cursor],
99
+ outputs=[chatbot, cursor, prev_btn, next_btn]
100
+ )
101
+
102
+ mark_btn.click(
103
+ lambda remove_list, cursor: remove_list.add(cursor),
104
+ inputs=[data_to_be_removed, cursor],
105
+ outputs=[data_to_be_removed]
106
+ )
107
+
108
+
109
+
110
+ demo.launch(debug=True)