File size: 10,461 Bytes
5953ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
959183d
95d7446
959183d
95d7446
959183d
5953ef9
 
 
 
959183d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5953ef9
 
 
 
 
 
959183d
5953ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
959183d
5953ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
959183d
 
5953ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
959183d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5953ef9
 
 
 
 
 
 
 
 
 
 
 
959183d
5953ef9
 
 
 
 
 
 
959183d
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>🪞Mirror</title>
  <link rel="stylesheet" href="https://unpkg.com/boltcss/bolt.min.css">
  <script type="importmap">
    {
      "imports": {
        "vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
      }
    }
  </script>
  <style>
    body {
      max-width: 800px;
      margin: 40px auto;
      padding: 0 20px;
    }

    .form-group {
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      align-items: center;
    }

    label {
      margin-right: 1rem;
    }

    button {
      margin: 0.2rem 0.2rem;
    }

    button:hover {
      background-color: #dbdbdb;
    }

    footer {
      text-align: center;
      margin-top: 2rem;
    }

    input {
      width: 100%;
    }

    .button-group {
      margin-top: 1rem;
      margin-bottom: 1rem;
    }

    .submit-button {
      background-color: #ffc83d;
      color: #d67d00;
      font-weight: bold;
    }

    .lc-button {
      background-color: #c4e5be;
    }

    .lm-button {
      background-color: #dae7fb;
    }

    .lr-button {
      background-color: #fff3ce;
    }

    .submit-button:hover {
      background-color: #ffc83dc0;
    }

    .download-button {
      background-color: #98ca56;
      color: white;
      font-weight: bold;
    }

    .download-button:hover {
      background-color: #98ca56d1;
    }

    .output-title {
      margin-top: 2rem;
      margin-bottom: 0;
      display: block;
      background-color: #98ca56;
      color: white;
      font-weight: bold;
      font-size: large;
      padding: 6px 15px;
      border-top-left-radius: 6px;
      border-top-right-radius: 6px;
    }

    .output-box {
      margin-top: 0;
      padding: 6px 15px;
      background-color: white;
      border: 2px solid #98ca56;
      border-bottom-left-radius: 6px;
      border-bottom-right-radius: 6px;
    }
  </style>
</head>

<body>
  <header>
    <h1>🪞Mirror</h1>
    <p>
      🪞Mirror can help you deal with a wide range of Natural Language Understanding and Information Extraction tasks.
    </p>
    <p>🔍 Mechanism behind this demo: <a href="https://arxiv.org/abs/2311.05419" target="_blank">Mirror paper</a>.</p>
    <p>
      🌟 Star me on <a href="https://github.com/Spico197/Mirror" target="_blank">GitHub</a>!
    </p>
    <p>📢 This demo only supports English tasks. For Chinese Information Extraction tasks, please refer to this model: <a href="https://huggingface.co/Spico/mirror-chinese-mrcqa-alpha" target="_blank">Spico/mirror-chinese-mrcqa-alpha</a>.</p>
  </header>

  <main>
    <div id="app">
      <div>
        <label for="example-select"><strong>Examples</strong></label>
        <select id="example-select" v-model="selectedExample">
          <optgroup label="Named Entity Recognition">
            <option>Person & Nationality</option>
          </optgroup>
          <optgroup label="Relation Extraction">
            <option>Break up</option>
          </optgroup>
          <optgroup label="Classification">
            <option>The most handsome man</option>
            <option>I'm more handsome!</option>
          </optgroup>
          <optgroup label="Machine Reading Comprehension">
            <option>The former CEO of Apple</option>
            <option>The new CEO of Apple</option>
          </optgroup>
        </select>
      </div>
      <div>
        <label for="instruction"><strong>Instruction</strong></label>
        <input id="instruction" type="text" v-model="instruction" placeholder="Mirror mirror tell me ..." size="200">
      </div>
      <div>
        <label for="schema"><strong>Schema Labels</strong></label>
        <p>For entities, relations or classification, input <code>{"ent|rel|cls": ["type1", "type2"]}</code> .</p>
        <input id="schema" type="text" v-model="schema" size="200">
      </div>
      <div>
        <label for="text"><strong>Text</strong></label>
        <input id="text" type="text" v-model="text" size="200">
      </div>
      <!-- <div>
        <label for="background"><strong>Background</strong></label>
        <input id="background" type="text" v-model="background" size="200">
      </div> -->

      <div class="button-group">
        <button @click.prevent="reset">Reset</button>
        <button @click.prevent="clearOutput">Clear Output</button>
        <button class="submit-button" @click.prevent="getResults">Ask Mirror</button>
      </div>

      <div v-if="timerHandler">
        <p>⏱️ {{ searchSecondsString }}</p>
      </div>

      <div>
        <div v-if="isNotEmptyObj(results)">
          <label for="output"><strong>Output</strong></label>
          <table>
            <thead>
              <th>Item</th>
              <th>Predicted</th>
            </thead>
            <tbody>
              <tr v-for="(value, key, index) in results" :key="index">
                <template v-if="value.length">
                  <td>{{ key }}</td>
                  <td>{{ value }}</td>
                </template>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

    </div>
  </main>

  <footer>
    <hr>
    Made by Mirror Team w/ 💖
  </footer>

  <script type="module">
    import { createApp, ref, computed, toRaw, watch } from 'vue'

    createApp(
      {
        setup() {
          const instruction = ref("")
          const text = ref("")
          const background = ref("")
          const selectedExample = ref("")
          const schema = ref("{}")
          const results = ref({})
          const timerHandler = ref(0)
          const searchSeconds = ref(0.0)
          const searchSecondsString = computed(() => {
            return `${searchSeconds.value.toFixed(1)}s`
          })

          function isNotEmptyObj(obj) {
            return Object.keys(obj).length > 0
          }

          function clearOutput() {
            timerHandler.value = 0
            results.value = {}
          }

          function reset() {
            schema.value = "{}"
            instruction.value = ""
            text.value = ""
            clearOutput()
          }

          function startTimer() {
            searchSeconds.value = 0.0
            timerHandler.value = setInterval(() => {
              searchSeconds.value += 0.1
            }, 100)
          }

          function endTimer() {
            if (timerHandler.value > 0) {
              clearInterval(timerHandler.value)
            }
          }

          function getResults() {
            clearOutput()
            startTimer()
            const data = {
              "id": Date.now().toString(),
              "instruction": instruction.value,
              "schema": JSON.parse(schema.value),
              "text": text.value,
              "background": background.value,
              "ans": {},
            }
            const postData = JSON.stringify({
              "data": [data],
            })
            fetch(
              "/process",
              {
                method: "POST",
                headers: {
                  'Content-Type': 'application/json',
                },
                body: postData,
              }
            )
              .then((response) => response.json())
              .then((json) => {
                if (json["ok"] === false) {
                  alert(json["msg"])
                } else {
                  results.value = json["results"][0]["results"]
                }
              })
              .catch((err) => { alert(err) })
              .finally(() => endTimer())
          }

          watch(
            () => selectedExample.value,
            (newValue, oldValue) => {
              if (newValue !== oldValue) {
                reset()
                console.log(newValue)
                if (newValue === "Person & Nationality") {
                  instruction.value = "Please identify any entities in the provided text and classify them based on their types."
                  text.value = "Isaac Newton was an English mathematician."
                  schema.value = '{"ent": ["person", "nationality"]}'
                } else if (newValue === "Break up") {
                  instruction.value = "Please extract the entity relationship triplet."
                  text.value = "The drama surrounding the high-profile divorce between Hollywood actors Johnny Depp and Amber Heard appears to be over as the couple reportedly reached an amicable settlement."
                  schema.value = '{"rel": ["break up"]}'
                } else if (newValue === "The most handsome man") {
                  instruction.value = "Mirror Mirror tell me, who's the most handsome man in the world?"
                  text.value = ""
                  schema.value = '{"cls": ["Tong Zhu", "Leonardo Dicaprio"]}'
                } else if (newValue === "I'm more handsome!") {
                  instruction.value = "Mirror Mirror tell me, who's the most handsome man in the world?"
                  text.value = "Tong Zhu won the Most Handsome Man Award every year for ten consecutive years."
                  schema.value = '{"cls": ["Tong Zhu", "Leonardo Dicaprio"]}'
                } else if (newValue === "The former CEO of Apple") {
                  instruction.value = "Who's the former CEO of Apple?"
                  text.value = "After Jobs resigned as CEO and became chairman of the board, Cook was named the new chief executive officer of Apple Inc. on August 24, 2011."
                  schema.value = '{}'
                } else if (newValue === "The new CEO of Apple") {
                  instruction.value = "Who's the new CEO of Apple?"
                  text.value = "After Jobs resigned as CEO and became chairman of the board, Cook was named the new chief executive officer of Apple Inc. on August 24, 2011."
                  schema.value = '{}'
                }
              }
            }
          )

          return {
            instruction,
            text,
            background,
            schema,
            results,
            reset,
            clearOutput,
            getResults,
            searchSecondsString,
            timerHandler,
            isNotEmptyObj,
            selectedExample,
          }
        }
      }
    ).mount("#app")
  </script>
</body>

</html>