-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (204 loc) · 5.88 KB
/
index.js
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
const _ = require('lodash');
const firebase = require('firebase/app');
require('firebase/auth');
require('firebase/storage');
require('firebase/functions');
require('firebase/database');
const {
buildAinftTrainRequestTxBody,
buildAuthTxBody,
signTx,
} = require('./AinUtil');
const firebaseConfig = {
apiKey: process.env.GPT2_FIREBASE_API_KEY,
authDomain: process.env.GPT2_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.GPT2_FIREBASE_DATABASE_URL,
projectId: process.env.GPT2_FIREBASE_PROJECT_ID,
storageBucket: process.env.GPT2_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.GPT2_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.GPT2_FIREBASE_APP_ID,
measurementId: process.env.GPT2_FIREBASE_MEASUREMENT_ID,
};
let gpt2Firebase = {};
if (!firebase.apps.find((el) => el.name_ === 'GPT2Firebase')) {
gpt2Firebase = firebase.initializeApp(firebaseConfig, 'GPT2Firebase');
console.log('GPT-2 Firebase initialized.');
} else {
gpt2Firebase = firebase.app('GPT2Firebase');
}
const GPT2FirebaseManager = {
async signInWithCustomToken(address, privateKey) {
try {
const timestamp = Date.now();
const txBody = buildAuthTxBody(address, timestamp);
const { signedTx } = signTx(txBody, privateKey);
const response = await gpt2Firebase
.functions()
.httpsCallable('getAuthToken')(signedTx);
const customToken = _.get(response, 'data.customToken');
await gpt2Firebase.auth().signInWithCustomToken(customToken);
return response.data;
} catch (e) {
console.error(e);
return null;
}
},
async requestJobType(task) {
const res = await gpt2Firebase.functions().httpsCallable('getJobTypes')();
let ret = {};
if (task && res.data) {
ret = [];
for (const key in res.data) {
const jobInfo = res.data[key];
if (task === jobInfo.task) {
ret.push({
label: jobInfo.name,
value: key,
urlTemplate: {
demo: jobInfo.demoURLTemplate,
api: jobInfo.apiURLTemplate,
insight: jobInfo.insightURLTemplate,
},
});
}
}
return ret;
}
Object.entries(res.data).forEach(([key, value]) => {
// FIXME(YoungJaeKim): refactoring after eth Denver event
if(value.task !== 'ainft-chatbot'){
ret[value.name] = key;
}
});
return ret;
},
async uploadAINFTImageFile(address, trainId, file) {
const ref = gpt2Firebase
.storage()
.ref(`ainftImage/${address}/${trainId}/${file.name}`);
await ref.put(file);
return await ref.getDownloadURL();
},
async uploadDataFile(address, trainId, file, callback) {
const ref = gpt2Firebase
.storage()
.ref(`trainData/${address}/${trainId}/${file.name}`);
const uploadTask = ref.put(file);
uploadTask.on('state_changed', (snapshot) => {
let progress = parseInt(
(snapshot.bytesTransferred / snapshot.totalBytes) * 100,
);
callback(progress);
});
return uploadTask;
},
async requestAinftTrain(data) {
const {
trainId,
fileName,
fileSize,
jobType,
address,
privateKey,
ainizeUid,
ainizeMail,
metadata,
} = data;
try {
const timestamp = Date.now();
const txBody = buildAinftTrainRequestTxBody({
trainId,
fileName,
fileSize,
jobType: jobType.value,
jobTypeName: jobType.label,
address,
ainizeUid,
ainizeMail,
metadata,
timestamp,
});
const { signedTx } = signTx(txBody, privateKey);
const { result, ...rest } = await gpt2Firebase
.functions()
.httpsCallable('sendSignedTransaction')(signedTx);
} catch (error) {
console.error(error);
}
},
async getTrainIdInfo(address, trainId, task) {
if (!address || !trainId) {
return [null, false];
}
const taskInfo = (
await gpt2Firebase
.database()
.ref(`train_tasks/${address}/${trainId}`)
.once('value')
).val();
if (!taskInfo) {
return [null, false];
}
if (task) {
const { request, response, mint } = taskInfo;
if (taskInfo.request.task === task) {
const file = {
name: request.data.fileName,
size: request.data.fileSize,
};
const nft = {
asset_contract: {
address: request.data.metadata.sourceNftContractAddress,
},
token_id: request.data.metadata.sourceNftTokenId,
ainftImageUrl: request.data.metadata.ainftImageUrl,
image_url: request.data.metadata.sourceImageUrl,
description: request.data.metadata.description,
name: request.data.metadata.name,
};
const nickname = trainId;
const isTrained = (response !== undefined && response.few_shot_learning !== undefined) ? (response.few_shot_learning.status === 'ainize') : undefined;
return [
{
file,
nft,
nickname,
mint,
},
isTrained,
];
} else {
return [null, false];
}
}
},
async setErc721TxHash(txHash, appName) {
try {
await gpt2Firebase.functions().httpsCallable('updateMintInfo')({
trainId: appName,
txHash,
});
await gpt2Firebase
.database()
.ref(`/teachable_ainft/erc721_mint_pending_txs/${txHash}`)
.set(appName);
} catch (e) {
console.error(e);
}
},
async setHrc721TxHash(txHash, appName) {
try {
await gpt2Firebase.functions().httpsCallable('updateMintInfo')({
trainId: appName,
txHash,
});
await gpt2Firebase
.database()
.ref(`/teachable_ainft/hrc721_mint_pending_txs/${txHash}`)
.set(appName);
} catch (e) {
console.error(e);
}
},
};
module.exports = GPT2FirebaseManager;