-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe-file-enhanced.py
More file actions
101 lines (75 loc) · 3.16 KB
/
transcribe-file-enhanced.py
File metadata and controls
101 lines (75 loc) · 3.16 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
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
import json
import uuid
from time import sleep
from typing import Union
import requests
import os
import sys
from pathlib import Path
def transcribe(file_path: Union[str, Path], api_key: str, stream_configuration_template_id: str):
assert api_key, 'API_KEY is required'
stream_id: str = str(uuid.uuid4())
# Upload the file
upload_url: str = 'https://http-gateway.vatis.tech/http-gateway/api/v1/upload'
query_parameters: dict = {
'streamConfigurationTemplateId': stream_configuration_template_id,
'id': stream_id,
'persist': 'true',
# transcription enhancement parameters
'enhancedTranscription': 'true',
}
upload_headers: dict = {
'Accept': 'application/json',
'Authorization': f'Basic {api_key}',
'Content-Type': 'application/octet-stream'
}
with open(file_path, 'rb') as payload:
upload_response = requests.post(upload_url, headers=upload_headers, params=query_parameters, data=payload)
if not upload_response.ok:
print(f'Error on file upload: {upload_response.status_code} - {upload_response.json()}')
return
print(f'File uploaded successfully: {stream_id}')
# wait on stream status
status_url = f'https://stream-service.vatis.tech/stream-service/api/v1/streams/{stream_id}'
status_headers = {
'Accept': 'application/json',
'Authorization': f'Basic {api_key}',
}
while True:
status_response = requests.request('GET', status_url, headers=status_headers)
if not status_response.ok:
print(f'Error on stream status: {status_response.json()}')
return
if status_response.json()['state'] == 'COMPLETED':
break
elif status_response.json()['state'] == 'FAILED':
print(f'Error on stream: {status_response.json()}')
break
else:
sleep(3)
print(f'Waiting for stream to be completed: {stream_id}')
print(f'The stream is completed: {stream_id}')
# Export the results
export_url: str = f"https://export-service.vatis.tech/export-service/api/v1/export/JSON?streams={stream_id}"
export_headers: dict = {
'Authorization': f'Basic {api_key}',
'Accept': 'application/json'
}
export_response = requests.request('GET', export_url, headers=export_headers)
export_result = export_response.json()
if export_response.ok:
print(export_result['enhancedTranscription']['transcription']['text'])
else:
print(f'Error on export: {export_result}')
if __name__ == '__main__':
if len(sys.argv) > 1:
file_path = sys.argv[1]
else:
file_path = os.environ.get('FILE_PATH', '../data/stt/test-phone-call.wav')
file_path = Path(file_path).resolve()
assert file_path.exists() and file_path.is_file(), f'File {file_path} does not exist or is not a file'
api_key: str = os.environ.get('API_KEY')
stream_configuration_template_id: str = os.environ.get('CONFIGURATION_ID', '668115d123bca7e3509723d4')
transcribe(file_path=file_path,
api_key=api_key,
stream_configuration_template_id=stream_configuration_template_id)