using AntDesign;
using AntDesign.TableModels;
using FreeRedis;
using Microsoft.Extensions.DependencyModel;
using SqlSugar;
using SqlSugar.IOC;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Runtime.Loader;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UserCenter.Model.Interface;
using VideoAnalysisCore.AICore.SherpaOnnx;
using VideoAnalysisCore.Model.Dto;
using VideoAnalysisCore.Model.Enum;
using VideoAnalysisCore.Model.Interface;
namespace VideoAnalysisCore.Common
{
///
/// 程序 公共变量
///
public static class AppCommon
{
///
/// 应用有效程序集
///
public static readonly IEnumerable Assemblies;
///
/// 主库数据库表类型
///
public static readonly IEnumerable DbMatserType;
public static readonly IEnumerable KnowsType;
static AppCommon()
{
try
{
Assemblies = ExpandFunction.GetAssemblies();
var assembliesType = Assemblies.Where(s => s.FullName.Contains("VideoAnalysis")).SelectMany(s => s.ExportedTypes
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false)));
DbMatserType = assembliesType
.Where(u => u.GetInterfaces().Contains(typeof(IDB)));
}
catch
{
throw;
}
}
///
/// 程序配置
///
public static AppConfig Config = new AppConfig();
///
/// ServiceProvider
///
public static IServiceProvider? Services;
///
/// 文件下载路径
///
public static string TaskCachedFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TaskCachedFile");
///
/// 模型地址
///
public static string AIModelFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AICore", "_Static");
///
/// 获取视频路径
///
///
///
public static string GetVideoPath(string tid) => $"./video/{tid}/{tid}.mp4";
}
///
/// 拓展函数
///
public static class ExpandFunction
{
static Dictionary FormulaData;
static string FormulaDataKey;
///
/// 识别字符串中的json字符串
///
///
///
public static List ExtractJson(this string input)
{
List jsonList = new List();
int index = 0;
while (index < input.Length)
{
if (input[index] == '{' || input[index] == '[')
{
int startIndex = index;
int openCount = 1;
index++;
while (index < input.Length && openCount > 0)
{
if (input[index] == '{' || input[index] == '[')
{
openCount++;
}
else if (input[index] == '}' || input[index] == ']')
{
openCount--;
}
index++;
}
if (openCount == 0)
{
string json = input.Substring(startIndex, index - startIndex);
jsonList.Add(json);
}
}
else
{
index++;
}
}
return jsonList;
}
///
/// 处理数学公式
///
///
///
public static string HandleFormula(string f)
{
if (FormulaData is null)
{
var hotwords = JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(AppCommon.AIModelFile, "Hotwords.json")));
foreach (var item in hotwords.OrderByDescending(s=>s.key.Count()))
foreach (var key in item.v)
FormulaData.Add(key, item.key);
}
if (string.IsNullOrEmpty(FormulaDataKey))
FormulaDataKey = string.Join("|", FormulaData.Keys.Count);
if (string.IsNullOrEmpty(f))
return f;
return Regex.Replace(f, FormulaDataKey,
match =>
FormulaData[match.Value]
);
}
///
/// 转换 ant 查询枚举 到 sqlsuger枚举
///
/// ant 查询枚举
///
///
public static ConditionalType ConvertToConditionalType(TableFilterCompareOperator filterOperator)
{
return filterOperator switch
{
TableFilterCompareOperator.Equals => ConditionalType.Equal,
TableFilterCompareOperator.Contains => ConditionalType.Like,
TableFilterCompareOperator.StartsWith => ConditionalType.LikeLeft,
TableFilterCompareOperator.EndsWith => ConditionalType.LikeRight,
TableFilterCompareOperator.GreaterThan => ConditionalType.GreaterThan,
TableFilterCompareOperator.LessThan => ConditionalType.LessThan,
TableFilterCompareOperator.GreaterThanOrEquals => ConditionalType.GreaterThanOrEqual,
TableFilterCompareOperator.LessThanOrEquals => ConditionalType.LessThanOrEqual,
TableFilterCompareOperator.Condition => ConditionalType.In,
TableFilterCompareOperator.NotEquals => ConditionalType.NoEqual,
TableFilterCompareOperator.IsNull => ConditionalType.IsNullOrEmpty,
TableFilterCompareOperator.IsNotNull => ConditionalType.IsNot,
TableFilterCompareOperator.NotContains => ConditionalType.NoLike,
TableFilterCompareOperator.TheSameDateWith => ConditionalType.EqualNull,
TableFilterCompareOperator.Between => ConditionalType.Range,
_ => throw new ArgumentOutOfRangeException(nameof(filterOperator), filterOperator, "未知的枚举类型!")
};
}
public static List ToSqlSugerWhere(this QueryModel qm )
{
return qm.FilterModel.SelectMany(s => s.Filters.Select(x => new ConditionalModel()
{
FieldName = s.FieldName,
ConditionalType = ConvertToConditionalType( x.FilterCompareOperator),
FieldValue = x.Value.GetType().IsEnum?((int)x.Value).ToString() : x.Value.ToString(),
} as IConditionalModel)).ToList();
}
///
/// 获取应用有效程序集
///
/// IEnumerable
public static List GetAssemblies()
{
// 获取当前解决方案的所有程序集
var assembliesStr = DependencyContext.Default.RuntimeLibraries
.Where(u => !u.Name.StartsWith(nameof(Microsoft))
&& !u.Name.StartsWith(nameof(System))
&& !u.Name.StartsWith("netstandard")
&& (u.Type == "project"));
var assemblies = assembliesStr.Select(a => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(a.Name))).ToList();
var assemblies1 = Assembly.GetEntryAssembly().GetReferencedAssemblies().Where(x => x.Name.StartsWith("App.") || x.Name.StartsWith("UserCenter."))
.Select(a => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(a.Name))).ToList();
foreach (var item in assemblies1)
{
if (!assemblies.Contains(item))
assemblies.Add(item);
}
return assemblies;
}
///
/// 获取Task处理后的 说话人字幕
///
public static TotalCaptionsDto GetSpeakerCaptions(SenseVoiceRes[] captionsArr, OfflineSpeakerRes[] speakerArr)
{
if (captionsArr is null || captionsArr.Length == 0)
//|| speakerArr is null || speakerArr.Length == 0)
throw new Exception("音频解析数据异常");
// 教师说话人Id
var techerId = speakerArr is null || !speakerArr.Any()
? 0
:speakerArr.GroupBy(s=>s.SpeakerIndex).Select(s => (s.Key,s.Sum(x=>x.Total)))
.OrderByDescending(s=>s.Item2).First().Key;
var teacherSpeaking = 0f;
var studentSpeaking = 0f;
var results = new Dictionary>();
var ss = new List { 1 };
if (speakerArr is null || speakerArr.Count() == 0)
{
results = captionsArr.ToDictionary(s => s, s=> ss);
}
else
{
foreach (var segment in captionsArr)
{
var spList = new List();
foreach (var speakerRes in speakerArr)
{
if (speakerRes.Start > segment.End)
break;
if (segment.Start <= speakerRes.End
&& segment.End >= speakerRes.Start)
{
if (speakerRes.SpeakerIndex == techerId)
teacherSpeaking += speakerRes.Total;
else
studentSpeaking += speakerRes.Total;
spList.Add(speakerRes.SpeakerIndex);
}
}
var sp = spList.Distinct().ToList();
if (sp.Count > 0)
results.Add(segment, sp);
}
}
//拼接 提示词字幕源
var stringBuilder = new StringBuilder();
foreach (var item in results)
{
//stringBuilder.Append(item.Value.First());
//stringBuilder.Append(":");
stringBuilder.Append((int)item.Key.Start);
stringBuilder.Append(":");
stringBuilder.Append((int)item.Key.End);
stringBuilder.Append(":");
stringBuilder.Append(item.Key.Text);
stringBuilder.Append("|");
}
return new TotalCaptionsDto
{
StudentSpeaking = (decimal)studentSpeaking,
TeacherSpeaking = (decimal)teacherSpeaking,
Captions = stringBuilder.ToString(),
TimeBase = results.Select(s=>new TimeBase()
{
Start = s.Key.Start,
End = s.Key.End,
Content = s.Key.Text,
TimeBaseType = s.Value.Count == 1 && s.Value.First() == techerId
? TimeBaseTypeEnum.教师讲授
: TimeBaseTypeEnum.互动交流
})
};
}
///
/// 转化枚举
///
///
///
public static T? ToEnum(this object data) where T : struct, System.Enum
{
try
{
if (data is null || string.IsNullOrEmpty(data?.ToString()))
return null;
return System.Enum.Parse(data.ToString());
}
catch (Exception)
{
return null;
}
}
///
/// 转化本地缓存目录
///
/// 任务id
///
public static string LocalPath(this string taskId)
{
return Path.Combine(AppCommon.TaskCachedFile, taskId);
}
}
///
/// ffmpeg配置
///
public class GptConfig
{
///
/// 请求 公开的服务地址
///
public string Host { get; set; } = string.Empty;
///
/// api的密钥
///
public string ApiKey { get; set; } = string.Empty;
}
///
/// 文本模型 配置
///
public class ChatGptConfig
{
///
/// KIMI
///
///
public GptConfig ChatGpt { get; set; } = new GptConfig();
public GptConfig DeepSeek { get; set; } = new GptConfig();
public GptConfig KIMI { get; set; } = new GptConfig();
public GptConfig aliyun { get; set; } = new GptConfig();
}
///
/// ffmpeg配置
///
public class FFmpegConfig
{
///
/// 音频切片时间段
/// 0不切片
///
public int TimeSlice { get; set; } = 0;
}
///
/// Whisper配置
///
public class WhisperConfig
{
///
/// 模型名称
///
public string ModelName { get; set; } = string.Empty;
}
///
/// 管理界面Admin账号
///
public class AdminConfig
{
///
/// 账号
///
public string Account { get; set; } = string.Empty;
///
/// 密码
///
public string Password { get; set; } = string.Empty;
}
///
/// redis配置
///
public class RedisConfig
{
///
/// redis连接字符串
///
public string ConnectionString { get; set; } = string.Empty;
}
public class DBConfig
{
///
/// 主库链接
///
public string ConnectionString { get; set; }=string.Empty;
///
/// 数据库类型
///
public IocDbType SqlType { get; set; }
///
/// 启动时更新表结构
///
public bool UpdateTable { get; set; }
///
/// 配置ID
///
public long ConfigId { get; set; }
}
///
/// 子系统配置
///
public class SubsystemInfo
{
public string APIUrl { get; set; } = string.Empty;
public string Token { get; set; } = string.Empty;
}
///
/// 子系统配置
///
public class SubsystemConfig
{
public SubsystemInfo 蓝鲸智库 { get; set; } = new SubsystemInfo();
}
///
/// 应用程序配置
///
public class AppConfig
{
///
/// 程序ID
///
public string ID { get; set; } = string.Empty;
///
/// Admin
///
public AdminConfig Admin { get; set; } = new AdminConfig();
///
/// 子系统
///
public SubsystemConfig Subsystem { get; set; } = new SubsystemConfig();
///
/// redis
///
public RedisConfig Redis { get; set; } = new RedisConfig();
///
/// Whisper AI
///
public WhisperConfig Whisper { get; set; } = new WhisperConfig();
///
/// FFmpeg
///
public FFmpegConfig FFmpeg { get; set; } = new FFmpegConfig();
///
/// ChatGpt
///
public ChatGptConfig ChatGpt { get; set; } = new ChatGptConfig();
///
/// 阿里云视频点播配置
///
public AlibabaCloudVodConfig AlibabaCloudVod { get; set; } = new AlibabaCloudVodConfig();
///
/// 数据库配置
///
public DBConfig DB { get; set; } = new DBConfig();
///
/// 其他数据库配置
///
public DBConfig[] OtherDBArr { get; set; } = Array.Empty();
}
}