File size: 2,911 Bytes
1d5e796
 
 
 
ec15163
 
 
 
 
 
1d5e796
 
 
ec15163
1d5e796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec15163
1d5e796
 
 
 
 
 
 
 
ec15163
1d5e796
 
 
 
 
 
ec15163
 
 
 
1d5e796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec15163
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
import json
import requests
import gradio as gr

STYLE = """
#chatbot {
    height: 700px !important;
}
"""

def download_fn(url):
    resp = requests.get(url)
    data = json.loads(resp.content)
    print(len(data))

    for idx, data_item in enumerate(data[:]):
        for conv in data_item:
            if len(conv) != 2:
                del data[idx]
                break

    first_data = []
    if len(data) > 0:
        for first_conv in data[0]:
            piece = []
            for key in first_conv:
                piece.append(first_conv[key])

            first_data.append(piece)
        
    return (
        data,
        first_data,
        0,
        gr.Button(interactive=True) if len(data) > 0 else gr.Button(interactive=False),
        gr.Button(interactive=True) if len(data) > 0 else gr.Button(interactive=False)
    )

def move_next_fn(data, cursor):
    cursor = cursor + 1
    next_data = []

    for conv in data[cursor]:
        piece = []
        for key in conv:
            piece.append(conv[key])

        next_data.append(piece)

    return (
        next_data,
        cursor,
        gr.Button(interactive=True),
        gr.Button(interactive=True) if cursor < len(data)-1 else gr.Button(interactive=False)
    )

def move_prev_fn(data, cursor):
    cursor = cursor -1
    prev_data = []

    for conv in data[cursor]:
        piece = []
        for key in conv:
            piece.append(conv[key])

        prev_data.append(piece)

    return (
        prev_data,
        cursor,
        gr.Button(interactive=True) if cursor > 0 else gr.Button(interactive=False),
        gr.Button(interactive=True)
    )

with gr.Blocks(css=STYLE) as demo:
    data = gr.State([])
    data_to_be_removed = gr.State(set())
    cursor = gr.State(-1)

    with gr.Row():
        url = gr.Textbox(label="URL", placeholder="URL to download conversation recorded JSON", scale=9)
        download = gr.Button("Download", scale=1)
    
    chatbot = gr.Chatbot(elem_id="chatbot")

    with gr.Row():
        prev_btn = gr.Button("Prev", interactive=False)
        mark_btn = gr.Button("✅ Mark to remove ", interactive=False)
        next_btn = gr.Button("Next", interactive=False)

    with gr.Row():
        cleanup_btn = gr.Button("Clean-up")
        get_btn = gr.Button("Download clean-up version")

    download.click(
        download_fn,
        inputs=[url], 
        outputs=[data, chatbot, cursor, next_btn, mark_btn]
    )

    prev_btn.click(
        move_prev_fn,
        inputs=[data, cursor],
        outputs=[chatbot, cursor, prev_btn, next_btn]
    )

    next_btn.click(
        move_next_fn,
        inputs=[data, cursor],
        outputs=[chatbot, cursor, prev_btn, next_btn]
    )

    mark_btn.click(
        lambda remove_list, cursor: remove_list.add(cursor),
        inputs=[data_to_be_removed, cursor],
        outputs=[data_to_be_removed]
    )

    

demo.launch()