forked from GoogleCloudPlatform/document-ai-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_cf.sh
65 lines (37 loc) · 2.76 KB
/
deploy_cf.sh
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
#!/bin/bash
#####################################################################################################
# Script Name: deploy_cf.sh
# Date of Creation: 12/01/2022
# Author: Ankur Wahi
# Updated: 12/02/2022
#####################################################################################################
# shellcheck disable=SC2154,SC1091,SC2153
source ./config.sh
project_id=${PROJECT_ID}
cf_create_docai="create-docai"
cf_process_docai="parser-docai"
echo "Deploying Doc AI CF"
cd ~/document-ai-samples/sql-pdf-python/src/cloud-functions/create_docai || exit
# shellcheck disable=SC2154
gcloud functions deploy ${cf_create_docai} --entry-point get_request --runtime python39 --trigger-http --allow-unauthenticated --project "${project_id}" --service-account "${doc_sa}" --gen2 --region "${REGION}" --run-service-account "${doc_sa}" --memory 256MB
endpoint_create_docai=$(gcloud functions describe ${cf_create_docai} --region="${REGION}" --gen2 --format=json | jq -r '.serviceConfig.uri')
echo "Creating Doc AI Processor"
processor_id=$(curl -m 1010 -X POST "${endpoint_create_docai}" -H "Authorization: bearer $(gcloud auth print-identity-token)" -H "Content-Type: application/json" -d '{
"name": "Create Processor Trigger"
}')
cd ~/document-ai-samples/sql-pdf-python/src/cloud-functions/process_docai || exit
gcloud functions deploy ${cf_process_docai} --entry-point get_doc --runtime python39 --trigger-http --allow-unauthenticated --project "${project_id}" --service-account "${doc_sa}" --gen2 --region "${REGION}" --run-service-account "${doc_sa}" --memory 256MB
echo "Processor Id ${processor_id} has been created"
endpoint_parser_docai=$(gcloud functions describe ${cf_process_docai} --region="${REGION}" --gen2 --format=json | jq -r '.serviceConfig.uri')
bq mk -d docai
build_sql="CREATE OR REPLACE FUNCTION docai.doc_extractor(uri STRING, content_type STRING, location STRING,docai_processorId STRING) RETURNS STRING REMOTE WITH CONNECTION \`${project_id}.us.gcf-docai-conn\` OPTIONS ( endpoint = '${endpoint_parser_docai}')"
bq query --use_legacy_sql=false "${build_sql}"
echo "Creating bucket"
BUCKET_NAME=${PROJECT_ID}-docai-wahi
gcloud storage buckets create gs://"${BUCKET_NAME}"
echo "Bucket ${BUCKET_NAME} has been created"
echo "Uploading sample expense docs from data folder to ${BUCKET_NAME} "
gsutil cp ~/document-ai-samples/sql-pdf-python/src/data/* gs://"${BUCKET_NAME}"
bq mk --external_table_definition=gs://"${BUCKET_NAME}"/*@projects/"${PROJECT_ID}"/locations/us/connections/gcf-docai-conn --object_metadata=DIRECTORY --max_staleness=0:30:0 --metadata_cache_mode=AUTOMATIC --table docai.repos
sql_stmt="select docai.doc_extractor(uri,content_type,\"us\",\"${processor_id}\") FROM docai.repos LIMIT 10"
bq query --use_legacy_sql=false "${sql_stmt}"