File size: 12,632 Bytes
18cbe65
 
 
29beee6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18cbe65
 
 
29beee6
 
 
 
 
18cbe65
41bef2b
29beee6
18cbe65
 
c17a86a
 
29beee6
 
bbdaf4d
41bef2b
 
 
 
 
 
 
 
 
18cbe65
 
 
 
 
 
 
29beee6
 
 
 
18cbe65
 
 
 
 
29beee6
18cbe65
 
bbdaf4d
 
 
 
 
 
 
29beee6
bbdaf4d
29beee6
bbdaf4d
29beee6
bbdaf4d
 
 
 
 
 
29beee6
18cbe65
 
29beee6
 
 
 
 
 
 
18cbe65
 
29beee6
18cbe65
 
 
29beee6
bbdaf4d
ffc83ec
18cbe65
 
 
 
 
 
 
 
 
 
 
 
29beee6
 
18cbe65
 
 
29beee6
18cbe65
bbdaf4d
18cbe65
 
 
 
 
 
 
 
 
 
 
 
 
 
29beee6
18cbe65
 
 
 
 
 
 
 
bbdaf4d
18cbe65
 
 
 
 
41bef2b
18cbe65
 
 
 
41bef2b
18cbe65
 
 
 
41bef2b
18cbe65
 
29beee6
 
41bef2b
18cbe65
 
 
 
29beee6
18cbe65
 
29beee6
 
 
 
 
18cbe65
 
 
 
29beee6
 
 
41bef2b
 
 
 
29beee6
 
 
 
 
 
 
 
 
 
 
 
41bef2b
 
 
 
18cbe65
29beee6
18cbe65
 
 
 
 
 
 
 
 
 
 
 
41bef2b
18cbe65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29beee6
18cbe65
 
 
 
 
 
 
 
 
29beee6
18cbe65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41bef2b
 
18cbe65
 
 
29beee6
 
 
41bef2b
 
18cbe65
 
 
 
29beee6
 
 
 
18cbe65
 
 
 
 
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from esgen.functions import *


js = """

function createGradioAnimation() {

    var container = document.createElement('div');

    container.id = 'gradio-animation';

    container.style.fontSize = '2em';

    container.style.fontWeight = 'bold';

    container.style.textAlign = 'center';

    container.style.marginBottom = '20px';



    var text = 'EntitySchema Generator';

    for (var i = 0; i < text.length; i++) {

        (function(i){

            setTimeout(function(){

                var letter = document.createElement('span');

                letter.style.opacity = '0';

                letter.style.transition = 'opacity 0.5s';

                letter.innerText = text[i];



                container.appendChild(letter);



                setTimeout(function() {

                    letter.style.opacity = '1';

                }, 50);

            }, i * 250);

        })(i);

    }



    var gradioContainer = document.querySelector('.gradio-container');

    gradioContainer.insertBefore(container, gradioContainer.firstChild);



    return 'Animation created';

}

"""


with gr.Blocks() as introduction_tab:
    gr.Markdown(
        """

        This is a [Wikidata EntitySchema](https://www.wikidata.org/wiki/Wikidata:Schemas) Generator that consists of 

        two components:

        - The **Generate** tab is used to generate an EntitySchema from scratch by analyzing existing items and 

        refining the results with the help of a chatbot.

        - The **Modify** tab is used to add, change, or remove constraints from an  existing EntitySchema.

        

        ## Resources

        - [Wikidata Schemas](https://www.wikidata.org/wiki/Wikidata:Schemas)

        - [Shape Expressions (ShEx) 2.1 Primer](https://shex.io/shex-primer/)

        - [EntitySchema directory](https://www.wikidata.org/wiki/Wikidata:Database_reports/EntitySchema_directory)

        - [WikiShape](https://wikishape.weso.es)

        - [RDFShape](https://rdfshape.weso.es)

        - [ShExStatements](https://shexstatements.toolforge.org)

        - A similar toolkit can be found [here on Toolforge](https://tools-static.wmflabs.org/entityschema-generator/).

        - [ShEx2 Simple Online Validator](https://shex-simple.toolforge.org/wikidata/packages/shex-webapp/doc/shex-simple.html?data=Endpoint:%20https://query.wikidata.org/sparql&hideData&manifest=[]&textMapIsSparqlQuery)

        

        ## Report an issue

        Hugging Face provides a [Community](https://huggingface.co/spaces/b289zhan/ESGen/discussions) panel for discussions.

        If you encounter any bugs or issues with this tool, please feel free to start a new discussion there. 

        You can also post any new feature requests.

        

        ## Acknowledgements

        This tool is a collaborative effort by King's College London, London, and Wikimedia Deutschland, Berlin. 

        The project is funded by the NMES Enterprise & Engagement Partnerships Fund from King's College London.

        """
    )


with gr.Blocks() as generate_tab:
    gr.Markdown(
        """

        On this page, you can generate an EntitySchema from scratch. The tool will analyze existing items to draft an 

        EntitySchema for you. Afterwards, you can refine it by answering a few questions from a chatbot.

        

        ## Step 1: Generate an EntitySchema from scratch

        """
    )
    with gr.Row():
        with gr.Column():
            es_name = gr.Textbox(
                label="Name of the new EntitySchema",
                placeholder="e.g., algorithm"
            )
            es_class = gr.Textbox(
                label="Item ID for class",
                placeholder="e.g., Q8366",
                info="Specify which class the new EntitySchema will cover. The tool will look at all items that "
                     "are an 'instance of' (P31) the class."
            )
            class_choices = gr.Radio(visible=False)  # TODO: remove the label
            threshold_slider = gr.Slider(
                minimum=10, maximum=100, value=50, step=10, label="Cutoff",
                info="Ignore all properties that are used on less than X% of items in the class for the initial "
                     "EntitySchema generation."
            )
            property_type_checkboxes = gr.CheckboxGroup(
                choices=[("WikibaseItem", "wikibase:WikibaseItem"), ("ExternalId", "wikibase:ExternalId")],
                value=["wikibase:WikibaseItem"],
                label="Property types",
                info="Select the Wikidata datatypes of properties you want to encompass. Default: 'WikibaseItem'."
            )
            generate_btn = gr.Button(value="Generate EntitySchema")
            gr.Markdown(
                """

                ## Step 2: Refine the generated EntitySchema

                The chatbot will ask you a series of questions to refine the EntitySchema. Click the following buttons 

                to answer the questions.

                - If you think the property is required for all items in the class, click "Yes". 

                - If you think the property is only used on some items in the class, click "Optional". 

                - If you think the property should not be used on any items in the class, click "No". 

                - If you can't decide, feel free to click "Skip".

                """
            )
            # TODO: what does the rejection ("No") semantically mean

            refine_chatbot = gr.Chatbot(
                label="Chatbot",
                value=[[None, None]],
                height=420,
                render_markdown=True,
            )
            # chatbot_input = gr.Textbox(placeholder="Type your message here :)")
            with gr.Row():
                yes_btn = gr.Button(
                    value="✅ Yes"
                )
                optional_btn = gr.Button(
                    value="🉑 Optional"
                )
                no_btn = gr.Button(
                    value="⛔️ No"
                )
                skip_btn = gr.Button(
                    value="⏩ Skip"
                )

        with gr.Column():
            # TODO: meaningful error messages
            es_output = gr.Code(
                lines=70,
                label="EntitySchema",
                interactive=True,
            )
            # es_download = gr.DownloadButton()
            es_json = gr.JSON(
                label="JSON",
                visible=False,
            )

    es_class.input(
        fn=update_class_choices,
        inputs=es_class,
        outputs=class_choices
    )
    # TODO: more distinction between labels and descriptions
    class_choices.select(
        fn=select_class,
        inputs=[class_choices],
        outputs=[es_class, class_choices]
    )

    generate_btn.click(
        fn=entity_schema_generation,
        inputs=[es_name, es_class, threshold_slider, property_type_checkboxes],
        outputs=[es_output, es_json, refine_chatbot]
    )

    yes_btn.click(
        fn=input_yes,
        inputs=[es_output, es_json, refine_chatbot],
        outputs=[es_output, es_json, refine_chatbot]
    )
    optional_btn.click(
        fn=input_optional,
        inputs=[es_output, es_json, refine_chatbot],
        outputs=[es_output, es_json, refine_chatbot]
    )
    no_btn.click(
        fn=input_no,
        inputs=[es_output, es_json, refine_chatbot],
        outputs=[es_output, es_json, refine_chatbot]
    )
    skip_btn.click(
        fn=input_skip,
        inputs=[es_output, es_json, refine_chatbot],
        outputs=[es_output, es_json, refine_chatbot]
    )


with gr.Blocks() as modify_single_tab:
    gr.Markdown(
        """

        On this page, you can modify an existing EntitySchema. You can load an example or paste your own EntitySchema 

        into the EntitySchema box on the right. Afterwards, you can specify the changes you would like to make to your 

        EntitySchema.

        

        ## Step 1: Load an EntitySchema

        """
    )
    with gr.Row():
        with gr.Column():
            # examples
            example_dropdown = gr.Dropdown(
                choices=["algorithm"],
                label="EntitySchema Examples",
                info="Either select an example from the dropdown list or "
                     "paste your own EntitySchema into the EntitySchema box on the right and "
                     "click the 'load example' button."
            )
            example_btn = gr.Button("Load example")

            gr.Markdown(
                """

                ## Step 2: Modify the EntitySchema

                

                You can add a new constraint to the EntitySchema by specifying it below. If the EntitySchema already 

                contains a constraint for the property, it will be overwritten by the new constraint. If you would like 

                to delete constraints, simply delete them in the EntitySchema box.

                """
            )
            shape_name = gr.Dropdown(
                choices=[],
                label="Shape Name"
            )
            property_name = gr.Textbox(
                label="Property ID"
            )
            property_choices = gr.Radio(
                visible=False
            )
            with gr.Column():
                with gr.Group():
                    allowed_values = gr.Dropdown(
                        choices=[
                            "with any value",
                            "with one of a set of specific entities",
                            "with an instance of",
                            "with a subclass of",
                            "datatypes"
                        ],
                        label="Allowed Values"
                    )
                    class_names = gr.Textbox(label="Class Name", interactive=True, visible=False)
                    class_choices = gr.Radio(visible=False)
                    datatypes = gr.Dropdown(visible=False)
                    # add_class_btn = gr.Button(value="Add", visible=False)
            cardinality = gr.Dropdown(
                choices=[
                    "has 1 matching statement",
                    "has 0 or 1 matching statements",
                    "has 0 or more matching statements",
                    "has 1 or more matching statements",
                    "has no matching statements"
                ],
                label="Cardinality",
                interactive=True
            )
            with gr.Column():
                comment = gr.Textbox(
                    label="Comment"
                )
            insert_btn = gr.Button(value="Insert")

        with gr.Column():
            es_output = gr.Code(
                lines=46,
                label="EntitySchema",
                interactive=True,
            )
            # es_download = gr.DownloadButton()
            # es_data = gr.JSON(
            #     label="JSON",
            #     visible=False,
            # )

    # single triple constraint
    property_name.input(
        fn=update_property_choices,
        inputs=property_name,
        outputs=property_choices
    )
    property_choices.select(
        fn=select_property,
        inputs=property_choices,
        outputs=[property_name, property_choices]
    )
    allowed_values.change(
        fn=select_allowed_values,
        inputs=allowed_values,
        outputs=[class_names, class_choices, datatypes]
    )
    class_names.input(
        fn=update_class_choices,
        inputs=class_names,
        outputs=class_choices
    )
    class_choices.select(
        fn=add_class,
        inputs=[class_names, class_choices],
        outputs=[class_names, class_choices]
    )
    insert_btn.click(
        fn=insert_constraint,
        inputs=[es_output, shape_name, property_name, allowed_values, class_names, datatypes, cardinality, comment],
        outputs=es_output
    )

    # example
    example_btn.click(
        fn=load_examples,
        inputs=[example_dropdown, es_output],
        outputs=[es_output, shape_name]
    )


demo = gr.TabbedInterface(
    interface_list=[introduction_tab, generate_tab, modify_single_tab],
    tab_names=["Introduction", "Generate", "Modify"],
    css="footer {visibility: hidden}",
    js=js
)


if __name__ == "__main__":
    demo.launch()