Aichest.php
4.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Ktadmin\Tencent;
use think\facade\Db;
use app\base\model\BaseModel;
use think\facade\Cache;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Asr\V20190614\AsrClient;
use TencentCloud\Asr\V20190614\Models\CreateRecTaskRequest;
use TencentCloud\Asr\V20190614\Models\SentenceRecognitionRequest;
use TencentCloud\Asr\V20190614\Models\DescribeTaskStatusRequest;
use TencentCloud\Common\Profile\HttpProfile;
/**
* 腾讯 Ai百宝箱
**/
class Aichest
{
protected $secret_id;
protected $secret_key;
public $EngineModelType = '16k_zh';
public $EngSerViceType = '16k_zh';
public $ChannelNum = 1;
public $ResTextFormat = 0;
public $SourceType = 0; //0url 1数据
protected $CallbackUrl = '';
public $VoiceFormat = 'mp3';
public function __construct($secret_id,$secret_key){
$this->secret_id = $secret_id;
$this->secret_key = $secret_key;
}
/**
* 一句话识别
* @param string $url
* @return - http response body if succeeds, else false.
*/
public function SentenceRecognition($str)
{
$TENCENTCLOUD_SECRET_ID = $this->secret_id;
$TENCENTCLOUD_SECRET_KEY = $this->secret_key;
$cred = new Credential($TENCENTCLOUD_SECRET_ID, $TENCENTCLOUD_SECRET_KEY);
// 实例化一个http选项,可选的,没有特殊需求可以跳过
$httpProfile = new HttpProfile();
// $httpProfile->setEndpoint(); // 指定接入地域域名(默认就近接入)
// 实例化一个client选项,可选的,没有特殊需求可以跳过
$clientProfile = new ClientProfile();
$clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
$clientProfile->setHttpProfile($httpProfile);
$client = new AsrClient($cred, "ap-shanghai", $clientProfile);
$req = new SentenceRecognitionRequest();
$params = [
"EngSerViceType" => $this->EngSerViceType,
"VoiceFormat" => $this->VoiceFormat,
"SourceType" => $this->SourceType,
"SubServiceType" => 2,
"ProjectId" => 0,
"UsrAudioKey" => "test",
];
if($this->SourceType == 0) $params['Url'] = $str;
if($this->SourceType == 1) $params['Data'] = $str;
// $params = '{"EngSerViceType":"'.$this->EngSerViceType .'","VoiceFormat":'.$this->VoiceFormat.',"SourceType":' . $this->SourceType . ',"Url":" ","Data":"'.$str.'"}';
$req->fromJsonString(json_encode($params));
$resp = $client->SentenceRecognition($req);
return $resp;
}
/**
* 创建录音文件识别请求
* @param string $url
* @return - http response body if succeeds, else false.
*/
public function CreateRecTask($str = '')
{
$TENCENTCLOUD_SECRET_ID = $this->secret_id;
$TENCENTCLOUD_SECRET_KEY = $this->secret_key;
$cred = new Credential($TENCENTCLOUD_SECRET_ID, $TENCENTCLOUD_SECRET_KEY);
// 实例化一个http选项,可选的,没有特殊需求可以跳过
$httpProfile = new HttpProfile();
// $httpProfile->setEndpoint(); // 指定接入地域域名(默认就近接入)
// 实例化一个client选项,可选的,没有特殊需求可以跳过
$clientProfile = new ClientProfile();
$clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
$clientProfile->setHttpProfile($httpProfile);
$client = new AsrClient($cred, "ap-shanghai", $clientProfile);
$req = new CreateRecTaskRequest();
$params = [
"EngineModelType" => $this->EngineModelType,
"ChannelNum" => $this->ChannelNum,
"ResTextFormat" => $this->ResTextFormat,
"SourceType" => $this->SourceType,
];
if($this->SourceType == 0) $params['Url'] = $str;
if($this->SourceType == 1) $params['Data'] = $str;
// $params = '{"EngineModelType":"'.$this->EngineModelType.'","ChannelNum":'.$this->ChannelNum.',"ResTextFormat":'.$this->ResTextFormat.',"SourceType":' . $SourceType . ',"Url":"' . $url . '","CallbackUrl":"","Data":"'.$str.'"}';
$req->fromJsonString(json_encode($params));
$resp = $client->CreateRecTask($req);
return $resp;
}
/**
* 查看任务状态
* @param string $taskid
* @return - http response body if succeeds, else false.
*/
public function DescribeTaskStatus($taskid)
{
$TENCENTCLOUD_SECRET_ID = $this->secret_id;
$TENCENTCLOUD_SECRET_KEY = $this->secret_key;
$Endpoint = 'asr.tencentcloudapi.com';
$cred = '';
$cred = new Credential($TENCENTCLOUD_SECRET_ID, $TENCENTCLOUD_SECRET_KEY);
// 实例化一个http选项,可选的,没有特殊需求可以跳过
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint($Endpoint); // 指定接入地域域名(默认就近接入)
// 实例化一个client选项,可选的,没有特殊需求可以跳过
$clientProfile = new ClientProfile();
$clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
$clientProfile->setHttpProfile($httpProfile);
$client = new AsrClient($cred, "ap-shanghai", $clientProfile);
$req = new DescribeTaskStatusRequest();
$params = '{"TaskId":'.$taskid.'}';
$req->fromJsonString($params);
$resp = $client->DescribeTaskStatus($req);
return $resp;
}
}