-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
66 lines (50 loc) · 2.11 KB
/
model.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
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/
// const userResponse = require("./GROOP.v0/modelTest");
// const text = userResponse;
const text = "The conversation was great"
const endpointId = "653956530852331520";
const project = 'hardy-thinker-402615';
const location = 'us-central1';
const aiplatform = require('@google-cloud/aiplatform');
const {instance, prediction} =
aiplatform.protos.google.cloud.aiplatform.v1.schema.predict;
// Imports the Google Cloud Model Service Client library
const {PredictionServiceClient} = aiplatform.v1;
// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};
// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);
async function predictTextSentimentAnalysis() {
// Configure the endpoint resource
const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`;
const instanceObj = new instance.TextSentimentPredictionInstance({
content: text,
});
const instanceVal = instanceObj.toValue();
const instances = [instanceVal];
const request = {
endpoint,
instances,
};
// Predict request
const [response] = await predictionServiceClient.predict(request);
console.log('Predict text sentiment analysis response:');
console.log(`\tDeployed model id : ${response.deployedModelId}`);
console.log('\nPredictions :');
// Assuming there's only one prediction value
const predictionResult =
prediction.TextSentimentPredictionResult.fromValue(
response.predictions[0]
);
console.log(`\tSentiment measure: ${predictionResult.sentiment}`);
return predictionResult.sentiment;
}
// If you want to run the function when this file is executed directly
// predictTextSentimentAnalysis();
// Export the function for use in other files
module.exports.predictTextSentimentAnalysis = predictTextSentimentAnalysis;