Skip to content

Commit b0c37c4

Browse files
authored
samples: create analysis (#44)
1 parent 40d1263 commit b0c37c4

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# [START contactcenterinsights_create_analysis]
16+
from google.cloud import contact_center_insights_v1
17+
18+
19+
def create_analysis(conversation_name: str) -> contact_center_insights_v1.Analysis:
20+
# Construct an analysis.
21+
analysis = contact_center_insights_v1.Analysis()
22+
23+
# Call the Insights client to create an analysis.
24+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
25+
analysis_operation = insights_client.create_analysis(
26+
parent=conversation_name, analysis=analysis
27+
)
28+
analysis = analysis_operation.result(timeout=86400)
29+
print(f"Created {analysis.name}")
30+
return analysis
31+
32+
33+
# [END contactcenterinsights_create_analysis]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
import google.auth
16+
17+
from google.cloud import contact_center_insights_v1
18+
19+
import pytest
20+
21+
import create_analysis
22+
23+
TRANSCRIPT_URI = "gs://cloud-samples-data/ccai/chat_sample.json"
24+
AUDIO_URI = "gs://cloud-samples-data/ccai/voice_6912.txt"
25+
26+
27+
@pytest.fixture
28+
def project_id():
29+
_, project_id = google.auth.default()
30+
return project_id
31+
32+
33+
@pytest.fixture
34+
def conversation_resource(project_id):
35+
# Create a conversation.
36+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
37+
38+
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
39+
project_id, "us-central1"
40+
)
41+
42+
conversation = contact_center_insights_v1.Conversation()
43+
conversation.data_source.gcs_source.transcript_uri = TRANSCRIPT_URI
44+
conversation.data_source.gcs_source.audio_uri = AUDIO_URI
45+
conversation.medium = contact_center_insights_v1.Conversation.Medium.CHAT
46+
47+
conversation = insights_client.create_conversation(
48+
parent=parent, conversation=conversation
49+
)
50+
yield conversation
51+
52+
# Delete the conversation.
53+
delete_request = contact_center_insights_v1.DeleteConversationRequest()
54+
delete_request.name = conversation.name
55+
delete_request.force = True
56+
insights_client.delete_conversation(request=delete_request)
57+
58+
59+
@pytest.fixture
60+
def analysis_resource(conversation_resource):
61+
conversation_name = conversation_resource.name
62+
yield create_analysis.create_analysis(conversation_name)
63+
64+
65+
def test_create_analysis(capsys, analysis_resource):
66+
analysis = analysis_resource
67+
out, err = capsys.readouterr()
68+
assert "Created {}".format(analysis.name) in out

0 commit comments

Comments
 (0)