Skip to content

Commit c3aeece

Browse files
authored
feat: add gemini mllm (#250)
* feat: add gemini mllm * docs: update docs
1 parent 5289798 commit c3aeece

File tree

6 files changed

+94
-12
lines changed

6 files changed

+94
-12
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- **硬件要求极低**:无需GPU,只需最基础的单核CPU搭配最低的运存即可完成录制,弹幕渲染,上传等等全部过程,无最低配置要求,10年前的电脑或服务器依然可以使用!
3636
- **( :tada: NEW)自动渲染字幕**(如需使用本功能,则需保证有 Nvidia 显卡):采用 OpenAI 的开源模型 [`whisper`](https://github.com/openai/whisper),自动识别视频内语音并转换为字幕渲染至视频中。
3737
- **( :tada: NEW)自动切片上传**:根据弹幕密度计算寻找高能片段并切片,该自动切片工具库已开源 [auto-slice-video](https://github.com/timerring/auto-slice-video)
38-
,结合多模态视频理解大模型 [`GLM-4V-PLUS`](https://bigmodel.cn/dev/api/normal-model/glm-4) 自动生成有意思的切片标题及内容,并且自动上传。
38+
,结合多模态视频理解大模型 [`GLM-4V-PLUS`](https://bigmodel.cn/dev/api/normal-model/glm-4) 或者 [`Gemini-2.0-flash`](https://deepmind.google/technologies/gemini/flash/) 自动生成有意思的切片标题及内容,并且自动上传。
3939
- **( :tada: NEW)持久化登录/下载/上传视频(支持多p投稿)**[bilitool](https://github.com/timerring/bilitool)已经开源,实现持久化登录,下载视频及弹幕(含多p)/上传视频(可分p投稿),查询投稿状态,查询详细信息等功能,一键pip安装,可以使用命令行 cli 操作,也可以作为api调用。
4040
- **( :tada: NEW)自动多平台循环直播推流**:该工具已经开源 [looplive](https://github.com/timerring/looplive) 是一个 7 x 24 小时全自动**循环多平台同时推流**直播工具。
4141

@@ -137,21 +137,36 @@ pip install -r requirements.txt
137137
./setPath.sh && source ~/.bashrc
138138
```
139139

140-
#### 3. 配置 whisper 模型及 GLM-4V-PLUS 模型
140+
#### 3. 配置 whisper 模型及 MLLM 模型
141141

142-
##### 3.1 whisper 模型
142+
##### 3.1 whisper 模型(字幕识别)
143143
项目默认采用 [`small`](https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt) 模型,请点击下载所需文件,并放置在 `src/subtitle/models` 文件夹中。
144144

145145
> [!TIP]
146146
> 使用该参数模型至少需要保证有显存大于 2.7GB 的 GPU,否则请使用其他参数量的模型。
147147
> + 更多模型请参考 [whisper 参数模型](https://timerring.github.io/bilive/models.html) 部分。
148148
> + 更换模型方法请参考 [更换模型方法](https://timerring.github.io/bilive/models.html#更换模型方法) 部分。
149149
150-
##### 3.2 GLM-4V-PLUS 模型
150+
##### 3.2 MLLM 模型
151151

152-
> 此功能默认关闭,如果需要打开请将 `src/config.py` 文件中的 `AUTO_SLICE` 参数设置为 `True`
152+
MLLM 模型主要用于自动切片后的切片标题生成,此功能默认关闭,如果需要打开请将 `src/config.py` 文件中的 `AUTO_SLICE` 参数设置为 `True`。其他配置分别有:
153+
- `SLICE_DURATION` 以秒为单位设置切片时长(不建议超过 60 秒)。
154+
- `SLICE_NUM` 设置切片数量。
155+
- `SLICE_OVERLAP` 设置切片重叠时长。切片采用滑动窗口法处理,细节内容请见 [auto-slice-video](https://github.com/timerring/auto-slice-video)
156+
- `SLICE_STEP` 设置切片步长。
157+
- `MIN_VIDEO_SIZE` 设置切片最小视频大小。防止对一些连线或者网络波动原因造成的短片段再切片。
153158

154-
在配置文件 `src/config.py` 中,`SLICE_DURATION` 以秒为单位设置切片时长(不建议超过 1 分钟),在项目的自动切片功能需要使用到智谱的 [`GLM-4V-PLUS`](https://bigmodel.cn/dev/api/normal-model/glm-4) 模型,请自行[注册账号](https://www.bigmodel.cn/invite?icode=shBtZUfNE6FfdMH1R6NybGczbXFgPRGIalpycrEwJ28%3D)并申请 API Key,填写到 `src/config.py` 文件中对应的 `Your_API_KEY` 中。
159+
##### 3.2.1 GLM-4V-PLUS 模型
160+
161+
> 如需使用 GLM-4V-PLUS 模型,请将 `src/config.py` 文件中的 `MLLM_MODEL` 参数设置为 `zhipu`
162+
163+
在项目的自动切片功能需要使用到智谱的 [`GLM-4V-PLUS`](https://bigmodel.cn/dev/api/normal-model/glm-4) 模型,请自行[注册账号](https://www.bigmodel.cn/invite?icode=shBtZUfNE6FfdMH1R6NybGczbXFgPRGIalpycrEwJ28%3D)并申请 API Key,填写到 `src/config.py` 文件中对应的 `ZHIPU_API_KEY` 中。
164+
165+
##### 3.2.2 Gemini 模型
166+
167+
> 如需使用 Gemini-2.0-flash 模型,请将 `src/config.py` 文件中的 `MLLM_MODEL` 参数设置为 `gemini`
168+
169+
在项目的自动切片功能需要使用到 Gemini-2.0-flash 模型,请自行[注册账号](https://aistudio.google.com/app/apikey)并申请 API Key,填写到 `src/config.py` 文件中对应的 `GEMINI_API_KEY` 中。
155170

156171
#### 4. bilitool 登录
157172

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from google import genai
2+
from google.genai import types
3+
from src.log.logger import scan_log
4+
from src.config import GEMINI_API_KEY
5+
6+
def gemini_generate_title(video_path, artist):
7+
8+
client = genai.Client(api_key=GEMINI_API_KEY)
9+
10+
# Only for videos of size <20Mb
11+
video_bytes = open(video_path, 'rb').read()
12+
13+
response = client.models.generate_content(
14+
model='models/gemini-2.0-flash',
15+
contents=types.Content(
16+
parts=[
17+
types.Part(text=f'视频是{artist}的直播的切片,请根据该视频中的内容及弹幕信息,为这段视频起一个调皮并且吸引眼球的标题,只需要返回一个标题即可,无需返回其他内容'),
18+
types.Part(
19+
inline_data=types.Blob(data=video_bytes, mime_type='video/mp4')
20+
)
21+
]
22+
)
23+
)
24+
scan_log.info("使用 Gemini-2.0-flash 生成切片标题")
25+
scan_log.info(f"Prompt: 视频是{artist}的直播的切片,请根据该视频中的内容及弹幕信息,为这段视频起一个调皮并且吸引眼球的标题,只需要返回一个标题即可,无需返回其他内容")
26+
scan_log.info(f"生成的切片标题为: {response.text}")
27+
return response.text
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Copyright (c) 2024 bilive.
22

33
import base64
4-
from src.config import Your_API_KEY
4+
from src.config import ZHIPU_API_KEY
55
from zhipuai import ZhipuAI
66
from src.log.logger import scan_log
77

88
def zhipu_glm_4v_plus_generate_title(video_path, artist):
99
with open(video_path, 'rb') as video_file:
1010
video_base = base64.b64encode(video_file.read()).decode('utf-8')
1111

12-
client = ZhipuAI(api_key=Your_API_KEY)
12+
client = ZhipuAI(api_key=ZHIPU_API_KEY)
1313
response = client.chat.completions.create(
1414
model="glm-4v-plus-0111",
1515
messages=[
@@ -30,6 +30,7 @@ def zhipu_glm_4v_plus_generate_title(video_path, artist):
3030
}
3131
]
3232
)
33+
scan_log.info("使用 Zhipu-glm-4v-plus 生成切片标题")
3334
scan_log.info(f"Prompt: 视频是{artist}的直播的切片,请根据该视频中的内容及弹幕信息,为这段视频起一个调皮并且吸引眼球的标题,注意标题中如果有“主播”请替换成{artist}")
3435
scan_log.info(f"生成的切片标题为: {response.choices[0].message.content}")
3536
return response.choices[0].message.content.replace("《", "").replace("》", "")

src/autoslice/title_generator.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from functools import wraps
2+
from src.log.logger import scan_log
3+
from src.config import MLLM_MODEL
4+
5+
def title_generator(model_type):
6+
"""Decorator to select title generation function based on model type
7+
Args:
8+
model_type: str, type of model to use
9+
Returns:
10+
function: wrapped title generation function
11+
"""
12+
def decorator(func):
13+
def wrapper(video_path, artist):
14+
if model_type == "zhipu":
15+
from .mllm_sdk.zhipu_sdk import zhipu_glm_4v_plus_generate_title
16+
return zhipu_glm_4v_plus_generate_title(video_path, artist)
17+
elif model_type == "gemini":
18+
from .mllm_sdk.gemini_sdk import gemini_generate_title
19+
return gemini_generate_title(video_path, artist)
20+
else:
21+
scan_log.error(f"Unsupported model type: {model_type}")
22+
return None
23+
return wrapper
24+
return decorator
25+
26+
@title_generator(MLLM_MODEL)
27+
def generate_title(video_path, artist):
28+
"""Generate title for video
29+
Args:
30+
video_path: str, path to the video file
31+
artist: str, artist name
32+
Returns:
33+
str: generated title
34+
"""
35+
pass # The actual implementation is handled by the decorator

src/burn/render_video.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from src.burn.render_command import render_command
1010
from autoslice import slice_video_by_danmaku
1111
from src.autoslice.inject_metadata import inject_metadata
12-
from src.autoslice.zhipu_sdk import zhipu_glm_4v_plus_generate_title
12+
from src.autoslice.title_generator import generate_title
1313
from src.upload.extract_video_info import get_video_info
1414
from src.log.logger import scan_log
1515
from db.conn import insert_upload_queue
@@ -66,9 +66,9 @@ def render_video(video_path):
6666
slices_path = slice_video_by_danmaku(ass_path, format_video_path, SLICE_DURATION, SLICE_NUM, SLICE_OVERLAP, SLICE_STEP)
6767
for slice_path in slices_path:
6868
try:
69-
glm_title = zhipu_glm_4v_plus_generate_title(slice_path, artist)
69+
slice_title = generate_title(slice_path, artist)
7070
slice_video_flv_path = slice_path[:-4] + '.flv'
71-
inject_metadata(slice_path, glm_title, slice_video_flv_path)
71+
inject_metadata(slice_path, slice_title, slice_video_flv_path)
7272
os.remove(slice_path)
7373
if not insert_upload_queue(slice_video_flv_path):
7474
scan_log.error('Cannot insert the video to the upload queue')

src/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727
SLICE_STEP = 1
2828
# The minimum video size to be sliced (MB)
2929
MIN_VIDEO_SIZE = 200
30+
# the multi-model LLMs, can be "gemini" or "zhipu"
31+
MLLM_MODEL = "gemini" # Please make sure you have the right API key for the LLM you choose
3032
# Apply for your own GLM-4v-Plus API key at https://www.bigmodel.cn/invite?icode=shBtZUfNE6FfdMH1R6NybGczbXFgPRGIalpycrEwJ28%3D
31-
Your_API_KEY = ""
33+
ZHIPU_API_KEY = ""
34+
# Apply for your own Gemini API key at https://aistudio.google.com/app/apikey
35+
GEMINI_API_KEY = ""
3236
# ============================ Basic configuration ============================
3337
SRC_DIR = str(Path(os.path.abspath(__file__)).parent)
3438
BILIVE_DIR = str(Path(SRC_DIR).parent)

0 commit comments

Comments
 (0)