-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
337 lines (312 loc) · 12.1 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
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
const fs = require("fs");
const path = require("path");
const {
getLocalRPCSettings,
initializeRPCConnection,
createAndFundNewPSLCreditTrackingAddress,
} = require("./rpc_functions");
const { PastelInferenceClient } = require("./pastel_inference_client");
const {
sendMessageAndCheckForNewIncomingMessages,
handleCreditPackTicketEndToEnd,
getCreditPackTicketInfoEndToEnd,
handleInferenceRequestEndToEnd,
} = require("./end_to_end_functions");
const { logger } = require("./logger");
const { prettyJSON } = require("./utility_functions");
async function main() {
const { rpcport } = await getLocalRPCSettings();
await initializeRPCConnection();
let burnAddress;
if (rpcport === "9932") {
burnAddress = "PtpasteLBurnAddressXXXXXXXXXXbJ5ndd";
} else if (rpcport === "19932") {
burnAddress = "tPpasteLBurnAddressXXXXXXXXXXX3wy7u";
} else if (rpcport === "29932") {
burnAddress = "44oUgmZSL997veFEQDq569wv5tsT6KXf9QY7";
}
const globals = require("./globals");
const useTestMessagingFunctionality = false;
const useTestCreditPackTicketFunctionality = false;
const useTestCreditPackTicketUsage = false;
const useTestInferenceRequestFunctionality = false;
const useTestLLMTextCompletion = false;
const useTestImageGeneration = false;
const MY_LOCAL_PASTELID = "";
const MY_PASTELID_PASSPHRASE = "";
const inferenceClient = new PastelInferenceClient(
MY_LOCAL_PASTELID,
MY_PASTELID_PASSPHRASE
);
// Define variables to store the credit pack ticket TXID and tracking address
let creditPackTicketPastelTxid;
let creditUsageTrackingPSLAddress;
const savedCreditPackTicketPastelTxid =
"e8954607c3e64a00e81b216516f403b687698c59ca31ef3faf92793a113fcd5e";
if (useTestMessagingFunctionality) {
const messageBody =
"Hello, this is a brand 🍉 NEW test message from a regular user!";
const toPastelID =
"jXXiVgtFzLto4eYziePHjjb1hj3c6eXdABej5ndnQ62B8ouv1GYveJaD5QUMfainQM3b4MTieQuzFEmJexw8Cr";
const messageDict = await sendMessageAndCheckForNewIncomingMessages(
toPastelID,
messageBody
);
logger.info(`Message data: ${prettyJSON(messageDict)}`);
}
if (useTestCreditPackTicketFunctionality && !creditPackTicketPastelTxid) {
const desiredNumberOfCredits = 250;
const amountOfPSLForTrackingTransactions = 10.0;
const creditPriceCushionPercentage = 0.15;
const maximumTotalAmountOfPSLToFundInNewTrackingAddress = 150000.0;
const estimatedTotalCostInPSLForCreditPack =
await inferenceClient.internalEstimateOfCreditPackTicketCostInPSL(
desiredNumberOfCredits,
creditPriceCushionPercentage
);
if (
estimatedTotalCostInPSLForCreditPack >
maximumTotalAmountOfPSLToFundInNewTrackingAddress
) {
logger.error(
`Estimated total cost of credit pack exceeds the maximum allowed amount of ${maximumTotalAmountOfPSLToFundInNewTrackingAddress} PSL`
);
throw new Error(
`Estimated total cost of credit pack exceeds the maximum allowed amount of ${maximumTotalAmountOfPSLToFundInNewTrackingAddress} PSL`
);
}
const amountToFundCreditTrackingAddress = Math.round(
amountOfPSLForTrackingTransactions + estimatedTotalCostInPSLForCreditPack
);
const { newCreditTrackingAddress: creditUsageTrackingPSLAddress } =
await createAndFundNewPSLCreditTrackingAddress(
amountToFundCreditTrackingAddress
);
const creditPackPurchaseRequestConfirmationResponse =
await handleCreditPackTicketEndToEnd(
desiredNumberOfCredits,
creditUsageTrackingPSLAddress,
burnAddress
);
if (creditPackPurchaseRequestConfirmationResponse) {
// Store the TXID and tracking address from the credit pack ticket creation response
creditPackTicketPastelTxid =
creditPackPurchaseRequestConfirmationResponse.pastel_api_credit_pack_ticket_registration_txid;
const creditUsageTrackingPSLAddress =
creditPackPurchaseRequestConfirmationResponse.credit_usage_tracking_psl_address;
logger.info(
`Credit pack ticket stored on the blockchain with transaction ID: ${creditPackPurchaseRequestConfirmationResponse.pastel_api_credit_pack_ticket_registration_txid} (corresponding to tracking address: ${creditUsageTrackingPSLAddress})`
);
logger.info(
`Credit pack details: ${prettyJSON(
creditPackPurchaseRequestConfirmationResponse
)}`
);
} else {
logger.error("Credit pack ticket storage failed!");
}
}
// Check if creditPackTicketPastelTxid is undefined
if (typeof creditPackTicketPastelTxid === "undefined") {
// If undefined, assign the value of savedCreditPackTicketPastelTxid
creditPackTicketPastelTxid = savedCreditPackTicketPastelTxid;
}
logger.info(
`Selected credit pack ticket transaction ID: ${creditPackTicketPastelTxid}`
);
if (useTestCreditPackTicketUsage) {
// Check if a credit pack ticket TXID is available
if (creditPackTicketPastelTxid) {
const startTime = Date.now();
const { requestResponse, requestConfirmation } =
await getCreditPackTicketInfoEndToEnd(creditPackTicketPastelTxid);
// Decoding Base64 using Buffer
let credit_pack_purchase_request_fields_json = Buffer.from(
requestResponse.credit_pack_purchase_request_fields_json_b64,
"base64"
).toString("utf-8");
const creditPackPurchaseRequestDict = JSON.parse(
credit_pack_purchase_request_fields_json
);
const initialCreditPackBalance =
creditPackPurchaseRequestDict.requested_initial_credits_in_credit_pack;
logger.info(
`Credit pack ticket data retrieved with initial balance ${initialCreditPackBalance}`
);
logger.info(
`Corresponding credit pack request dict: ${prettyJSON(
creditPackPurchaseRequestDict
)}`
);
const endTime = Date.now();
const durationInSeconds = (endTime - startTime) / 1000;
logger.info(
`Total time taken for credit pack ticket lookup: ${durationInSeconds.toFixed(
2
)} seconds`
);
}
}
if (useTestInferenceRequestFunctionality) {
if (useTestLLMTextCompletion) {
// Check if a credit pack ticket TXID is available
if (creditPackTicketPastelTxid) {
const startTime = Date.now();
const inputPromptTextToLLM =
"how do you measure the speed of an earthquake?";
const requestedModelCanonicalString = "claude3-opus";
const modelInferenceTypeString = "text_completion";
const modelParameters = {
number_of_tokens_to_generate: 2000,
number_of_completions_to_generate: 1,
};
const maxCreditCostToApproveInferenceRequest = 200.0;
const { inferenceResultDict, auditResults, validationResults } =
await handleInferenceRequestEndToEnd(
creditPackTicketPastelTxid,
inputPromptTextToLLM,
requestedModelCanonicalString,
modelInferenceTypeString,
modelParameters,
maxCreditCostToApproveInferenceRequest,
burnAddress
);
logger.info(
`Inference result data:\n\n${prettyJSON(inferenceResultDict)}`
);
logger.info(`Audit results:\n\n${prettyJSON(auditResults)}`);
logger.info(`Validation results:\n\n${prettyJSON(validationResults)}`);
logger.info(
"\n_____________________________________________________________________\n"
);
logger.info(
`\n\nFinal Decoded Inference Result:\n\n${inferenceResultDict.inference_result_decoded}`
);
const endTime = Date.now();
const durationInMinutes = (endTime - startTime) / 60000;
logger.info(
`Total time taken for inference request: ${durationInMinutes.toFixed(
2
)} minutes`
);
}
}
if (useTestImageGeneration) {
// Check if a credit pack ticket TXID is available
if (creditPackTicketPastelTxid) {
const startTime = Date.now();
const inputPromptTextToLLM =
"A picture of a clown holding a sign that says PASTEL";
const requestedModelCanonicalString = "stability-core";
const modelInferenceTypeString = "text_to_image";
const styleStringsList = [
"3d-model",
"analog-film",
"anime",
"cinematic",
"comic-book",
"digital-art",
"enhance",
"fantasy-art",
"isometric",
"line-art",
"low-poly",
"modeling-compound",
"neon-punk",
"origami",
"photographic",
"pixel-art",
"tile-texture",
];
const stylePresetString = styleStringsList[styleStringsList.length - 3];
const outputFormatList = ["png", "jpeg", "webp"];
const outputFormatString = outputFormatList[0];
const aspectRatioList = [
"16:9",
"1:1",
"21:9",
"2:3",
"3:2",
"4:5",
"5:4",
"9:16",
"9:21",
];
const aspectRatioString = aspectRatioList[0];
const randomSeed = Math.floor(Math.random() * 1001);
let modelParameters;
if (requestedModelCanonicalString.includes("core")) {
modelParameters = {
aspect_ratio: aspectRatioString,
seed: randomSeed,
style_preset: stylePresetString,
output_format: outputFormatString,
negative_prompt: "low quality, blurry, pixelated",
};
} else {
modelParameters = {
height: 512,
width: 512,
steps: 50,
seed: 0,
num_samples: 1,
negative_prompt: "low quality, blurry, pixelated",
style_preset: stylePresetString,
};
}
const maxCreditCostToApproveInferenceRequest = 200.0;
const { inferenceResultDict, auditResults, validationResults } =
await handleInferenceRequestEndToEnd(
creditPackTicketPastelTxid,
inputPromptTextToLLM,
requestedModelCanonicalString,
modelInferenceTypeString,
modelParameters,
maxCreditCostToApproveInferenceRequest,
burnAddress
);
logger.info(
`Inference result data received at ${new Date()}; decoded image size in megabytes: ${inferenceResultDict.generated_image_decoded.length / (1024 * 1024)
}`
);
logger.info(`Audit results:\n\n${prettyJSON(auditResults)}`);
logger.info(`Validation results:\n\n${prettyJSON(validationResults)}`);
logger.info(
"\n_____________________________________________________________________\n"
);
const currentDatetimeString = new Date()
.toISOString()
.replace(/[-:.]/g, "_")
.slice(0, -5);
const imageGenerationPromptWithoutWhitespaceOrNewlinesAbbreviatedTo100Characters =
inputPromptTextToLLM.replace(/\s+/g, "_").slice(0, 100);
const generatedImageFilename = `generated_image__prompt__${imageGenerationPromptWithoutWhitespaceOrNewlinesAbbreviatedTo100Characters}__generated_on_${currentDatetimeString}.${outputFormatString}`;
const generatedImageFolderName = "generated_images";
if (!fs.existsSync(generatedImageFolderName)) {
fs.mkdirSync(generatedImageFolderName);
}
const generatedImageFilePath = path.join(
generatedImageFolderName,
generatedImageFilename
);
const imageData = inferenceResultDict.generated_image_decoded;
fs.writeFileSync(generatedImageFilePath, imageData);
logger.info(`Generated image saved as '${generatedImageFilePath}'`);
const endTime = Date.now();
const durationInMinutes = (endTime - startTime) / 60000;
logger.info(
`Total time taken for inference request: ${durationInMinutes.toFixed(
2
)} minutes`
);
}
}
}
}
main().catch((error) => {
logger.error(`Error in main function: ${error.message.slice(0, globals.MAX_CHARACTERS_TO_DISPLAY_IN_ERROR_MESSAGE)}`);
process.exit(1);
});
module.exports = {
logger,
};