File size: 6,357 Bytes
63858e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as d3 from 'd3';
import { debug } from 'util';
import { TokenDisplay } from '../data/TokenWrapper'
import * as tp from '../etc/types'
import * as rsp from './responses'
import * as R from 'ramda'
import { DemoAPI } from './demoAPI'
import * as hash from 'object-hash'
import { makeUrl, toPayload } from '../etc/apiHelpers'
import { URLHandler } from '../etc/URLHandler';

export const emptyTokenDisplay = new TokenDisplay()

const baseurl = URLHandler.basicURL()

/**
 * A rewrite of `d3-fetch`'s `d3.json` callback. If an api call fails, make a backup call to specified url and payload, if specified.
 * 
 * @param response Object expected at time of callback
 * @param backupUrl Backup url in the event of fail
 * @param backupPayload Backup payload if making a post request
 */
function responseJson(response, backupUrl = null, backupPayload = null) {
    if (!response.ok) {
        if (backupUrl != null) {
            console.log("STATIC FILE NOT FOUND");
            return fetch(backupUrl, backupPayload).then(responseJson);
        }
        throw new Error(response.status + " " + response.statusText)
    }
    return response.json()
}

/**
 * Check first if the information being sent exists in a static demo file. If it does, send that. Otherwise, make a normal call to the server.
 * 
 * @param toSend The packet of information to send to an API endpoint
 * @param backupUrl Backup url in the event that the demo file is not found
 * @param backupPayload Backup payload if demo file not found, for POST requests only
 */
function checkDemoAPI(toSend, backupUrl = null, backupPayload = null) {
    const hsh = hash.sha1(toSend);
    console.log("CHECKING DEMOAPI: " + hsh);
    if (DemoAPI.hasOwnProperty(hsh)) {
        // Relies on a symbolic link being present in the dist folder to the demo folder
        const path = './demo/' + DemoAPI[hsh]
        console.log("TRYING TO SENDING STATIC: ", path);
        const follow = (response) => responseJson(response, backupUrl, backupPayload)
        return fetch(path).then(follow)
    }
    return d3.json(backupUrl, backupPayload)
}


export class API {

    constructor(private baseURL: string = null) {
        if (this.baseURL == null) {
            this.baseURL = baseurl + '/api';
        }
    }

    getModelDetails(model: string, hashObj: {} | null = null): Promise<rsp.ModelDetailResponse> {
        const toSend = {
            model: model
        }

        const url = makeUrl(this.baseURL + "/get-model-details", toSend)
        console.log("--- GET " + url);

        if (hashObj != null) {
            const key = hash.sha1(toSend)
            d3.json(url).then(r => {
                hashObj[key] = r;
            })
        }

        return checkDemoAPI(toSend, url)
    }

    getMetaAttentions(model: string, sentence: string, layer: number, hashObj: {} | null = null): Promise<rsp.AttentionDetailsResponse> {
        const toSend = {
            model: model,
            sentence: sentence,
            layer: layer
        };

        const url = makeUrl(this.baseURL + "/attend+meta", toSend)
        console.log("--- GET " + url);

        // Add hash and value to hashObj
        if (hashObj != null) {
            const key = hash.sha1(toSend)
            d3.json(url).then(r => {
                hashObj[key] = r;
            })
        }

        return checkDemoAPI(toSend, url)
    }

    /**
     * Update the display based on the information that was already parsed from the passed sentence.
     * 
     * @param a The displayed tokens in the columns 
     * @param sentenceA The original sentence that led to the tokenized information in `a`
     * @param layer Which layer to search at
     * @param hashObj If not null, store the information of the responses into the passed object. Used for creating demos.
     */
    updateMaskedAttentions(model: string, tokens: TokenDisplay, sentence: string, layer: number, hashObj: {} | null = null): Promise<rsp.AttentionDetailsResponse> {
        const toSend = {
            model: model,
            tokens: R.map(R.prop('text'), tokens.tokenData),
            sentence: sentence,

            // Empty masks need to be sent as a number, unfortunately. Choosing -1 for this
            mask: tokens.maskInds.length ? tokens.maskInds : [-1],
            layer: layer,
        }

        const url = makeUrl(this.baseURL + '/update-mask');
        const payload = toPayload(toSend)


        if (hashObj != null) {
            // Add hash and value to hashObj for demo purposes
            const key = hash.sha1(toSend)
            d3.json(url, payload).then(r => {
                hashObj[key] = r;
            })
        }

        console.log("--- POST " + url, payload);

        return checkDemoAPI(toSend, url, payload)
    }

    /**
     * 
     * @param embedding Embedding of the word
     * @param layer In the l'th layer
     * @param k how many results to retrieve
     */
    getNearestEmbeddings(model: string, corpus: string, embedding: number[], layer: number, heads: number[], k = 10, hashObj: {} | null = null): Promise<rsp.NearestNeighborResponse> {
        const toSend = {
            model: model,
            corpus: corpus,
            embedding: embedding,
            layer: layer,
            heads: heads,
            k: k,
        }

        const url = makeUrl(this.baseURL + '/k-nearest-embeddings', toSend);
        console.log("--- GET " + url);

        if (hashObj != null) {
            const key = hash.sha1(toSend)
            d3.json(url).then(r => {
                hashObj[key] = r;
            })
        }

        return checkDemoAPI(toSend, url)
    }

    getNearestContexts(model: string, corpus: string, context: number[], layer: number, heads: number[], k = 10, hashObj: {} | null = null): Promise<rsp.NearestNeighborResponse> {
        const toSend = {
            model: model,
            corpus: corpus,
            context: context,
            layer: layer,
            heads: heads,
            k: k,
        }

        const url = makeUrl(this.baseURL + '/k-nearest-contexts', toSend);
        console.log("--- GET " + url);

        if (hashObj != null) {
            const key = hash.sha1(toSend)
            d3.json(url).then(r => {
                hashObj[key] = r;
            })
        }

        return checkDemoAPI(toSend, url)
    }
};