File size: 6,723 Bytes
a779273
 
 
 
ced76fc
a779273
ced76fc
a779273
 
 
ad445e5
ced76fc
ad445e5
ced76fc
421e27d
ad445e5
 
421e27d
 
 
 
 
 
a779273
 
ced76fc
 
a779273
ced76fc
a779273
ced76fc
 
 
 
 
 
 
 
a779273
 
 
ced76fc
a779273
ced76fc
 
 
 
a779273
 
 
 
ced76fc
 
 
 
a779273
ced76fc
 
 
 
 
a779273
 
ced76fc
 
 
 
a779273
ced76fc
 
 
 
a779273
 
ced76fc
 
 
 
a779273
ced76fc
 
 
 
a779273
 
ced76fc
 
 
 
a779273
ced76fc
 
 
 
a779273
 
ced76fc
 
 
 
a779273
ced76fc
 
 
 
a779273
 
 
ced76fc
 
 
 
 
 
 
 
 
a779273
ced76fc
 
 
 
 
 
 
 
 
 
 
a779273
ced76fc
 
 
a779273
ced76fc
 
 
 
a779273
ced76fc
 
 
 
a779273
 
 
 
 
 
 
 
 
 
 
ced76fc
 
 
a779273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced76fc
a779273
 
 
ced76fc
 
 
 
 
a779273
 
 
ced76fc
 
 
a779273
 
 
 
 
ced76fc
a779273
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import gradio as gr 
import pandas as pd
import matplotlib.pyplot as plt

from modules.module_connection import WordExplorerConnector
from modules.module_logsManager import HuggingFaceDatasetSaver
from tool_info import TOOL_INFO

plt.rcParams.update({'font.size': 14})

def interface(
    embedding, # Class Embedding instance
    available_logs: bool, 
    max_neighbors: int,
    lang: str="es",
) -> gr.Blocks:

    # -- Load examples ---
    if lang == 'es':
        from examples.examples_es import examples_explorar_relaciones_entre_palabras
    elif lang == 'en':
        from examples.examples_en import examples_explorar_relaciones_entre_palabras

    # --- Init logs ---
    log_callback = HuggingFaceDatasetSaver(
        available_logs=available_logs,
        dataset_name=f"logs_edia_we_{lang}"
    )

    # --- Init vars ---
    connector = WordExplorerConnector(
        embedding=embedding
    )

    # --- Load language ---
    labels = pd.read_json(
        f"language/{lang}.json"
    )["WordExplorer_interface"]

    # --- Interface ---
    interface = gr.Blocks()

    with interface:
        gr.Markdown(
            value=labels["title"]
        )
        
        with gr.Row():
            with gr.Column(scale=3):
                with gr.Row(equal_height=True):
                    with gr.Column(scale=5):
                        diagnose_list = gr.Textbox(
                            lines=2, 
                            label=labels["wordListToDiagnose"]
                        )
                    with gr.Column(scale=1,min_width=10):
                        color_wordlist = gr.ColorPicker(
                            label="",
                            value='#000000'
                        )

                with gr.Row():
                    with gr.Column(scale=5): 
                        wordlist_1 = gr.Textbox(
                            lines=2, 
                            label=labels["wordList1"]
                        )
                    with gr.Column(scale=1,min_width=10): 
                        color_wordlist_1 = gr.ColorPicker(
                            label="",
                            value='#1f78b4'
                        )
                with gr.Row():
                    with gr.Column(scale=5): 
                        wordlist_2 = gr.Textbox(
                            lines=2, 
                            label=labels["wordList2"]
                        )
                    with gr.Column(scale=1,min_width=10):     
                        color_wordlist_2 = gr.ColorPicker(
                            label="",
                            value='#33a02c'
                        )
                with gr.Row():
                    with gr.Column(scale=5):    
                        wordlist_3 = gr.Textbox(
                            lines=2, 
                            label=labels["wordList3"]
                        )
                    with gr.Column(scale=1,min_width=10): 
                        color_wordlist_3 = gr.ColorPicker(
                            label="",
                            value='#e31a1c'
                        )
                with gr.Row():
                    with gr.Column(scale=5):    
                        wordlist_4 = gr.Textbox(
                            lines=2, 
                            label=labels["wordList4"]
                        )
                    with gr.Column(scale=1,min_width=10): 
                        color_wordlist_4 = gr.ColorPicker(
                            label="",
                            value='#6a3d9a'
                        )
            with gr.Column(scale=4):
                with gr.Row():
                    with gr.Row():
                        gr.Markdown(
                            value=labels["plotNeighbours"]["title"]
                        )
                        n_neighbors = gr.Slider(
                            minimum=0,
                            maximum=max_neighbors,
                            step=1,
                            label=labels["plotNeighbours"]["quantity"]
                        )
                    with gr.Row():
                        alpha = gr.Slider(
                            minimum=0.1,
                            maximum=0.9, 
                            value=0.3, 
                            step=0.1,
                            label=labels["options"]["transparency"]
                        )
                        fontsize=gr.Number(
                            value=25, 
                            label=labels["options"]["font-size"]
                        )
                    with gr.Row():
                        btn_plot = gr.Button(
                            value=labels["plot_button"]
                        )
                with gr.Row(): 
                    err_msg = gr.Markdown(
                        label="", 
                        visible=True
                    )
                with gr.Row():
                    word_proyections = gr.Plot(
                        label="", 
                        show_label=False
                    )

        with gr.Row():
            gr.Examples(
                fn=connector.plot_proyection_2d,
                inputs=[diagnose_list,wordlist_1,wordlist_2,wordlist_3,wordlist_4],
                outputs=[word_proyections,err_msg],
                examples=examples_explorar_relaciones_entre_palabras,
                label=labels["examples"]
            )

        with gr.Row():
            gr.Markdown(
                value=TOOL_INFO
            )

        btn_plot.click(
            fn=connector.plot_proyection_2d,
            inputs=[
                diagnose_list,
                wordlist_1,
                wordlist_2,
                wordlist_3,
                wordlist_4,
                color_wordlist,
                color_wordlist_1,
                color_wordlist_2,
                color_wordlist_3,
                color_wordlist_4,
                alpha,
                fontsize,
                n_neighbors
            ],
            outputs=[word_proyections, err_msg]
        )

        # --- Logs ---
        save_field = [diagnose_list, wordlist_1, wordlist_2, wordlist_3, wordlist_4]
        log_callback.setup(
            components=save_field, 
            flagging_dir="logs_word_explorer"
        )
        
        btn_plot.click(
            fn=lambda *args: log_callback.flag(
                flag_data=args,
                flag_option="word_explorer",
                username="vialibre",
            ),
            inputs=save_field,
            outputs=None, 
            preprocess=False
        )
        
        return interface