Repository navigation
Expand file tree
/
Copy pathstreaming_api_file.py
More file actions
64 lines (51 loc) · 1.9 KB
/
Copy pathstreaming_api_file.py
File metadata and controls
64 lines (51 loc) · 1.9 KB
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
import json
import argparse
from dotenv import load_dotenv
from behavioralsignals import Client, StreamingOptions
from behavioralsignals.utils import print_results, make_audio_stream
def parse_args():
parser = argparse.ArgumentParser(description="Behavioral Signals API Client Example")
parser.add_argument(
"--file_path", type=str, required=True, help="Path to the audio file to send"
)
parser.add_argument(
"--output", type=str, default="output.json", help="Path to save the output JSON file"
)
parser.add_argument(
"--api",
type=str,
default="behavioral",
choices=["behavioral", "deepfakes"],
help="API to use for streaming",
)
parser.add_argument(
"--response_level",
type=str,
default="segment",
choices=["segment", "utterance", "all"],
help="Level of response granularity",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
file_path, output = args.file_path, args.output
# Step 1. Initialize the client with your user ID and API key
load_dotenv()
client = Client()
# Step 2. Read the audio file, and wrap it inside an iterator of chunks
audio_stream, sample_rate = make_audio_stream(file_path, chunk_size=0.25)
options = StreamingOptions(
sample_rate=sample_rate, encoding="LINEAR_PCM", level=args.response_level
)
if args.api == "behavioral":
responses = client.behavioral.stream_audio(audio_stream=audio_stream, options=options)
else:
responses = client.deepfakes.stream_audio(audio_stream=audio_stream, options=options)
output_data = []
for resp in responses:
print_results(resp.results)
resp_data = resp.model_dump()
output_data.append(resp_data)
with open(output, "w") as f:
json.dump(output_data, f, indent=4)
print(f"Results saved to {output}")