parent
73d4fbd8c1
commit
d12839be20
|
|
@ -101,7 +101,7 @@ namespace VideoAnalysisCore.AICore.FFMPGE
|
|||
keyFrames[i] = -1;
|
||||
}
|
||||
//写入数据库
|
||||
var keyFramStr = JsonSerializer.Serialize(keyFrames.Where(s=>s!=-1));
|
||||
var keyFramStr = keyFrames.Where(s => s != -1).ToJson();
|
||||
await DbScoped.Sugar
|
||||
.Updateable<VideoTask>()
|
||||
.SetColumns(it => it.PPTKeyFrame == keyFramStr)
|
||||
|
|
|
|||
|
|
@ -32,47 +32,6 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
|
|||
public async Task<(Usage u, string res)?> ChatSSE(ChatRequest chatReq)
|
||||
{
|
||||
throw new Exception($"未实现");
|
||||
//chatReq.stream = true;
|
||||
var requestBody = System.Text.Json.JsonSerializer.Serialize(chatReq);
|
||||
var chatResp = await PostJsonStreamAsync("/v1/chat/completions", requestBody);
|
||||
using var stream = await chatResp.Content.ReadAsStreamAsync();
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||
string line;
|
||||
StringBuilder messageBuilder = new StringBuilder();
|
||||
ChatRes lastChat = new ChatRes();
|
||||
|
||||
//while ((line = await reader.ReadLineAsync()) != null)
|
||||
//{
|
||||
// if (line.EndsWith("[DONE]"))
|
||||
// {
|
||||
// // 表示一条消息结束
|
||||
// string message = messageBuilder.ToString();
|
||||
// messageBuilder.Clear();
|
||||
// var u = lastChat?.choices?.FirstOrDefault()?.usage;
|
||||
// if (u == null || string.IsNullOrEmpty(message))
|
||||
// return null;
|
||||
// return (u, message);
|
||||
// }
|
||||
// else if (line.StartsWith("data:"))
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// var data = System.Text.Json.JsonSerializer.Deserialize<ChatRes>(line.Substring("data:".Length).Trim());
|
||||
// lastChat = data;
|
||||
// var str = data?.choices.FirstOrDefault()?.delta.content;
|
||||
// if (!string.IsNullOrEmpty(str))
|
||||
// messageBuilder.Append(str);
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// Console.WriteLine("异常 ChatSSE=>");
|
||||
// Console.WriteLine(line);
|
||||
// Console.WriteLine(e.Message);
|
||||
// Console.WriteLine(e.StackTrace);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -82,14 +41,14 @@ namespace VideoAnalysisCore.AICore.GPT.ChatGPT
|
|||
/// <returns>Return HttpResponseMessage for SSE</returns>
|
||||
public async Task<(Usage u, string res)?> Chat(ChatRequest chatReq)
|
||||
{
|
||||
var requestBody = System.Text.Json.JsonSerializer.Serialize(chatReq);
|
||||
var requestBody = chatReq.ToJson();
|
||||
var chatResp = await PostJsonStreamAsync("v1/chat/completions", requestBody);
|
||||
var res = await chatResp.Content.ReadFromJsonAsync<ChatRes>();
|
||||
var chatResContent = res?.choices.FirstOrDefault()?.message.content.Trim();
|
||||
|
||||
if (res is null || res.error != null)
|
||||
throw new Exception($" ChatGPT模型返回异常 返回参数: " +
|
||||
$" {System.Text.Json.JsonSerializer.Serialize(res)}");
|
||||
$" {res?.ToJson()}");
|
||||
|
||||
if (string.IsNullOrEmpty(chatResContent))
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
//chatReq.model = "deepseek-r1";
|
||||
if (chatReq.stream) return await ChatSSE(chatReq);
|
||||
postStar:
|
||||
var requestBody = System.Text.Json.JsonSerializer.Serialize(chatReq);
|
||||
var requestBody = chatReq.ToJson();
|
||||
HttpResponseMessage chatResp = PostJsonStream(Path, requestBody);
|
||||
var res1 = await chatResp.Content.ReadAsStringAsync();
|
||||
if (res1 == null || string.IsNullOrEmpty(res1)|| !chatResp.IsSuccessStatusCode)
|
||||
|
|
@ -65,7 +65,7 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
var res = await chatResp.Content.ReadFromJsonAsync<ChatRes>();
|
||||
if (res is null || res.error != null)
|
||||
throw new Exception($" GPT模型返回异常 返回参数: " +
|
||||
$" {System.Text.Json.JsonSerializer.Serialize(res)}");
|
||||
$" {res.ToJson()}");
|
||||
var d = thinkMSG(res?.choices.FirstOrDefault()?.message);
|
||||
var chatResContent = d.m1;
|
||||
var chatResReasoning = d.m2;
|
||||
|
|
@ -136,7 +136,7 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
/// <returns>Return HttpResponseMessage for SSE</returns>
|
||||
public async Task<(Usage u, string res, string reasoning)?> ChatSSE(ChatRequest chatReq)
|
||||
{
|
||||
var requestBody = System.Text.Json.JsonSerializer.Serialize(chatReq);
|
||||
var requestBody = chatReq.ToJson();
|
||||
PostJsonStream:
|
||||
var chatResp = PostJsonStream(string.Empty, requestBody);
|
||||
if (!chatResp.IsSuccessStatusCode)
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
/// 获取内容对应的章节
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task<List<VideoKonwPoint>> GetVideoKnow(List<VideoKnowRes> questionRes, VideoTask taskInfo,
|
||||
private async Task<List<VideoKonwPoint>> GetVideoKnow(VideoKnowRes[] questionRes, VideoTask taskInfo,
|
||||
string sections, List<KnowledgeInfo> knowledgeInfos)
|
||||
{
|
||||
var knows = string.Join(',', knowledgeInfos.Select(s => s.Id + "|" + s.Name));
|
||||
|
|
@ -67,8 +67,8 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
.GroupBy(s => s.Name)
|
||||
.ToDictionary(s => s.First().Name, s => s.First().Id);
|
||||
questionRes = questionRes.Where(s => s != null)
|
||||
.OrderBy(s => s.StartTime).ToList();
|
||||
var thems = JsonSerializer.Serialize(questionRes.Adapt<VideoKnowQueryDto[]>());// string.Join(',', questionRes.Select(s => s.StartTime + "->" + s.Theme));
|
||||
.OrderBy(s => s.StartTime).ToArray();
|
||||
var thems = questionRes.Adapt<VideoKnowQueryDto[]>().ToJson();
|
||||
var checkResFormat1 = """[{"StartTime":开始秒(number),"KnowPoint":知识点名称(string),"KnowPointId":知识点Id(string)}]""";
|
||||
var knowMessages =
|
||||
$"我针对{taskInfo.Subject}课堂授课视频分析出了视频的授课阶段片段。" +
|
||||
|
|
@ -160,10 +160,10 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
/// 检查AI切片结果质量
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task<CheckMessageDto> VerifySpanQuality(List<VideoKnowRes> questionRes, VideoTask taskInfo, TotalCaptionsDto captions, string sections, int course_Id)
|
||||
private async Task<CheckMessageDto> VerifySpanQuality(VideoKnowRes[] questionRes, VideoTask taskInfo, TotalCaptionsDto captions, string sections, int course_Id)
|
||||
{
|
||||
//校验结果质量
|
||||
var thems = JsonSerializer.Serialize(questionRes.Adapt<VideoKnowQueryDto[]>());
|
||||
var thems = questionRes.Adapt<VideoKnowQueryDto[]>().ToJson();
|
||||
var pptFormat = taskInfo.VideoType == AttachmentsInfoType.复习
|
||||
? "这堂课是习题课,所讲解内容都是试题。"
|
||||
: string.Empty;
|
||||
|
|
@ -206,7 +206,7 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
.Take(spanCount);
|
||||
if (cArr.Count() == 0)
|
||||
return;
|
||||
var nowCaptionStr =JsonSerializer.Serialize(cArr.Select(s =>s.Text));
|
||||
var nowCaptionStr = cArr.Select(s => s.Text).ToJson();
|
||||
var resFormat = """[string(修改结果)]""";
|
||||
var postMessages =
|
||||
$"这是一堂中国{subject}课堂的字幕,由结果是语音识别提供。" +
|
||||
|
|
@ -219,11 +219,11 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
$"`{nowCaptionStr}`" +
|
||||
$"字幕结束。" +
|
||||
$"最后请确保输出字幕条数与输入字幕条数一致!!! 如果不一致则重新优化并且确保字幕条数一致!!!!";
|
||||
Console.WriteLine(DateTime.Now + $"=>字幕优化 分段{s}/{totalCount}开始...");
|
||||
var resData = await ChatAsync<string[]>(taskInfo.Id.ToString(), postMessages, "优化字幕", "deepseek-chat");
|
||||
Console.WriteLine(DateTime.Now + $"=>{taskInfo.Id}字幕优化 分段{s}开始...");
|
||||
var resData = await ChatAsync<string[]>(taskInfo.Id.ToString(), postMessages, "优化字幕", "deepseek-chat", 3000);
|
||||
if (resData.Count() != cArr.Count())
|
||||
{
|
||||
Console.WriteLine(DateTime.Now + $"=>字幕优化 分段{s}/{totalCount} AI结果数量不匹配,重试");
|
||||
Console.WriteLine(DateTime.Now + $"=>{taskInfo.Id}字幕优化 分段{s} AI结果数量不匹配,重试×");
|
||||
continue;
|
||||
}
|
||||
newCaptionsList.AddRange(resData.Select((text, i) => new SenseVoiceRes()
|
||||
|
|
@ -232,13 +232,13 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
End = captionsArr[spanCount * s + i].End,
|
||||
Text = text,
|
||||
}));
|
||||
Console.WriteLine(DateTime.Now + $"=>字幕优化 分段{s}/{totalCount}完成√");
|
||||
Console.WriteLine(DateTime.Now + $"=>{taskInfo.Id}字幕优化 分段{s}完成√ ");
|
||||
return;
|
||||
}
|
||||
});
|
||||
var res = newCaptionsList.OrderBy(s => s.Start).ToArray();
|
||||
Console.WriteLine(DateTime.Now + $"=>字幕优化执行完成");
|
||||
var jsonData = JsonSerializer.Serialize(res);
|
||||
var jsonData = res.ToJson();
|
||||
await videoTaskDB.AsUpdateable()
|
||||
.SetColumns(it => it.CaptionsAI == jsonData)
|
||||
.Where(it => it.Id == taskInfo.Id)
|
||||
|
|
@ -250,7 +250,7 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
/// 视频AI分析字幕
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task Analytics(List<VideoKnowRes> questionRes, VideoTask taskInfo,
|
||||
private async Task<VideoKnowRes[]> Analytics(VideoTask taskInfo,
|
||||
TotalCaptionsDto captions, string sections)
|
||||
{
|
||||
var tryCount = 10;
|
||||
|
|
@ -300,15 +300,12 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
break;
|
||||
case AttachmentsInfoType.活动:
|
||||
case AttachmentsInfoType.班会:
|
||||
throw new Exception("无效的课程类型");
|
||||
default:
|
||||
break;
|
||||
throw new Exception("无效的课程类型");
|
||||
}
|
||||
|
||||
Console.WriteLine(DateTime.Now + $"=>{taskInfo.Id.ToString()}.开始分析视频内容 {tryCount}");
|
||||
var resData = await ChatAsync<VideoKnowRes[]>(taskInfo.Id.ToString(), postMessages, "分析字幕");
|
||||
questionRes.AddRange(resData);
|
||||
break;
|
||||
return await ChatAsync<VideoKnowRes[]>(taskInfo.Id.ToString(), postMessages, "分析字幕");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -317,10 +314,21 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
Console.WriteLine(DateTime.Now + ex.StackTrace);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public async Task<T> ChatAsync<T>(string task, string postMessages, string title, string model = "deepseek-reasoner")
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T">返回JSON类型</typeparam>
|
||||
/// <param name="task">任务id</param>
|
||||
/// <param name="postMessages">提示词</param>
|
||||
/// <param name="title">任务类型</param>
|
||||
/// <param name="model">GPT版本</param>
|
||||
/// <param name="max_tokens">最大token <para>不设置默认最大值 16000/8000</para></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public async Task<T> ChatAsync<T>(string task, string postMessages, string title, string model = "deepseek-reasoner", int max_tokens = -1)
|
||||
{
|
||||
Message[] messageArr = [
|
||||
new Message(postMessages,"user"),
|
||||
|
|
@ -335,6 +343,8 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
temperature = 0.2f,
|
||||
messages = messageArr
|
||||
};
|
||||
if (max_tokens != -1)
|
||||
chatRep.max_tokens = max_tokens;
|
||||
var tryCount = 10;
|
||||
while (--tryCount > 0)
|
||||
{
|
||||
|
|
@ -347,7 +357,8 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
var chatResContent = chatResp?.res;
|
||||
if (string.IsNullOrEmpty(chatResContent))
|
||||
throw new Exception("GPT返回message无效结果");
|
||||
if (chatResp != null) {
|
||||
if (chatResp != null)
|
||||
{
|
||||
redisCached[1] = new object[] { chatResp.Value.res, chatResp.Value.u, chatResp.Value.reasoning };
|
||||
RedisExpand.SetTaskGPTCached(task, time, redisCached);
|
||||
}
|
||||
|
|
@ -359,6 +370,8 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
chatResContent = chatResContent?.Replace("}|{", "},{");
|
||||
chatResContent = chatResContent?.Trim();
|
||||
|
||||
if (string.IsNullOrEmpty(chatResContent))
|
||||
throw new Exception("ChatGPT返回结果无有效JSON");
|
||||
var startsStr = typeof(T).IsArray ? "[" : "{";
|
||||
var endStr = typeof(T).IsArray ? "]" : "}";
|
||||
if (!chatResContent.StartsWith(startsStr))
|
||||
|
|
@ -525,7 +538,17 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
Course_Id = 27;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (taskInfo?.VideoType)
|
||||
{
|
||||
case AttachmentsInfoType.其他资料:
|
||||
case AttachmentsInfoType.新课:
|
||||
case AttachmentsInfoType.复习:
|
||||
break;
|
||||
case AttachmentsInfoType.活动:
|
||||
case AttachmentsInfoType.班会:
|
||||
default:
|
||||
throw new Exception("无效的课程类型");
|
||||
}
|
||||
|
||||
var captionsArr = JsonSerializer.Deserialize<SenseVoiceRes[]>(taskInfo.Captions);
|
||||
//处理视频授课章节
|
||||
|
|
@ -543,14 +566,14 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
//合并字幕
|
||||
var captions = ExpandFunction.GetSpeakerCaptions(captionsArr);
|
||||
var maxVideoTime = captions?.TimeBase?.LastOrDefault()?.End ?? 0;
|
||||
var questionRes = new List<VideoKnowRes>();
|
||||
while (true)
|
||||
VideoKnowRes[]? questionRes =null;
|
||||
var tryCount = 20;
|
||||
while (tryCount-- > 0)
|
||||
{
|
||||
questionRes = new List<VideoKnowRes>();
|
||||
//视频字幕分析
|
||||
await Analytics(questionRes, taskInfo, captions, sections);
|
||||
questionRes = await Analytics(taskInfo, captions, sections);
|
||||
|
||||
if (questionRes.Count == 0) continue;
|
||||
if (questionRes is null) continue;
|
||||
//处理分段 知识点
|
||||
var insertData = await GetVideoKnow(questionRes, taskInfo, sections, knowledgeInfos);
|
||||
//校验结果质量
|
||||
|
|
@ -575,7 +598,10 @@ namespace VideoAnalysisCore.AICore.GPT.DeepSeek
|
|||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (tryCount == 0)
|
||||
{
|
||||
throw new Exception("重试次数过多!");
|
||||
}
|
||||
await RedisExpand.Redis
|
||||
.HMSetAsync(RedisExpandKey.Task(task), "VideoKnows", questionRes);
|
||||
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx
|
|||
if (!string.IsNullOrEmpty(task))
|
||||
{
|
||||
Console.WriteLine(DateTime.Now + "=> SenseVoice 字幕数量" + res.Count);
|
||||
var captionsStr = JsonSerializer.Serialize(res);
|
||||
var captionsStr = res.ToJson();
|
||||
await DbScoped.Sugar
|
||||
.Updateable<VideoTask>()
|
||||
.SetColumns(it => it.Captions == captionsStr)
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ namespace VideoAnalysisCore.AICore.SherpaOnnx
|
|||
}, nint.Zero);
|
||||
var res = segments.Select(s => new OfflineSpeakerRes(s));
|
||||
await RedisExpand.Redis.HSetAsync(RedisExpandKey.Task(task), "Speaker", res);
|
||||
var speakerStr = JsonSerializer.Serialize(res);
|
||||
var speakerStr = res.ToJson();
|
||||
await DbScoped.Sugar
|
||||
.Updateable<VideoTask>()
|
||||
.SetColumns(it => it.Speaker == speakerStr)
|
||||
|
|
|
|||
|
|
@ -52,7 +52,6 @@ namespace VideoAnalysisCore.Common
|
|||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 程序配置
|
||||
/// </summary>
|
||||
|
|
@ -85,6 +84,7 @@ namespace VideoAnalysisCore.Common
|
|||
/// </summary>
|
||||
public static class ExpandFunction
|
||||
{
|
||||
|
||||
static Dictionary<string, string> FormulaData;
|
||||
static string FormulaDataKey;
|
||||
/// <summary>
|
||||
|
|
@ -92,6 +92,22 @@ namespace VideoAnalysisCore.Common
|
|||
/// </summary>
|
||||
public static string FrameName = "frame_";
|
||||
|
||||
/// <summary>
|
||||
/// 对象转化为JSON字符串
|
||||
/// </summary>
|
||||
/// <param name="o">拓展对象</param>
|
||||
/// <param name="WriteIndented">美化输出?</param>
|
||||
/// <returns></returns>
|
||||
public static string ToJson(this object o, bool WriteIndented = false)
|
||||
{
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
WriteIndented = WriteIndented // 如果需要美化输出
|
||||
};
|
||||
return JsonSerializer.Serialize(o, jsonOptions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取视频帧文件资源路径
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ namespace VideoAnalysisCore.Common
|
|||
StackTrace = context.Exception.StackTrace,
|
||||
};
|
||||
// 将错误对象序列化为JSON格式
|
||||
var json = JsonSerializer.Serialize(errorObject);
|
||||
var json =errorObject.ToJson();
|
||||
|
||||
// 设置响应内容类型为JSON
|
||||
context.HttpContext.Response.ContentType = "application/json";
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ namespace VideoAnalysisCore.Common.Expand
|
|||
|
||||
if (request.isolated_formula_wrapper != null)
|
||||
{
|
||||
var isolatedWrapper = JsonSerializer.Serialize(request.isolated_formula_wrapper);
|
||||
var isolatedWrapper = request.isolated_formula_wrapper.ToJson();
|
||||
content.Add(new StringContent(isolatedWrapper), nameof(request.isolated_formula_wrapper));
|
||||
parameters[nameof(request.isolated_formula_wrapper)] = isolatedWrapper;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ namespace VideoAnalysisCore.Controllers
|
|||
[HttpPost(Name = "NodePackage")]
|
||||
public async Task<IActionResult> NodePackage(NodePackageReq[] reqArr)
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now} 文件包订阅请求 req=" + JsonSerializer.Serialize(reqArr));
|
||||
Console.WriteLine($"{DateTime.Now} 文件包订阅请求 req=" + reqArr.ToJson());
|
||||
if (reqArr is null || reqArr.Count() == 0)
|
||||
return BadRequest("无效视频列表数据");
|
||||
var videos = new List<VideoTask>(reqArr.Count());
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ namespace VideoAnalysisCore.Job
|
|||
var request = new HttpRequestMessage(HttpMethod.Post, postUrl);
|
||||
request.Headers.Add("Area", item.Area); // 直接添加到本次请求头
|
||||
request.Headers.Add("HostIP", item.HostIP); // 直接添加到本次请求头
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(postData), Encoding.UTF8, "application/json");
|
||||
request.Content = new StringContent(postData.ToJson(), Encoding.UTF8, "application/json");
|
||||
responseMessage = await apiClent.SendAsync(request);
|
||||
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
|
|
|
|||
Loading…
Reference in New Issue