Merge remote-tracking branch 'origin/master'

This commit is contained in:
Klein
2023-11-13 17:01:50 +08:00
11 changed files with 256 additions and 3 deletions
@@ -59,4 +59,5 @@ public class PluginName {
public static final String TITLE_GENERATE = "TitleGenerate";
public static final String DESC_GENERATE = "DescriptionGenerate";
public static final String LABEL_GENERATE = "LabelGenerate";
}
@@ -13,3 +13,8 @@ chopperbot:
enable: 0
address: 127.0.0.1
port: 7890
plugin:
title-generate-plugin:
handler: ${TITLE-GENERATE-PLUGIN-HANDLER:GptTitleGenerator}
label-generate-plugin:
handler: ${LABEL-GENERATE-PLUGIN-HANDLER:GptLabelGenerator}
@@ -0,0 +1,28 @@
package org.example.core.auto.video.label;
import org.example.util.MapUtil;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class CommonLabelGenerator extends LabelGenerator{
@Override
public List<?> sqlInit() {
return null;
}
@Override
public void preGenerate() {
}
@Override
public List<String> generate(Map<String, Object> data) {
String liver = MapUtil.getString(data,"liver");
String platform = MapUtil.getString(data,"platform");
String tag = MapUtil.getString(data,"tag");
return List.of(liver,platform,tag);
}
}
@@ -0,0 +1,85 @@
package org.example.core.auto.video.label;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.constpool.PluginName;
import org.example.core.gpt.ChatGPTMsgBuilder;
import org.example.core.gpt.OpenAPIPlugin;
import org.example.exception.plugin.PluginDependOnException;
import org.example.init.InitPluginRegister;
import org.example.mapper.AnalysisSchemeMapper;
import org.example.plugin.PluginCheckAndDo;
import org.example.pojo.AnalysisScheme;
import org.example.pojo.DescScheme;
import org.example.pojo.GPTKey;
import org.example.sql.annotation.SQLInit;
import org.example.util.ExceptionUtil;
import org.example.util.MapUtil;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class GptLabelGenerator extends LabelGenerator{
@Resource
AnalysisSchemeMapper mapper;
private List<AnalysisScheme> schemes;
@Override
@SQLInit(table = "analysis_scheme",tableSQL = "CREATE TABLE \"analysis_scheme\" (\n" +
" \"id\" integer NOT NULL,\n" +
" \"system\" text NOT NULL,\n" +
" \"comment\" TEXT,\n" +
" PRIMARY KEY (\"id\")\n" +
")",mapper = AnalysisSchemeMapper.class)
public List<AnalysisScheme> sqlInit() {
return List.of(new AnalysisScheme(null,
"请你作为一个专门看直播的观众,对下列的观众发送的弹幕内容进行分析,然后根据弹幕内容返回从以下几个标签返回给我最合适的一个标签来形容这段内容",
"方案一"));
}
@Override
public void preGenerate() {
if (!InitPluginRegister.isRegister(PluginName.CHAT_GPT)) {
throw PluginDependOnException.MissingFatherPlugin(PluginName.CHAT_GPT,"");
}
try {
schemes = mapper.selectList(new QueryWrapper<>());
}catch (Exception e){
throw new RuntimeException("读取label_scheme表失败");
}
}
@Override
public List<String> generate(Map<String, Object> data) {
if (schemes.isEmpty())return new ArrayList<>();
try {
return PluginCheckAndDo.CheckAndGet((plugin)->{
Object barrages = data.get("barrages");
String barrageStr = "";
if(barrages instanceof List){
barrageStr = OpenAPIPlugin.zipContent(OpenAPIPlugin.zipContent((List) barrages).toString());
}
GPTKey gptKey = ((OpenAPIPlugin) plugin).choseKey(OpenAPIPlugin.APIFunc.CHAT_GPT);
AnalysisScheme scheme = schemes.get(0);
if(gptKey==null)return new ArrayList<>();
ChatGPTMsgBuilder builder = new ChatGPTMsgBuilder().model(gptKey.getModel())
.system(scheme.getSystem())
.user("弹幕:" + barrageStr)
.stream(false);
String liver = MapUtil.getString(data,"liver");
String platform = MapUtil.getString(data,"platform");
String tag = MapUtil.getString(data,"tag");
JSONObject object = ((OpenAPIPlugin) plugin).reqGPT(builder, OpenAPIPlugin.APIFunc.CHAT_GPT);
return List.of(((OpenAPIPlugin) plugin).getCommonRes(object),liver,platform,tag);
},PluginName.CHAT_GPT,List.class);
} catch (Exception e){
throw new RuntimeException(ExceptionUtil.getCause(e));
}
}
}
@@ -0,0 +1,28 @@
package org.example.core.auto.video.label;
import org.example.core.auto.AbstractGeneratePlugin;
import org.example.core.auto.video.title.TitleGenerator;
import org.example.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
@Component
public class LabelGeneratePlugin extends AbstractGeneratePlugin<LabelGenerator> {
@Resource
Map<String, LabelGenerator> labelGeneratorMap;
@Value("${chopperbot.plugin.label-generate-plugin.handler}")
private String configType;
@Override
public boolean init() {
this.generatorMap = labelGeneratorMap;
this.type = StringUtil.lowerCaseFirstLetter(configType);
return super.init();
}
}
@@ -0,0 +1,32 @@
package org.example.core.auto.video.label;
import org.example.bean.section.PackageSection;
import org.example.bean.section.VideoSection;
import org.example.core.auto.SectionGenerator;
import org.example.mapper.AnalysisSchemeMapper;
import org.example.pojo.AnalysisScheme;
import org.example.sql.annotation.SQLInit;
import org.example.util.ClassUtil;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
public abstract class LabelGenerator extends SectionGenerator<List<String>> {
@Override
public <T extends VideoSection, V extends VideoSection> V generator(T section) {
PackageSection packageSection = new PackageSection();
BeanUtils.copyProperties(section,packageSection);
try {
Map<String, Object> map = ClassUtil.toDeepMap(packageSection);
List<String> labels = this.generate(map);
packageSection.setLabels(labels);
}catch (Exception e){
return (V) section;
}
return (V) packageSection;
}
}
@@ -7,6 +7,7 @@ import org.example.core.auto.SectionPipeline;
import org.example.core.auto.video.description.DescGenerator;
import org.example.plugin.SpringBootPlugin;
import org.example.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@@ -26,10 +27,13 @@ public class TitleGeneratePlugin extends AbstractGeneratePlugin<TitleGenerator>
@Resource
Map<String, TitleGenerator> titleGeneratorMap;
@Value("${chopperbot.plugin.title-generate-plugin.handler}")
private String configType;
@Override
public boolean init() {
this.generatorMap = titleGeneratorMap;
this.type = StringUtil.lowerCaseFirstLetter(GptTitleGenerator.class.getSimpleName());
this.type = StringUtil.lowerCaseFirstLetter(configType);
return super.init();
}
@@ -17,7 +17,8 @@ import org.springframework.stereotype.Component;
needPlugin = {PluginName.CHAT_GPT,PluginName.LABEL_MANAGER},
pluginClass= EmotionAnalysisPlugin.class,
springBootPlugin = true,
ignore=true
ignore=true,
autoStart = false
)
@Component
public class EmotionAnalysisPluginMachine extends SpringPlugInitMachine{
@@ -0,0 +1,19 @@
package org.example.init;
import org.example.constpool.ModuleName;
import org.example.constpool.PluginName;
import org.example.core.auto.video.label.LabelGeneratePlugin;
import org.example.plugin.annotation.Plugin;
import org.springframework.stereotype.Component;
@Plugin(moduleName = ModuleName.SECTION_WORK,
pluginName = PluginName.LABEL_GENERATE,
pluginName_CN = "视频情感标签生成插件",
pluginDescription = "根据切片内容自动生成视频情感标签",
pluginClass= LabelGeneratePlugin.class,
springBootPlugin = true,
ignore=true
)
@Component
public class LabelGeneratorPluginInitMachine extends SpringPlugInitMachine{
}
@@ -0,0 +1,48 @@
package org.example.sectionwork;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.example.ConsoleApplication;
import org.example.bean.section.VideoSection;
import org.example.core.auto.video.description.DescGeneratorPlugin;
import org.example.core.auto.video.label.LabelGeneratePlugin;
import org.example.core.auto.video.title.TitleGeneratePlugin;
import org.example.util.JsonFileUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConsoleApplication.class,webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class LabelGenerateTest {
@Resource
LabelGeneratePlugin labelGeneratePlugin;
@Test
public void test(){
Map<String, Object> map = JsonFileUtil.readJsonFile("E:\\Project\\ChopperBot\\config\\Barrage\\online\\huya\\Uzi_2023-10-18 21_22_57.json");
Object data = map.get("data");
if(data instanceof JSONArray){
List<String> content = ((JSONArray) data).stream().map(o -> {
if (o instanceof JSONObject) {
return ((JSONObject) o).get("content").toString();
}
return "";
}).collect(Collectors.toList());
VideoSection videoSection = new VideoSection();
videoSection.setBarrages(content.subList(0,100));
videoSection.setTag("英雄联盟");
videoSection.setLiver("UZI");
VideoSection process = labelGeneratePlugin.process(videoSection);
System.out.println(process);
}
}
}
+3 -1
View File
@@ -26,11 +26,13 @@
"HotConfig":true,
"TaskCenter":true,
"CreeperManager":true,
"DescriptionGenerate":true,
"InstantSlicing":true,
"LiveDownLoadManager":true,
"BarragePopularRange":true,
"HotRecommendation":true,
"NoticePlugin":true,
"TitleGenerate":true,
"VideoPush":true,
"AccountManager":true,
"LabelManager":true,
@@ -38,5 +40,5 @@
"OpenAPI":true
}
},
"updateTime":"2023-10-25 15:01:54"
"updateTime":"2023-11-06 12:00:41"
}