From a8dba48589cccf93aac5359cc72c3e823fb83124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=82=A5=E7=BE=8A?= <1048382248@qq.com> Date: Fri, 15 Nov 2024 11:48:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20float=20=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?double=20=E4=BC=98=E5=8C=96=20taskinfo=20api=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E4=B8=8D=E5=AF=BC=E5=87=BA=E5=AD=97=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Pages/VideoTaskPage.razor.cs | 2 +- VideoAnalysis/Controllers/ApiController.cs | 30 +++++++++++++++---- VideoAnalysis/Properties/launchSettings.json | 2 +- VideoAnalysisCore/AICore/ChatGPT/BserGPT.cs | 2 +- .../AICore/ChatGPT/Dto/CallGPTRes.cs | 17 +++++++++-- .../AICore/ChatGPT/KIMI/KIMI_GPT.cs | 6 ++-- .../AICore/ChatGPT/KIMI/MoonshotModel.cs | 2 +- .../AICore/SherpaOnnx/SenseVoice.cs | 12 ++++---- .../AICore/SherpaOnnx/Speaker.cs | 10 +++---- VideoAnalysisCore/Common/RedisExpand.cs | 6 ++-- .../Model/Dto/SpeakerCaptionsDto.cs | 12 ++++---- VideoAnalysisCore/Model/Dto/VideoTaskDto.cs | 2 +- VideoAnalysisCore/Model/VideoTask.cs | 4 +-- 13 files changed, 68 insertions(+), 39 deletions(-) diff --git a/VideoAnalysis/Components/Pages/VideoTaskPage.razor.cs b/VideoAnalysis/Components/Pages/VideoTaskPage.razor.cs index c8df664..af79c30 100644 --- a/VideoAnalysis/Components/Pages/VideoTaskPage.razor.cs +++ b/VideoAnalysis/Components/Pages/VideoTaskPage.razor.cs @@ -90,7 +90,7 @@ namespace Learn.VideoAnalysis.Components.Pages return; var data = RedisExpand.Redis.HMGet(RedisExpandKey.Task(item.Id), "Progress", "LastEnum", "StartTime", "ErrorMessage"); - item.Progress = double.Parse(data[0]); + item.Progress = float.Parse(data[0]); item.LastEnum = data[1].ToEnum() ?? default; item.StartTimeDic = System.Text.Json.JsonSerializer.Deserialize>(data[2]) ?? null; item.ErrorMessage = data[3]; diff --git a/VideoAnalysis/Controllers/ApiController.cs b/VideoAnalysis/Controllers/ApiController.cs index 15060a4..551526e 100644 --- a/VideoAnalysis/Controllers/ApiController.cs +++ b/VideoAnalysis/Controllers/ApiController.cs @@ -10,6 +10,7 @@ using VideoAnalysisCore.Model.Dto; using VideoAnalysisCore.AICore.ChatGPT.Dto; using AntDesign; using System.Threading.Tasks; +using FFmpeg.NET.Services; namespace Learn.VideoAnalysis.Controllers { @@ -41,19 +42,36 @@ namespace Learn.VideoAnalysis.Controllers /// /// /// 自定义id + /// 是否附加字幕 /// [HttpGet(Name = "TaskInfo")] - public async Task TaskInfo(long taskId,string? tagId) + public async Task TaskInfo(long taskId,string? tagId,bool needSubtitle=false) { var task = await videoTaskDB.AsQueryable() .WhereIF(taskId!=0, s => s.Id == taskId) - .WhereIF(string.IsNullOrEmpty(tagId), s => s.TagId == tagId) + .WhereIF(!string.IsNullOrEmpty(tagId), s => s.TagId == tagId) .FirstAsync(); - if(task is null) + if (task is null) return BadRequest(); + var taskData = task.ChatAnalysis; if (task.LastEnum != RedisChannelEnum.EndTask) - return BadRequest(new { Enum = task.LastEnum ,Task = task.ChatAnalysis}); - return Ok(new { Enum = task.LastEnum, Task = task.ChatAnalysis }); + return BadRequest(new + { + Enum = task.LastEnum , + Task = taskData + }); + if (!needSubtitle && taskData != null && taskData.TimeBase != null) + { + foreach (var item in taskData.TimeBase) + { + item.Content = null; + } + } + return Ok(new + { + Enum = task.LastEnum, + Task = taskData + }); } /// @@ -63,7 +81,7 @@ namespace Learn.VideoAnalysis.Controllers /// /// [HttpPost(Name = "TestInsertChannel")] - public IActionResult TestInsertChannel(int @enum=1, string msg= "test_0001") + public IActionResult TestInsertChannel(int @enum=1, string msg= "1") { RedisExpand.InsertChannel(@enum.ToEnum().Value , msg); diff --git a/VideoAnalysis/Properties/launchSettings.json b/VideoAnalysis/Properties/launchSettings.json index 12a2a37..8e2ec82 100644 --- a/VideoAnalysis/Properties/launchSettings.json +++ b/VideoAnalysis/Properties/launchSettings.json @@ -14,7 +14,7 @@ "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "", - "applicationUrl": "http://localhost:5238", + "applicationUrl": "http://*:5238", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/VideoAnalysisCore/AICore/ChatGPT/BserGPT.cs b/VideoAnalysisCore/AICore/ChatGPT/BserGPT.cs index 9dcea2a..7bfb2ea 100644 --- a/VideoAnalysisCore/AICore/ChatGPT/BserGPT.cs +++ b/VideoAnalysisCore/AICore/ChatGPT/BserGPT.cs @@ -15,6 +15,6 @@ namespace VideoAnalysisCore.AICore.ChatGPT /// /// 浠诲姟id /// - public Task CallGPT(string task); + public Task CallGPT(string task); } } diff --git a/VideoAnalysisCore/AICore/ChatGPT/Dto/CallGPTRes.cs b/VideoAnalysisCore/AICore/ChatGPT/Dto/CallGPTRes.cs index 0d40e7d..04ceff7 100644 --- a/VideoAnalysisCore/AICore/ChatGPT/Dto/CallGPTRes.cs +++ b/VideoAnalysisCore/AICore/ChatGPT/Dto/CallGPTRes.cs @@ -9,12 +9,15 @@ using VideoAnalysisCore.Model.Dto; namespace VideoAnalysisCore.AICore.ChatGPT.Dto { - public class CallGPTRes + /// + /// 浠诲姟缁撴灉 + /// + public class TaskRes { - public CallGPTRes() + public TaskRes() { } - public CallGPTRes(TotalCaptionsDto captions) + public TaskRes(TotalCaptionsDto captions) { this.TeacherSpeaking = captions.TeacherSpeaking; this.TimeBase = captions.TimeBase; @@ -55,5 +58,13 @@ namespace VideoAnalysisCore.AICore.ChatGPT.Dto /// AI缁煎悎璇勪及 /// public AssessmentDto Assessment { get; set; } = default!; + /// + /// AI缁煎悎璇勪及寰楀垎 + /// 婊″垎100 + /// + public float AssessmentScore => + (float)(Assessment?.Bad?.Select(x => x.Score) + .Concat(Assessment?.Merit?.Select(s => s.Score) ?? []) + .Average() ?? 0) * 100; } } diff --git a/VideoAnalysisCore/AICore/ChatGPT/KIMI/KIMI_GPT.cs b/VideoAnalysisCore/AICore/ChatGPT/KIMI/KIMI_GPT.cs index d3dcc87..50164f3 100644 --- a/VideoAnalysisCore/AICore/ChatGPT/KIMI/KIMI_GPT.cs +++ b/VideoAnalysisCore/AICore/ChatGPT/KIMI/KIMI_GPT.cs @@ -47,7 +47,7 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI /// /// 浠诲姟id /// - public async Task CallGPT(string task) + public async Task CallGPT(string task) { var captions = ExpandFunction.GetSpeakerCaptions(task); var criteriaArr = await criteriaDB.GetListAsync(); @@ -87,7 +87,7 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI var chatRep = new ChatReq { max_tokens = reqTokenCount * 2, - temperature = 0.3, + temperature = 0.3f, frequency_penalty = 0, presence_penalty = 0, model = modelId, @@ -108,7 +108,7 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI throw new Exception("KIMIGPT杩斿洖message鏃犳晥缁撴灉"); var questionRes = JsonConvert.DeserializeObject(chatResContent); - var gptRes = new CallGPTRes(captions); + var gptRes = new TaskRes(captions); if (questionRes is null) throw new Exception("KIMIGPT杩斿洖鏃犳晥缁撴灉"); //澶勭悊 ai闂瓟鎻愰棶 diff --git a/VideoAnalysisCore/AICore/ChatGPT/KIMI/MoonshotModel.cs b/VideoAnalysisCore/AICore/ChatGPT/KIMI/MoonshotModel.cs index 024d224..b36f9f5 100644 --- a/VideoAnalysisCore/AICore/ChatGPT/KIMI/MoonshotModel.cs +++ b/VideoAnalysisCore/AICore/ChatGPT/KIMI/MoonshotModel.cs @@ -41,7 +41,7 @@ /// 浣跨敤浠涔堥噰鏍锋俯搴︼紝浠嬩簬 0 鍜 1 涔嬮棿銆傝緝楂樼殑鍊硷紙濡 0.7锛夊皢浣胯緭鍑烘洿鍔犻殢鏈猴紝鑰岃緝浣庣殑鍊硷紙濡 0.2锛夊皢浣垮叾鏇村姞闆嗕腑鍜岀‘瀹氭 /// 榛樿涓 0锛屽鏋滆缃紝鍊煎煙椤讳负 [0, 1] 鎴戜滑鎺ㄨ崘 0.3锛屼互杈惧埌杈冨悎閫傜殑鏁堟灉 /// - public double temperature { get; set; } + public float temperature { get; set; } /// /// 鑱婂ぉ瀹屾垚鏃剁敓鎴愮殑鏈澶 token 鏁般傚鏋滃埌鐢熸垚浜嗘渶澶 token 鏁颁釜缁撴灉浠嶇劧娌℃湁缁撴潫锛宖inish reason 浼氭槸 "length", 鍚﹀垯浼氭槸 "stop" diff --git a/VideoAnalysisCore/AICore/SherpaOnnx/SenseVoice.cs b/VideoAnalysisCore/AICore/SherpaOnnx/SenseVoice.cs index 49166de..a6842af 100644 --- a/VideoAnalysisCore/AICore/SherpaOnnx/SenseVoice.cs +++ b/VideoAnalysisCore/AICore/SherpaOnnx/SenseVoice.cs @@ -89,7 +89,7 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx int windowSize = VADModelConfig.SileroVad.WindowSize; int sampleRate = VADModelConfig.SampleRate; int numIter = numSamples / windowSize; - var totalSecond = numSamples / (double)sampleRate; + var totalSecond = numSamples / (float)sampleRate; var res = new List(500); for (int i = 0; i != numIter; ++i) { @@ -114,10 +114,10 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx res.Add(new() { Text = stream.Result.Text, - Start= startTime, - End = startTime + duration + Start= (float)Math.Round(startTime, 2, MidpointRounding.AwayFromZero), + End = (float)Math.Round(startTime + duration, 2, MidpointRounding.AwayFromZero), }); - var progress = (double)(startTime + duration) / (totalSecond) * 100; + var progress = (float)(startTime + duration) / (totalSecond) * 100; RedisExpand.SetTaskProgress(task, progress); } @@ -141,8 +141,8 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx res.Add(new() { Text = stream.Result.Text, - Start = startTime, - End = startTime + duration + Start = (float)Math.Round(startTime, 2, MidpointRounding.AwayFromZero), + End = (float)Math.Round(startTime + duration, 2, MidpointRounding.AwayFromZero), }); } diff --git a/VideoAnalysisCore/AICore/SherpaOnnx/Speaker.cs b/VideoAnalysisCore/AICore/SherpaOnnx/Speaker.cs index 535b10f..1a8151c 100644 --- a/VideoAnalysisCore/AICore/SherpaOnnx/Speaker.cs +++ b/VideoAnalysisCore/AICore/SherpaOnnx/Speaker.cs @@ -21,7 +21,7 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx /// /// /// - public static void Init(int speakerNumber = 2, double threshold = 0.6,bool useGPU = false) + public static void Init(int speakerNumber = 2, float threshold = 0.6f,bool useGPU = false) { Console.WriteLine("鍒濆鍖 Speaker"); var config = new OfflineSpeakerDiarizationConfig(); @@ -36,7 +36,7 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx if (!useGPU) config.Embedding.Provider = "cuda"; //璇磋瘽浜哄垽瀹氶槇鍊 - config.Clustering.Threshold = (float)threshold; + config.Clustering.Threshold = threshold; SD = new OfflineSpeakerDiarization(config); } /// @@ -62,7 +62,7 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx i++; if(i%20 !=0) return 1; - var progress = (double)numProcessedChunks / numTotalChunks * 100; + var progress = (float)numProcessedChunks / numTotalChunks * 100; RedisExpand.SetTaskProgress(task, progress); return 1; }, nint.Zero); @@ -109,8 +109,8 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx /// public OfflineSpeakerRes(OfflineSpeakerDiarizationSegment sds) { - Start = sds.Start; - End =sds.End; + Start = (float)Math.Round(sds.Start, 2, MidpointRounding.AwayFromZero); + End = (float)Math.Round(sds.End, 2, MidpointRounding.AwayFromZero); SpeakerIndex = sds.Speaker; } } diff --git a/VideoAnalysisCore/Common/RedisExpand.cs b/VideoAnalysisCore/Common/RedisExpand.cs index 5455e83..b9d4f65 100644 --- a/VideoAnalysisCore/Common/RedisExpand.cs +++ b/VideoAnalysisCore/Common/RedisExpand.cs @@ -107,9 +107,9 @@ namespace VideoAnalysisCore.Common /// 鑾峰彇浠诲姟杩涘害 /// /// - public static double SetTaskProgress(object taskId) + public static float GetTaskProgress(object taskId) { - return Redis.HMGet(RedisExpandKey.Task(taskId), "Progress")[0]; + return Redis.HMGet(RedisExpandKey.Task(taskId), "Progress")[0]; } /// /// 璁剧疆浠诲姟杩涘害 @@ -151,7 +151,7 @@ namespace VideoAnalysisCore.Common { var tId = long.Parse(task); var gptRes = (await RedisExpand.Redis - .HMGetAsync(RedisExpandKey.Task(task), "ChatAnalysis")).FirstOrDefault(); + .HMGetAsync(RedisExpandKey.Task(task), "ChatAnalysis")).FirstOrDefault(); if (gptRes is null) throw new Exception("鏈兘璇诲彇鍒癎PT澶勭悊缁撴灉"); diff --git a/VideoAnalysisCore/Model/Dto/SpeakerCaptionsDto.cs b/VideoAnalysisCore/Model/Dto/SpeakerCaptionsDto.cs index c46e933..b1c15d7 100644 --- a/VideoAnalysisCore/Model/Dto/SpeakerCaptionsDto.cs +++ b/VideoAnalysisCore/Model/Dto/SpeakerCaptionsDto.cs @@ -21,8 +21,8 @@ namespace VideoAnalysisCore.Model.Dto var arr = str.Split(":"); if (arr !=null && arr.Length >= 3) { - Start = double.Parse(arr[1]); - End = double.Parse(arr[2]); + Start = float.Parse(arr[1]); + End = float.Parse(arr[2]); } } public TimeBase() @@ -32,18 +32,18 @@ namespace VideoAnalysisCore.Model.Dto /// /// 寮濮嬫椂闂 /// - public double Start { get; set; } + public float Start { get; set; } /// /// 缁撴潫鏃堕棿 /// - public double End { get; set; } + public float End { get; set; } /// /// 鍐呭 /// - public string Content { get; set; } + public string? Content { get; set; } /// /// 鏃堕棿娈 绫诲瀷 - /// 鏃朵负 null + /// 鏃朵负 null /// public TimeBaseTypeEnum? TimeBaseType { get; set; } diff --git a/VideoAnalysisCore/Model/Dto/VideoTaskDto.cs b/VideoAnalysisCore/Model/Dto/VideoTaskDto.cs index 3503180..dc11ca2 100644 --- a/VideoAnalysisCore/Model/Dto/VideoTaskDto.cs +++ b/VideoAnalysisCore/Model/Dto/VideoTaskDto.cs @@ -38,7 +38,7 @@ namespace VideoAnalysisCore.Model.Dto /// 鎵ц杩涘害 /// [DisplayName("杩涘害")] - public double Progress { get; set; } + public float Progress { get; set; } /// /// 閿欒淇℃伅 /// diff --git a/VideoAnalysisCore/Model/VideoTask.cs b/VideoAnalysisCore/Model/VideoTask.cs index 5e8c71f..262981b 100644 --- a/VideoAnalysisCore/Model/VideoTask.cs +++ b/VideoAnalysisCore/Model/VideoTask.cs @@ -89,9 +89,9 @@ namespace VideoAnalysisCore.Model /// Chat妯″瀷鍒嗘瀽缂撳瓨 /// [SugarColumn(IsIgnore = true)] - public CallGPTRes? ChatAnalysis + public TaskRes? ChatAnalysis { - get => JsonSerializer.Deserialize(_ChatAnalysis ?? "{}"); + get => JsonSerializer.Deserialize(_ChatAnalysis ?? "{}"); set => _ChatAnalysis = JsonSerializer.Serialize(value); } ///