Skip to content

Commit fe7232c

Browse files
authored
feat: add qwen mllm (#251)
* feat: add qwen mllm * dosc: update docs
1 parent c3aeece commit fe7232c

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<img src="assets/openai.svg" alt="OpenAI whisper" width="60" height="60" />
1616
<img src="assets/zhipu-color.svg" alt="Zhipu GLM-4V-PLUS" width="60" height="60" />
1717
<img src="assets/gemini-brand-color.svg" alt="Google Gemini 1.5 Pro" width="60" height="60" />
18+
<img src="assets/qwen-color.svg" alt="Qwen-2.5-72B-Instruct" width="60" height="60" />
1819

1920
</div>
2021

@@ -34,8 +35,10 @@
3435
- **自动渲染弹幕**:自动转换xml为ass弹幕文件,该转换工具库已经开源 [DanmakuConvert](https://github.com/timerring/DanmakuConvert) 并且渲染到视频中形成**有弹幕版视频**并自动上传。
3536
- **硬件要求极低**:无需GPU,只需最基础的单核CPU搭配最低的运存即可完成录制,弹幕渲染,上传等等全部过程,无最低配置要求,10年前的电脑或服务器依然可以使用!
3637
- **( :tada: NEW)自动渲染字幕**(如需使用本功能,则需保证有 Nvidia 显卡):采用 OpenAI 的开源模型 [`whisper`](https://github.com/openai/whisper),自动识别视频内语音并转换为字幕渲染至视频中。
37-
- **( :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) 或者 [`Gemini-2.0-flash`](https://deepmind.google/technologies/gemini/flash/) 自动生成有意思的切片标题及内容,并且自动上传。
38+
- **( :tada: NEW)自动切片上传**:根据弹幕密度计算寻找高能片段并切片,该自动切片工具库已开源 [auto-slice-video](https://github.com/timerring/auto-slice-video),结合多模态视频理解大模型自动生成有意思的切片标题及内容,并且自动上传,目前已经支持的模型有:
39+
- `GLM-4V-PLUS`
40+
- `Gemini-2.0-flash`
41+
- `Qwen-2.5-72B-Instruct`
3942
- **( :tada: NEW)持久化登录/下载/上传视频(支持多p投稿)**[bilitool](https://github.com/timerring/bilitool)已经开源,实现持久化登录,下载视频及弹幕(含多p)/上传视频(可分p投稿),查询投稿状态,查询详细信息等功能,一键pip安装,可以使用命令行 cli 操作,也可以作为api调用。
4043
- **( :tada: NEW)自动多平台循环直播推流**:该工具已经开源 [looplive](https://github.com/timerring/looplive) 是一个 7 x 24 小时全自动**循环多平台同时推流**直播工具。
4144

@@ -70,8 +73,6 @@ graph TD
7073

7174
## 3. 测试硬件
7275
+ OS: Ubuntu 22.04.4 LTS
73-
74-
>尽量使用 22.04+ 的版本,更早版本的 ubuntu 自带 gcc 版本无法更新至 biliup-rs 所需版本,若使用较早版本,请参考 [version `GLIBC_2.34‘ not found简单有效解决方法](https://blog.csdn.net/huazhang_001/article/details/128828999)
7576
+ CPU:2核 Intel(R) Xeon(R) Platinum 85
7677
+ GPU:无
7778
+ 内存:2G
@@ -168,6 +169,12 @@ MLLM 模型主要用于自动切片后的切片标题生成,此功能默认关
168169
169170
在项目的自动切片功能需要使用到 Gemini-2.0-flash 模型,请自行[注册账号](https://aistudio.google.com/app/apikey)并申请 API Key,填写到 `src/config.py` 文件中对应的 `GEMINI_API_KEY` 中。
170171

172+
##### 3.2.3 Qwen 模型
173+
174+
> 如需使用 Qwen-2.5-72B-Instruct 模型,请将 `src/config.py` 文件中的 `MLLM_MODEL` 参数设置为 `qwen`
175+
176+
在项目的自动切片功能需要使用到 Qwen-2.5-72B-Instruct 模型,请自行[注册账号](https://bailian.console.aliyun.com/?apiKey=1)并申请 API Key,填写到 `src/config.py` 文件中对应的 `QWEN_API_KEY` 中。
177+
171178
#### 4. bilitool 登录
172179

173180
> 由于一般日志打印不出二维码效果(docker 的日志不确定是否能打印,等发布新image时再修改,docker 版本请先参考文档[bilive](https://bilive.timerring.com),本 README 只针对源码部署),所以这步需要提前在机器上安装 [bilitool](https://github.com/timerring/bilitool):

assets/qwen-color.svg

Lines changed: 1 addition & 0 deletions
Loading

src/autoslice/mllm_sdk/qwen_sdk.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from src.config import QWEN_API_KEY
2+
from src.log.logger import scan_log
3+
from openai import OpenAI
4+
import os
5+
import base64
6+
7+
def encode_video(video_path):
8+
with open(video_path, "rb") as video_file:
9+
return base64.b64encode(video_file.read()).decode("utf-8")
10+
11+
def qwen_generate_title(video_path, artist):
12+
client = OpenAI(
13+
api_key=QWEN_API_KEY,
14+
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
15+
)
16+
17+
base64_video = encode_video(video_path)
18+
completion = client.chat.completions.create(
19+
model="qwen2.5-vl-72b-instruct",
20+
messages=[
21+
{
22+
"role": "system",
23+
"content": [{"type":"text","text": "你是一个视频切片员"}]},
24+
{
25+
"role": "user",
26+
"content": [
27+
{
28+
"type": "video_url",
29+
"video_url": {"url": f"data:video/mp4;base64,{base64_video}"},
30+
},
31+
{"type": "text", "text": f"视频是{artist}的直播切片,请根据该视频中的内容及弹幕信息,为这段视频起一个调皮并且吸引眼球的标题,标题中不要表情符号,可以适当使用网络热词或流行语"},
32+
],
33+
}
34+
],
35+
)
36+
scan_log.info("使用 Qwen-2.5-72B-Instruct 生成切片标题")
37+
scan_log.info(f"Prompt: 视频是{artist}的直播切片,请根据该视频中的内容及弹幕信息,为这段视频起一个调皮并且吸引眼球的标题,标题中不要表情符号,可以适当使用网络热词或流行语")
38+
scan_log.info(f"生成的切片标题为: {completion.choices[0].message.content.strip('"')}")
39+
return completion.choices[0].message.content.strip('"')

src/autoslice/title_generator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def wrapper(video_path, artist):
1717
elif model_type == "gemini":
1818
from .mllm_sdk.gemini_sdk import gemini_generate_title
1919
return gemini_generate_title(video_path, artist)
20+
elif model_type == "qwen":
21+
from .mllm_sdk.qwen_sdk import qwen_generate_title
22+
return qwen_generate_title(video_path, artist)
2023
else:
2124
scan_log.error(f"Unsupported model type: {model_type}")
2225
return None

src/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
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"
30+
# the multi-model LLMs, can be "gemini" or "zhipu" or "qwen"
3131
MLLM_MODEL = "gemini" # Please make sure you have the right API key for the LLM you choose
3232
# Apply for your own GLM-4v-Plus API key at https://www.bigmodel.cn/invite?icode=shBtZUfNE6FfdMH1R6NybGczbXFgPRGIalpycrEwJ28%3D
3333
ZHIPU_API_KEY = ""
3434
# Apply for your own Gemini API key at https://aistudio.google.com/app/apikey
3535
GEMINI_API_KEY = ""
36+
# Apply for your own Qwen API key at https://bailian.console.aliyun.com/?apiKey=1
37+
QWEN_API_KEY = ""
3638
# ============================ Basic configuration ============================
3739
SRC_DIR = str(Path(os.path.abspath(__file__)).parent)
3840
BILIVE_DIR = str(Path(SRC_DIR).parent)

0 commit comments

Comments
 (0)