-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathanalyze_content_stream_test.py
113 lines (91 loc) · 3.82 KB
/
analyze_content_stream_test.py
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
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import uuid
import pytest
import conversation_management
import conversation_profile_management
import participant_management
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
AUDIO_FILE_PATH = "{0}/resources/book_a_room.wav".format(
os.path.realpath(os.path.dirname(__file__)),
)
@pytest.fixture
def conversation_profile_display_name():
return f"sample_conversation_profile_{uuid.uuid4()}"
@pytest.fixture
def conversation_profile_id(conversation_profile_display_name):
# Create conversation profile.
response = conversation_profile_management.create_conversation_profile_article_faq(
project_id=PROJECT_ID, display_name=conversation_profile_display_name
)
conversation_profile_id = response.name.split("conversationProfiles/")[1].rstrip()
yield conversation_profile_id
# Delete the conversation profile.
conversation_profile_management.delete_conversation_profile(
PROJECT_ID, conversation_profile_id
)
@pytest.fixture
def conversation_id(conversation_profile_id):
# Create conversation.
response = conversation_management.create_conversation(
project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id
)
conversation_id = response.name.split("conversations/")[1].rstrip()
yield conversation_id
# Complete the conversation.
conversation_management.complete_conversation(
project_id=PROJECT_ID, conversation_id=conversation_id
)
@pytest.fixture
def participant_id(conversation_id):
response = participant_management.create_participant(
project_id=PROJECT_ID, conversation_id=conversation_id, role="END_USER"
)
participant_id = response.name.split("participants/")[1].rstrip()
yield participant_id
# Test live transcription of an audio file with streaming_analyze_content.
def test_analyze_content_audio(capsys, conversation_id, participant_id):
# Call StreamingAnalyzeContent to transcribe the audio.
results = participant_management.analyze_content_audio(
conversation_id=conversation_id,
participant_id=participant_id,
audio_file_path=AUDIO_FILE_PATH,
)
out = " ".join([result.message.content for result in results]).lower()
assert "book a room" in out
# Test live transcription of an audio stream with streaming_analyze_content.
def test_analyze_content_audio_stream(capsys, conversation_id, participant_id):
class stream_generator:
def __init__(self, audio_file_path):
self.audio_file_path = audio_file_path
def generator(self):
with open(self.audio_file_path, "rb") as audio_file:
while True:
chunk = audio_file.read(4096)
if not chunk:
break
# The later requests contains audio data.
yield chunk
# Call StreamingAnalyzeContent to transcribe the audio.
results = participant_management.analyze_content_audio_stream(
conversation_id=conversation_id,
participant_id=participant_id,
sample_rate_herz=16000,
stream=stream_generator(AUDIO_FILE_PATH),
language_code="en-US",
timeout=300,
)
out = " ".join([result.message.content for result in results]).lower()
assert "book a room" in out