mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-26 09:20:50 +00:00
feat: AI自动生成小说封面图片
This commit is contained in:
parent
04fc8e878a
commit
328bd55587
63
README.md
63
README.md
@ -39,25 +39,25 @@ novel-plus -- 父工程
|
||||
|
||||
## 技术选型
|
||||
|
||||
| 技术 | 说明
|
||||
|---------------------| ---------------------------
|
||||
| Spring Boot | Spring 应用快速开发脚手架
|
||||
| Spring AI | Spring 官方 AI 框架
|
||||
| MyBatis | 持久层 ORM 框架
|
||||
| MyBatis Dynamic SQL | Mybatis 动态 sql
|
||||
| PageHelper | MyBatis 分页插件
|
||||
| MyBatis Generator | 持久层代码生成插件
|
||||
| Sharding-JDBC | 代码层分库分表中间件
|
||||
| JJWT | JWT 登录支持
|
||||
| Spring Security | 安全框架
|
||||
| Apache Shiro | 安全框架
|
||||
| Redis | 缓存方案
|
||||
| Aliyun OSS | 阿里云对象存储服务(图片存储备选方案)
|
||||
| Lombok | 简化对象封装工具
|
||||
| Docker | 应用容器引擎
|
||||
| MySQL | 数据库服务
|
||||
| Thymeleaf | 模板引擎
|
||||
| Layui | 前端 UI 框架
|
||||
| 技术 | 说明
|
||||
|---------------------|---------------------
|
||||
| Spring Boot | Spring 应用快速开发脚手架
|
||||
| Spring AI | Spring 官方 AI 框架
|
||||
| MyBatis | 持久层 ORM 框架
|
||||
| MyBatis Dynamic SQL | Mybatis 动态 sql
|
||||
| PageHelper | MyBatis 分页插件
|
||||
| MyBatis Generator | 持久层代码生成插件
|
||||
| Sharding-JDBC | 代码层分库分表中间件
|
||||
| JJWT | JWT 登录支持
|
||||
| Spring Security | 安全框架
|
||||
| Apache Shiro | 安全框架
|
||||
| Redis | 缓存方案
|
||||
| Aliyun OSS | 阿里云对象存储服务(图片存储备选方案)
|
||||
| Lombok | 简化对象封装工具
|
||||
| Docker | 应用容器引擎
|
||||
| MySQL | 数据库服务
|
||||
| Thymeleaf | 模板引擎
|
||||
| Layui | 前端 UI 框架
|
||||
|
||||
## 项目截图
|
||||
|
||||
@ -67,15 +67,25 @@ novel-plus -- 父工程
|
||||
[](https://www.xxyopen.com/images/resource/os/novel-plus/green3.png)
|
||||
[](https://www.xxyopen.com/images/resource/os/novel-plus/green2.png)
|
||||
|
||||
## AI写作
|
||||
## 演示视频
|
||||
|
||||
novel-plus 5.x 版本已集成 Spring 官方最新发布的 Spring AI 框架,在小说章节发布页面的文本编辑器中推出了多项智能编辑功能,包括 AI 扩写、缩写、续写及文本润色等。这些功能的设计灵感来源于百家号文章编辑器中的 AI 助手。
|
||||
https://www.bilibili.com/video/BV18e41197xs
|
||||
|
||||
目前,AI 编辑功能仍处于实验阶段,仅实现了基础的核心功能。我们非常重视用户的实际使用体验和反馈,未来将根据用户需求和使用情况,持续优化和调整该功能。如果用户反馈积极,我们计划进一步开发更高级的 AI 功能,例如自动生成有声小说、智能情节推荐等,以全面提升 novel-plus 的创作能力和用户体验。
|
||||
## AI 功能
|
||||
|
||||
novel-plus 5.x 已集成 Spring 官方最新发布的 Spring AI 框架,并推出多项 AI 功能:
|
||||
|
||||
1. v5.0.0 版本在小说章节发布页面的文本编辑器中集成了多项智能编辑功能,包括 AI 扩写、缩写、续写及文本润色等。这些功能的设计灵感来源于百家号文章编辑器中的 AI 助手。
|
||||
2. v5.1.0 版本在小说发布页面,新增 AI 生成封面图功能。若作家未上传自定义封面图,系统将根据小说信息自动生成封面图。
|
||||
|
||||
目前,AI 功能仍处于实验阶段,仅实现了基础的核心功能。我们非常重视用户的实际使用体验和反馈,未来将根据用户需求和使用情况,持续优化和调整该功能。如果用户反馈积极,我们计划进一步开发更高级的
|
||||
AI 功能,例如自动生成有声小说、智能情节推荐等,以全面提升 novel-plus 的创作能力和用户体验。
|
||||
|
||||
我们将持续关注 AI 技术的发展,并致力于将其与小说创作场景深度融合,为用户带来更智能、更便捷的创作工具。
|
||||
|
||||
由于 DeepSeek 官方 API 目前不可用,novel-plus 项目默认使用的是第三方[硅基流动](https://cloud.siliconflow.cn/i/DOgMRH9S)提供的 API,采用的 AI 模型为:`deepseek-ai/DeepSeek-R1-Distill-Llama-8B`(DeepSeek-R1 的蒸馏版本,免费使用)。只需注册一个硅基流动账号,创建一个 API 密钥,并将其添加到 novel-plus 项目 novel-front 模块的 yaml 配置文件中,即可体验 novel-plus 项目的 AI 写作功能。
|
||||
由于 DeepSeek 官方 API 目前不可用,novel-plus 项目默认使用的是第三方[硅基流动](https://cloud.siliconflow.cn/i/DOgMRH9S)
|
||||
提供的 API,采用的 AI 模型有对话模型`deepseek-ai/DeepSeek-R1-Distill-Llama-8B`(DeepSeek-R1 的蒸馏版本,免费使用)和生图模型`Kwai-Kolors/Kolors`(快手 Kolors 团队开发的文本到图像生成模型,免费使用)。只需注册一个硅基流动账号,创建一个
|
||||
API 密钥,并将其添加到 novel-plus 项目 novel-front 模块的 yaml 配置文件中,即可体验 novel-plus 项目的 AI 写作功能。
|
||||
|
||||
```yaml
|
||||
spring:
|
||||
@ -88,10 +98,7 @@ spring:
|
||||
model: deepseek-ai/DeepSeek-R1-Distill-Llama-8B
|
||||
```
|
||||
|
||||

|
||||
## 演示视频
|
||||
|
||||
https://www.bilibili.com/video/BV18e41197xs
|
||||
> ⚠️ novel-plus 项目默认使用的都是免费 AI 模型,生成效果有限。如果对生成内容有更高的要求,建议选用付费的 AI 模型。
|
||||
|
||||
## 增值服务
|
||||
|
||||
@ -117,3 +124,5 @@ https://www.bilibili.com/video/BV18e41197xs
|
||||
## 免责声明
|
||||
|
||||
本项目提供的爬虫工具仅用于采集项目初期的测试数据,请勿用于商业盈利。 用户使用本系统从事任何违法违规的事情,一切后果由用户自行承担,作者不承担任何责任。
|
||||
|
||||
|
||||
|
@ -18,6 +18,11 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -125,5 +130,23 @@ public class FileUtil {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
*
|
||||
* @param downloadUrl 下载的URL
|
||||
* @param savePath 保存的路径
|
||||
*/
|
||||
@SneakyThrows
|
||||
public void downloadFile(String downloadUrl, String savePath) {
|
||||
Path path = Paths.get(savePath);
|
||||
Path parentPath = path.getParent();
|
||||
if (Files.notExists(parentPath)) {
|
||||
Files.createDirectories(parentPath);
|
||||
}
|
||||
URL url = new URL(downloadUrl);
|
||||
try (InputStream in = url.openStream()) {
|
||||
Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.config.BookPriceProperties;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.core.utils.Constants;
|
||||
import com.java2nb.novel.core.utils.FileUtil;
|
||||
import com.java2nb.novel.core.utils.StringUtil;
|
||||
import com.java2nb.novel.entity.Book;
|
||||
import com.java2nb.novel.entity.*;
|
||||
@ -27,10 +28,16 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.mybatis.dynamic.sql.SortSpecification;
|
||||
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
||||
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
|
||||
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
|
||||
import org.springframework.ai.image.Image;
|
||||
import org.springframework.ai.image.ImagePrompt;
|
||||
import org.springframework.ai.image.ImageResponse;
|
||||
import org.springframework.ai.openai.OpenAiImageModel;
|
||||
import org.springframework.ai.openai.OpenAiImageOptions;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -39,13 +46,16 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.java2nb.novel.mapper.BookCategoryDynamicSqlSupport.bookCategory;
|
||||
import static com.java2nb.novel.mapper.BookCategoryDynamicSqlSupport.sort;
|
||||
import static com.java2nb.novel.mapper.BookCommentDynamicSqlSupport.bookComment;
|
||||
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.bookContent;
|
||||
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.content;
|
||||
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.*;
|
||||
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.book;
|
||||
import static com.java2nb.novel.mapper.BookIndexDynamicSqlSupport.bookIndex;
|
||||
import static com.java2nb.novel.mapper.BookSettingDynamicSqlSupport.bookSetting;
|
||||
import static org.mybatis.dynamic.sql.SqlBuilder.*;
|
||||
@ -87,6 +97,10 @@ public class BookServiceImpl implements BookService {
|
||||
|
||||
private final BookPriceProperties bookPriceConfig;
|
||||
|
||||
private final OpenAiImageModel openAiImageModel;
|
||||
|
||||
private final ThreadPoolExecutor threadPoolExecutor;
|
||||
|
||||
private final IdWorker idWorker = IdWorker.INSTANCE;
|
||||
|
||||
|
||||
@ -235,7 +249,7 @@ public class BookServiceImpl implements BookService {
|
||||
BookIndexDynamicSqlSupport.isVip)
|
||||
.from(bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.bookId, isEqualTo(bookId));
|
||||
if("index_num desc".equals(orderBy)){
|
||||
if ("index_num desc".equals(orderBy)) {
|
||||
where.orderBy(BookIndexDynamicSqlSupport.indexNum.descending());
|
||||
}
|
||||
return bookIndexMapper.selectMany(where
|
||||
@ -502,6 +516,7 @@ public class BookServiceImpl implements BookService {
|
||||
|
||||
@Override
|
||||
public void addBook(Book book, Long authorId, String penName) {
|
||||
book.setId(IdWorker.INSTANCE.nextId());
|
||||
//判断小说名是否存在
|
||||
if (queryIdByNameAndAuthor(book.getBookName(), penName) != null) {
|
||||
//该作者发布过此书名的小说
|
||||
@ -516,7 +531,36 @@ public class BookServiceImpl implements BookService {
|
||||
book.setCreateTime(new Date());
|
||||
book.setUpdateTime(book.getCreateTime());
|
||||
bookMapper.insertSelective(book);
|
||||
|
||||
if (Objects.isNull(book.getPicUrl()) || !book.getPicUrl().startsWith(Constants.LOCAL_PIC_PREFIX)) {
|
||||
// 用户没有上传封面图片,AI自动生成封面图片
|
||||
threadPoolExecutor.execute(() -> {
|
||||
String prompt = String.format("生成一本小说的封面图片,图片中间显示书名《%s》,书名下方显示作者“%s 著”。",
|
||||
book.getBookName(), book.getAuthorName());
|
||||
log.debug("prompt:{}", prompt);
|
||||
ImageResponse response = openAiImageModel.call(
|
||||
new ImagePrompt(prompt,
|
||||
OpenAiImageOptions.builder()
|
||||
.quality("hd")
|
||||
.height(800)
|
||||
.width(600).build())
|
||||
);
|
||||
Image output = response.getResult().getOutput();
|
||||
Date currentDate = new Date();
|
||||
String picUrl = Constants.LOCAL_PIC_PREFIX +
|
||||
"aiGen/" + DateUtils.formatDate(currentDate, "yyyy") + "/" +
|
||||
DateUtils.formatDate(currentDate, "MM") + "/" +
|
||||
DateUtils.formatDate(currentDate, "dd") + "/" + book.getId() + ".png";
|
||||
FileUtil.downloadFile(output.getUrl(), picSavePath + picUrl);
|
||||
bookMapper.update(update(BookDynamicSqlSupport.book)
|
||||
.set(BookDynamicSqlSupport.picUrl)
|
||||
.equalTo(picUrl)
|
||||
.set(updateTime)
|
||||
.equalTo(currentDate)
|
||||
.where(id, isEqualTo(book.getId()))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -48,13 +48,17 @@ book:
|
||||
spring:
|
||||
ai:
|
||||
openai:
|
||||
api-key: sk-nnhjmxuljagcuubbovjztbhkiawqaabzziazeurppinxtgva
|
||||
image:
|
||||
enabled: true
|
||||
base-url: https://api.siliconflow.cn
|
||||
api-key: sk-jjtixmivxaccndqgkqfkbgkzvmbctdxogcrfbjzfttbouitt
|
||||
options:
|
||||
model: Kwai-Kolors/Kolors
|
||||
response_format: URL
|
||||
api-key: sk-jjtixmivxaccndqgkqfkbgkzvmbctdxogcrfbjzfttbouitt
|
||||
base-url: https://api.siliconflow.cn
|
||||
chat:
|
||||
options:
|
||||
model: deepseek-ai/DeepSeek-R1-Distill-Llama-8B
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -51,7 +51,8 @@
|
||||
</div>
|
||||
<div class="my_r">
|
||||
<div id="noContentDiv">
|
||||
<div class="tc" style="margin-top: 200px"><a href="/author/book_add.html" class="btn_red">创建作品</a></div>
|
||||
<div class="tc" style="margin-top: 200px"><a href="/author/book_add.html" class="btn_red">创建作品</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="my_bookshelf" id="hasContentDiv" style="display: none">
|
||||
@ -142,10 +143,13 @@
|
||||
<script src="/javascript/common.js" type="text/javascript"></script>
|
||||
|
||||
<script language="javascript" type="text/javascript">
|
||||
var searchCount = 0;
|
||||
var timeout;
|
||||
search(1, 5);
|
||||
|
||||
function search(curr, limit) {
|
||||
|
||||
searchCount++;
|
||||
clearTimeout(timeout);
|
||||
$.ajax({
|
||||
type: "get",
|
||||
url: "/author/listBookByPage",
|
||||
@ -155,6 +159,7 @@
|
||||
if (data.code == 200) {
|
||||
var bookList = data.data.list;
|
||||
if (bookList.length > 0) {
|
||||
var aiPicGenerating = bookList[0].picUrl == '/images/default.gif'
|
||||
$("#hasContentDiv").css("display", "block");
|
||||
$("#noContentDiv").css("display", "none");
|
||||
var bookListHtml = "";
|
||||
@ -226,6 +231,12 @@
|
||||
});
|
||||
});
|
||||
|
||||
if (curr === 1 && aiPicGenerating && searchCount < 10) {
|
||||
timeout = setTimeout(function () {
|
||||
search(curr, limit);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user