Bohui Zhang commited on
Commit
29beee6
·
1 Parent(s): 3b61bec

Update interface

Browse files
Files changed (1) hide show
  1. app.py +113 -151
app.py CHANGED
@@ -1,26 +1,58 @@
1
  from esgen.functions import *
2
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  with gr.Blocks() as introduction_tab:
5
  gr.Markdown(
6
  """
7
- # EntitySchema Generator
8
-
9
- This is an EntitySchema Generator consists of three components:
10
- - The **Generate** tab is used to generate an EntitySchema from scratch through a frequent-based draft and a user-based refinement step.
11
- - The **Append** tab is used to add properties that are missing in the first step.
12
- - The **Modify** tab is used to edit properties in an EntitySchema.
13
-
14
- What about other functions?
15
- - **Delete**: the EntitySchema code is interactive so that one can directly edit or delete content.
16
 
17
  ## Resources:
18
- - [Wikidata Project Schemas](https://www.wikidata.org/wiki/Wikidata:WikiProject_Schemas)
19
  - [Shape Expressions (ShEx) 2.1 Primer](https://shex.io/shex-primer/)
20
  - [EntitySchema directory](https://www.wikidata.org/wiki/Wikidata:Database_reports/EntitySchema_directory)
21
  - [WikiShape](https://wikishape.weso.es)
22
  - [RDFShape](https://rdfshape.weso.es)
23
- - A similar toolkit but more from scratch can be found [here at Toolforge](https://tools-static.wmflabs.org/entityschema-generator/).
 
24
  """
25
  )
26
 
@@ -28,37 +60,51 @@ with gr.Blocks() as introduction_tab:
28
  with gr.Blocks() as generate_tab:
29
  gr.Markdown(
30
  """
31
- # EntitySchema Generator: Generate ES from scratch
 
 
 
32
  """
33
  )
34
  with gr.Row():
35
  with gr.Column():
36
  es_name = gr.Textbox(
37
- label="Name of EntitySchema",
38
  placeholder="e.g., algorithm"
39
  )
40
  with gr.Group():
41
  es_class = gr.Textbox(
42
- label="Class ID",
43
- placeholder="e.g., Q8366"
 
 
44
  )
45
- class_choices = gr.Radio(visible=False)
46
- extra_checkbox = gr.Checkbox(label="Permitting extra properties")
47
- generate_btn = gr.Button(value="Generate")
 
 
 
 
 
 
48
  gr.Markdown(
49
  """
50
- ## Refinement
51
- The chatbot will ask you a series of questions to refine the EntitySchema. Click the following buttons to answer the questions.
52
- - If you think the property is required for the EntitySchema, click "Yes".
53
- - If you think the property is optional, click "Optional".
54
- - If you think the property is not properly used for the EntitySchema, click "No".
55
- - If you are unsure, click "Unsure".
 
56
  """
57
  )
 
58
 
59
  refine_chatbot = gr.Chatbot(
60
  label="Chatbot",
61
- value=[[None, None]]
 
62
  )
63
  # chatbot_input = gr.Textbox(placeholder="Type your message here :)")
64
  with gr.Row():
@@ -71,13 +117,14 @@ with gr.Blocks() as generate_tab:
71
  no_btn = gr.Button(
72
  value="⛔️ No"
73
  )
74
- unsure_btn = gr.Button(
75
- value="🤔 Unsure"
76
  )
77
 
78
  with gr.Column():
 
79
  es_output = gr.Code(
80
- lines=60,
81
  label="EntitySchema",
82
  interactive=True,
83
  )
@@ -87,19 +134,12 @@ with gr.Blocks() as generate_tab:
87
  visible=False,
88
  )
89
 
90
- # gr.Examples(
91
- # examples=["algorithm"],
92
- # inputs=es_output,
93
- # outputs=es_output,
94
- # fn=load_examples,
95
- # cache_examples=True
96
- # )
97
-
98
  es_class.input(
99
  fn=update_class_choices,
100
  inputs=es_class,
101
  outputs=class_choices
102
  )
 
103
  class_choices.select(
104
  fn=select_class,
105
  inputs=[class_choices],
@@ -108,7 +148,7 @@ with gr.Blocks() as generate_tab:
108
 
109
  generate_btn.click(
110
  fn=entity_schema_generation,
111
- inputs=[es_name, es_class, extra_checkbox],
112
  outputs=[es_output, es_json, refine_chatbot]
113
  )
114
 
@@ -127,23 +167,43 @@ with gr.Blocks() as generate_tab:
127
  inputs=[es_json, refine_chatbot],
128
  outputs=[es_output, es_json, refine_chatbot]
129
  )
130
- unsure_btn.click(
131
- fn=input_unsure,
132
  inputs=[es_json, refine_chatbot],
133
  outputs=[es_output, es_json, refine_chatbot]
134
  )
135
 
136
 
137
- with gr.Blocks() as append_tab:
138
  gr.Markdown(
139
  """
140
- # EntitySchema Generator: Add properties
 
 
 
 
141
  """
142
  )
143
  with gr.Row():
144
  with gr.Column():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  property_name = gr.Textbox(
146
- label="Property Name"
147
  )
148
  property_choices = gr.Radio(
149
  visible=False
@@ -183,7 +243,7 @@ with gr.Blocks() as append_tab:
183
 
184
  with gr.Column():
185
  es_output = gr.Code(
186
- lines=25,
187
  label="EntitySchema",
188
  interactive=True,
189
  )
@@ -193,14 +253,7 @@ with gr.Blocks() as append_tab:
193
  # visible=False,
194
  # )
195
 
196
- # gr.Examples(
197
- # examples=["algorithm"],
198
- # inputs=es_output,
199
- # outputs=es_output,
200
- # fn=load_examples,
201
- # cache_examples=True
202
- # )
203
-
204
  property_name.input(
205
  fn=update_property_choices,
206
  inputs=property_name,
@@ -226,116 +279,25 @@ with gr.Blocks() as append_tab:
226
  inputs=[class_names, class_choices],
227
  outputs=[class_names, class_choices]
228
  )
229
- # add_class_btn.click(
230
- # fn=add_classes,
231
- # inputs=class_names,
232
- # outputs=class_names
233
- # )
234
  insert_btn.click(
235
  fn=insert_triple_constraint,
236
  inputs=[es_output, property_name, allowed_values, class_names, datatypes, cardinality, comment],
237
  outputs=es_output
238
  )
239
 
240
-
241
- with gr.Blocks() as modify_tab:
242
- gr.Markdown(
243
- """
244
- # EntitySchema Generator: Change properties
245
- """
246
- )
247
- with gr.Row():
248
- with gr.Column():
249
- es_input = gr.Code(
250
- lines=23,
251
- label="EntitySchema",
252
- interactive=True,
253
- )
254
- convert_btn = gr.Button(value="Convert")
255
- with gr.Column():
256
- # with gr.Row():
257
- # es_name = gr.Textbox(label="Name of EntitySchema")
258
- # es_class = gr.Textbox(label="Class ID")
259
- property_name = gr.Dropdown(
260
- label="Property Name"
261
- )
262
- property_choices = gr.Radio(
263
- visible=False
264
- )
265
- with gr.Group():
266
- allowed_values = gr.Dropdown(
267
- choices=[
268
- "with any value",
269
- "with one of a set of specific entities",
270
- "with an instance of",
271
- "with a subclass of",
272
- "XML Schema datatypes"
273
- ],
274
- label="Allowed Values"
275
- )
276
- class_names = gr.Textbox(label="Class Name", interactive=True, visible=False)
277
- class_choices = gr.Radio(visible=False)
278
- datatypes = gr.Dropdown(visible=False)
279
- # add_class_btn = gr.Button(value="Add", visible=False)
280
- cardinality = gr.Dropdown(
281
- choices=[
282
- "has 1 matching statement",
283
- "has 0 or 1 matching statements",
284
- "has 0 or more matching statements",
285
- "has 1 or more matching statements",
286
- "has no matching statements"
287
- ],
288
- label="Cardinality",
289
- interactive=True
290
- )
291
- with gr.Row():
292
- comment = gr.Textbox(
293
- label="Comment"
294
- )
295
-
296
- change_btn = gr.Button(value="Change")
297
-
298
- # gr.Examples(
299
- # examples=["algorithm"],
300
- # inputs=es_input,
301
- # outputs=es_input,
302
- # fn=load_examples,
303
- # cache_examples=True
304
- # )
305
-
306
- convert_btn.click(
307
- fn=convert_string_to_es,
308
- inputs=es_input,
309
- outputs=[property_name]
310
- )
311
-
312
- # TODO: Duplicated code fragment, merge Append and Modify tabs?
313
- allowed_values.change(
314
- fn=select_allowed_values,
315
- inputs=allowed_values,
316
- outputs=[class_names, class_choices, datatypes]
317
- )
318
- class_names.input(
319
- fn=update_class_choices,
320
- inputs=class_names,
321
- outputs=class_choices
322
- )
323
- class_choices.select(
324
- fn=add_class,
325
- inputs=[class_names, class_choices],
326
- outputs=[class_names, class_choices]
327
- )
328
-
329
- change_btn.click(
330
- fn=change_triple_constraint,
331
- inputs=[es_input, property_name, allowed_values, class_names, datatypes, cardinality, comment],
332
- outputs=es_input
333
  )
334
 
335
 
336
  demo = gr.TabbedInterface(
337
- interface_list=[introduction_tab, generate_tab, append_tab, modify_tab],
338
- tab_names=["Introduction", "Generate", "Append", "Modify"]
 
 
339
  )
340
 
341
 
 
1
  from esgen.functions import *
2
 
3
 
4
+ js = """
5
+ function createGradioAnimation() {
6
+ var container = document.createElement('div');
7
+ container.id = 'gradio-animation';
8
+ container.style.fontSize = '2em';
9
+ container.style.fontWeight = 'bold';
10
+ container.style.textAlign = 'center';
11
+ container.style.marginBottom = '20px';
12
+
13
+ var text = 'EntitySchema Generator';
14
+ for (var i = 0; i < text.length; i++) {
15
+ (function(i){
16
+ setTimeout(function(){
17
+ var letter = document.createElement('span');
18
+ letter.style.opacity = '0';
19
+ letter.style.transition = 'opacity 0.5s';
20
+ letter.innerText = text[i];
21
+
22
+ container.appendChild(letter);
23
+
24
+ setTimeout(function() {
25
+ letter.style.opacity = '1';
26
+ }, 50);
27
+ }, i * 250);
28
+ })(i);
29
+ }
30
+
31
+ var gradioContainer = document.querySelector('.gradio-container');
32
+ gradioContainer.insertBefore(container, gradioContainer.firstChild);
33
+
34
+ return 'Animation created';
35
+ }
36
+ """
37
+
38
+
39
  with gr.Blocks() as introduction_tab:
40
  gr.Markdown(
41
  """
42
+ This is a [Wikidata EntitySchema](https://www.wikidata.org/wiki/Wikidata:Schemas) Generator that consists of
43
+ two components:
44
+ - The **Generate** tab is used to generate an EntitySchema from scratch by analyzing existing items and
45
+ refining the results with the help of a chatbot.
46
+ - The **Modify** tab is used to add, change, or remove constraints from an existing EntitySchema.
 
 
 
 
47
 
48
  ## Resources:
49
+ - [Wikidata Schemas](https://www.wikidata.org/wiki/Wikidata:Schemas)
50
  - [Shape Expressions (ShEx) 2.1 Primer](https://shex.io/shex-primer/)
51
  - [EntitySchema directory](https://www.wikidata.org/wiki/Wikidata:Database_reports/EntitySchema_directory)
52
  - [WikiShape](https://wikishape.weso.es)
53
  - [RDFShape](https://rdfshape.weso.es)
54
+ - [ShExStatements](https://shexstatements.toolforge.org)
55
+ - A similar toolkit can be found [here on Toolforge](https://tools-static.wmflabs.org/entityschema-generator/).
56
  """
57
  )
58
 
 
60
  with gr.Blocks() as generate_tab:
61
  gr.Markdown(
62
  """
63
+ On this page, you can generate an EntitySchema from scratch. The tool will analyze existing items to draft an
64
+ EntitySchema for you. Afterwards, you can refine it by answering a few questions from a chatbot.
65
+
66
+ ## Step 1: Generate an EntitySchema from scratch
67
  """
68
  )
69
  with gr.Row():
70
  with gr.Column():
71
  es_name = gr.Textbox(
72
+ label="Name of the new EntitySchema",
73
  placeholder="e.g., algorithm"
74
  )
75
  with gr.Group():
76
  es_class = gr.Textbox(
77
+ label="Item ID for class",
78
+ placeholder="e.g., Q8366",
79
+ info="Specify which class the new EntitySchema will cover. The tool will look at all items that "
80
+ "are an 'instance of' (P31) the class."
81
  )
82
+ class_choices = gr.Radio(visible=False) # TODO: remove the label
83
+ threshold_slider = gr.Slider(
84
+ minimum=10, maximum=100, value=50, step=10, label="Cut-off",
85
+ info="Ignore all properties that are used on less than X% of items in the class for the initial "
86
+ "EntitySchema generation"
87
+ )
88
+ # extra_checkbox = gr.Checkbox(label="Permitting extra properties")
89
+ # TODO: remove EXTRA
90
+ generate_btn = gr.Button(value="Generate EntitySchema")
91
  gr.Markdown(
92
  """
93
+ ## Step 2: Refine the generated EntitySchema
94
+ The chatbot will ask you a series of questions to refine the EntitySchema. Click the following buttons
95
+ to answer the questions.
96
+ - If you think the property is required for all items in the class, click "Yes".
97
+ - If you think the property is only used on some items in the class, click "Optional".
98
+ - If you think the property should not be used on any items in the class, click "No".
99
+ - If you can't decide, feel free to click "Skip".
100
  """
101
  )
102
+ # TODO: what does the rejection ("No") semantically mean
103
 
104
  refine_chatbot = gr.Chatbot(
105
  label="Chatbot",
106
+ value=[[None, None]],
107
+ height=400,
108
  )
109
  # chatbot_input = gr.Textbox(placeholder="Type your message here :)")
110
  with gr.Row():
 
117
  no_btn = gr.Button(
118
  value="⛔️ No"
119
  )
120
+ skip_btn = gr.Button(
121
+ value=" Skip"
122
  )
123
 
124
  with gr.Column():
125
+ # TODO: meaningful error messages
126
  es_output = gr.Code(
127
+ lines=65,
128
  label="EntitySchema",
129
  interactive=True,
130
  )
 
134
  visible=False,
135
  )
136
 
 
 
 
 
 
 
 
 
137
  es_class.input(
138
  fn=update_class_choices,
139
  inputs=es_class,
140
  outputs=class_choices
141
  )
142
+ # TODO: more distinction between labels and descriptions
143
  class_choices.select(
144
  fn=select_class,
145
  inputs=[class_choices],
 
148
 
149
  generate_btn.click(
150
  fn=entity_schema_generation,
151
+ inputs=[es_name, es_class, threshold_slider],
152
  outputs=[es_output, es_json, refine_chatbot]
153
  )
154
 
 
167
  inputs=[es_json, refine_chatbot],
168
  outputs=[es_output, es_json, refine_chatbot]
169
  )
170
+ skip_btn.click(
171
+ fn=input_skip,
172
  inputs=[es_json, refine_chatbot],
173
  outputs=[es_output, es_json, refine_chatbot]
174
  )
175
 
176
 
177
+ with gr.Blocks() as modify_single_tab:
178
  gr.Markdown(
179
  """
180
+ On this page, you can modify an existing EntitySchema. You can load an example or paste your own EntitySchema
181
+ into the EntitySchema box on the right. Afterwards, you can specify the changes you would like to make to your
182
+ EntitySchema.
183
+
184
+ ## Step 1: Load an EntitySchema
185
  """
186
  )
187
  with gr.Row():
188
  with gr.Column():
189
+ # examples
190
+ example_dropdown = gr.Dropdown(
191
+ choices=["algorithm"],
192
+ label="EntitySchema Examples"
193
+ )
194
+ example_btn = gr.Button("Load example")
195
+
196
+ gr.Markdown(
197
+ """
198
+ ## Step 2: Modify the EntitySchema
199
+
200
+ You can add a new constraint to the EntitySchema by specifying it below. If the EntitySchema already
201
+ contains a constraint for the property, it will be overwritten by the new constraint. If you would like
202
+ to delete constraints, simply delete them in the EntitySchema box.
203
+ """
204
+ )
205
  property_name = gr.Textbox(
206
+ label="Property ID"
207
  )
208
  property_choices = gr.Radio(
209
  visible=False
 
243
 
244
  with gr.Column():
245
  es_output = gr.Code(
246
+ lines=46,
247
  label="EntitySchema",
248
  interactive=True,
249
  )
 
253
  # visible=False,
254
  # )
255
 
256
+ # single triple constraint
 
 
 
 
 
 
 
257
  property_name.input(
258
  fn=update_property_choices,
259
  inputs=property_name,
 
279
  inputs=[class_names, class_choices],
280
  outputs=[class_names, class_choices]
281
  )
 
 
 
 
 
282
  insert_btn.click(
283
  fn=insert_triple_constraint,
284
  inputs=[es_output, property_name, allowed_values, class_names, datatypes, cardinality, comment],
285
  outputs=es_output
286
  )
287
 
288
+ # example
289
+ example_btn.click(
290
+ fn=load_examples,
291
+ inputs=[example_dropdown],
292
+ outputs=[es_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  )
294
 
295
 
296
  demo = gr.TabbedInterface(
297
+ interface_list=[introduction_tab, generate_tab, modify_single_tab],
298
+ tab_names=["Introduction", "Generate", "Modify"],
299
+ css="footer {visibility: hidden}",
300
+ js=js
301
  )
302
 
303