File size: 1,906 Bytes
7661f93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import base64
import json
import os
import sys

pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../../"))

import numpy as np
import requests


class AgentXVoice(object):
    """
    distil-whisper/distil-large-v3
    """
    def __init__(self,
                 api_key: str,
                 url_host: str = "https://api.agentx.so"
                 ):
        self.api_key = api_key
        self.url_host = url_host

    def do_asr_by_ndarray(self, signal: np.ndarray, language: str):
        pcm_signal = signal.tobytes()
        pcm_in_base64 = base64.b64encode(pcm_signal).decode("utf-8")
        return self.do_asr_by_base64(pcm_in_base64, language)

    def do_asr_by_file(self, filename: str, language: str):
        with open(filename, "rb") as f:
            data = f.read()

        base64string = base64.b64encode(data).decode("utf-8")

        return self.do_asr_by_base64(base64string, language)

    def do_asr_by_base64(self, signal_base64: str, language: str):
        url = "{}/api/v1/voiceApi/asr/recognize/do_asr".format(self.url_host)

        headers = {
            "Content-type": "application/json",
            "x-api-key": self.api_key
        }
        data = {
            "callID": "string",
            "seqID": "string",
            "callbackUrl": "string",
            "sampleInBase64": signal_base64,
            "sampleDuration": "string",
            "language": language,
            "other": "string"
        }
        resp = requests.request(
            "POST",
            url=url,
            headers=headers,
            data=json.dumps(data)
        )
        if resp.status_code != 200:
            raise AssertionError("status_code: {}, text: {}".format(resp.status_code, resp.text))

        js = resp.json()
        return js


if __name__ == '__main__':
    pass