warr.qian 2 жил өмнө
parent
commit
b83b1a2133

+ 32 - 0
Service/ASRService.cs

@@ -0,0 +1,32 @@
+using WingASRService.Tencent;
+using WingInterfaceLibrary.DTO.ASR;
+using WingInterfaceLibrary.Enum;
+using WingInterfaceLibrary.Interface;
+using WingInterfaceLibrary.Request.ASR;
+using WingServerCommon.Service;
+
+namespace WingASRService.Service
+{
+    public class ASRService : JsonRpcService, IASRService
+    {
+        private void ThrowCustomerException(CustomerRpcCode customerRpcCodeEnum, string msg)
+        {
+            string customerErrorMsg = Enum.GetName(customerRpcCodeEnum) ?? string.Empty;
+            ThrowRpcException((int)customerRpcCodeEnum, msg, customerErrorMsg);
+        }
+
+        public async Task<ASRResultDTO> CommitASRInfoAsync(CommitASRInfoRequest request)
+        {
+            var result = ASRApi.CallASRApi(request.Url, request.FileType);
+            if (!result.isSuccess)
+            {
+                ThrowCustomerException(CustomerRpcCode.ASRError, result.message);
+            }
+            return await Task.FromResult(new ASRResultDTO
+            {
+                IsComplete = result.isSuccess,
+                Content = result.message,
+            });
+        }
+    }
+}

+ 70 - 0
Tencent/ASRApi.cs

@@ -0,0 +1,70 @@
+using System.Linq;
+using System.Text.RegularExpressions;
+using Newtonsoft.Json.Linq;
+using TencentCloud.Asr.V20190614;
+using TencentCloud.Asr.V20190614.Models;
+using TencentCloud.Common;
+using TencentCloud.Common.Profile;
+using WingServerCommon.Log;
+
+namespace WingASRService.Tencent
+{
+    public class ASRApi
+    {
+        private ASRApi(){}
+        
+        public static (string message, bool isSuccess) CallASRApi(string url, string fileType)
+        {
+            try
+            {
+                // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
+                // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
+                Credential cred = new Credential {
+                    SecretId = "AKIDgDUVFbjbDahYAXeV6RXNxHK8gy0mJabN",
+                    SecretKey = "XprdARRBgusa3GEwObYFgrmRq0uYv3vY"
+                };
+                // Credential cred = new Credential {
+                //     SecretId = "AKIDMSIsADUUpvJGwJOKvsb5IDrGtGNBRzzi",
+                //     SecretKey = "4sQhdJBl5HI7f3oLbZIlwqXOyILyLWP0"
+                // };
+                // 实例化一个client选项,可选的,没有特殊需求可以跳过
+                ClientProfile clientProfile = new ClientProfile();
+                // 实例化一个http选项,可选的,没有特殊需求可以跳过
+                HttpProfile httpProfile = new HttpProfile();
+                httpProfile.Endpoint = ("asr.tencentcloudapi.com");
+                clientProfile.HttpProfile = httpProfile;
+
+                // 实例化要请求产品的client对象,clientProfile是可选的
+                AsrClient client = new AsrClient(cred, "", clientProfile);
+                // 实例化一个请求对象,每个接口都会对应一个request对象
+                SentenceRecognitionRequest req = new SentenceRecognitionRequest();
+                req.ProjectId = 0;
+                req.SubServiceType = 2;
+                req.EngSerViceType = "16k_zh_medical";
+                req.SourceType = 0;
+                req.Url = url;
+                req.VoiceFormat = fileType;
+                req.UsrAudioKey = "any";
+                // 返回的resp是一个SentenceRecognitionResponse的实例,与请求对象对应
+                var resp = client.SentenceRecognitionSync(req);
+                return (message: resp.Result, isSuccess: true);
+            }
+            catch (Exception e)
+            {
+                var regMatch = Regex.Match(e.Message,@".*code:([.\w]+)[\s]+.*");
+                if(regMatch.Groups.Count>1)
+                {
+                    var errorCode = regMatch.Groups[1].Value;
+                    if(!string.IsNullOrWhiteSpace(errorCode) && ASRErrorCode.ErrorsDictionary.Keys.Contains(errorCode))
+                    {
+                        var errorMessage = ASRErrorCode.ErrorsDictionary[errorCode];
+                        Logger.WriteLineError($"ASRApi.CallASRApi code:{errorCode} message:{errorMessage}");
+                        return (message: errorMessage, isSuccess: false);
+                    }
+                }
+                Logger.WriteLineError($"ASRApi.CallASRApi error {e}");
+                return (message: "Unknow error", isSuccess: false);;
+            }
+        }
+    }
+}

+ 40 - 0
Tencent/ASRErrorCode.cs

@@ -0,0 +1,40 @@
+namespace WingASRService.Tencent
+{
+    public class ASRErrorCode
+    {
+        private ASRErrorCode(){}
+        public static readonly Dictionary<string, string> ErrorsDictionary = new Dictionary<string, string>()
+        {
+            {"FailedOperation.ErrorRecognize",  "识别失败。"},
+            {"FailedOperation.ServiceIsolate",  "账号因为欠费停止服务,请在腾讯云账户充值。"},
+            {"FailedOperation.UserHasNoFreeAmount", "账号本月免费额度已用完。"},
+            {"FailedOperation.UserNotRegistered",   "服务未开通,请在腾讯云官网语音识别控制台开通服务。"},
+            {"InternalError",   "内部错误。"},
+            {"InternalError.ErrorConfigure",    "初始化配置失败。"},
+            {"InternalError.ErrorCreateLog",    "创建日志失败。"},
+            {"InternalError.ErrorDownFile", "下载音频文件失败。"},
+            {"InternalError.ErrorFailNewprequest",  "新建数组失败。"},
+            {"InternalError.ErrorFailWritetodb",    "写入数据库失败。"},
+            {"InternalError.ErrorFileCannotopen",   "文件无法打开。"},
+            {"InternalError.ErrorGetRoute", "获取路由失败。"},
+            {"InternalError.ErrorMakeLogpath",  "创建日志路径失败。"},
+            {"InternalError.ErrorRecognize",    "识别失败。"},
+            {"InvalidParameter.ErrorContentlength", "请求数据长度无效。"},
+            {"InvalidParameter.ErrorParamsMissing", "参数不全。"},
+            {"InvalidParameter.ErrorParsequest",    "解析请求数据失败。"},
+            {"InvalidParameterValue",   "参数取值错误。"},
+            {"InvalidParameterValue.ErrorInvalidAppid", "AppId无效。"},
+            {"InvalidParameterValue.ErrorInvalidClientip",  "ClientIp无效。"},
+            {"InvalidParameterValue.ErrorInvalidEngservice",    "EngSerViceType无效。"},
+            {"InvalidParameterValue.ErrorInvalidProjectid", "ProjectId无效。"},
+            {"InvalidParameterValue.ErrorInvalidRequestid", "RequestId无效。"},
+            {"InvalidParameterValue.ErrorInvalidSourcetype",    "SourceType无效。"},
+            {"InvalidParameterValue.ErrorInvalidSubservicetype",    "SubserviceType无效。"},
+            {"InvalidParameterValue.ErrorInvalidUrl",   "Url无效。"},
+            {"InvalidParameterValue.ErrorInvalidUseraudiokey",  "UsrAudioKey无效。"},
+            {"InvalidParameterValue.ErrorInvalidVoiceFormat",   "音频编码格式不支持。"},
+            {"InvalidParameterValue.ErrorInvalidVoicedata", "音频数据无效。"},
+            {"InvalidParameterValue.ErrorVoicedataTooLong", "音频时长超过限制。"}
+        };
+    }
+}

+ 15 - 0
WingASRService.csproj

@@ -0,0 +1,15 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net6.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="TencentCloudSDK" Version="3.0.665" />
+    <PackageReference Include="WingInterfaceLibrary" Version="1.1.2.1" />
+    <PackageReference Include="WingServerCommon" Version="1.1.2.1" />
+  </ItemGroup>
+
+</Project>