Skip to content

Commit 944c53c

Browse files
authored
feat: stable diffusion (#265)
* feat: stable diffusion * docs: update docs
1 parent c3777fc commit 944c53c

File tree

7 files changed

+94
-19
lines changed

7 files changed

+94
-19
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<img src="assets/siliconcloud-color.svg" alt="SiliconFlow" width="15" height="60" />
2323
<img src="assets/siliconcloud-text.svg" alt="SiliconFlow" width="100" height="60" />
2424
<img src="assets/wenxin-color.svg" alt="Baidu ERNIE" width="60" height="60" />
25+
<img src="assets/stability-brand-color.svg" alt="Stability AI" width="80" height="60" />
2526
</div>
2627

2728
## 1. Introduction
@@ -50,7 +51,8 @@
5051
- `Minimax image-01`
5152
- `Kwai Kolors`
5253
- `Tencent Hunyuan`
53-
- `Baidu ERNIE`
54+
- `Baidu ERNIE irag-1.0`
55+
- `Stable Diffusion 3.5 large turbo`
5456

5557
项目架构流程如下:
5658

@@ -223,6 +225,12 @@ MLLM 模型主要用于自动切片后的切片标题生成,此功能默认关
223225
224226
请自行[注册账号](https://console.bce.baidu.com/iam/key/list)并申请 API Key,填写到 `bilive.toml` 文件中对应的 `BAIDU_API_KEY` 中。
225227

228+
##### 3.3.5 Stability SD 3.5 large turbo 模型
229+
230+
> 如需使用 Stability SD 3.5 large turbo 模型,请将 `IMAGE_GEN_MODEL` 参数设置为 `stability`
231+
232+
请自行[注册账号](https://platform.stability.ai/account/keys)并申请 API Key,填写到 `bilive.toml` 文件中对应的 `STABILITY_API_KEY` 中。
233+
226234
#### 4. bilitool 登录
227235

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

assets/stability-brand-color.svg

Lines changed: 1 addition & 0 deletions
Loading

bilive.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ qwen_api_key = "" # Apply for your own Qwen API key at https://bailian.console.a
3737

3838
[cover]
3939
generate_cover = false # whether to generate cover
40-
image_gen_model = "minimax" # the image generation model, can be "minimax" or "siliconflow" or "tencent" or "baidu"
40+
image_gen_model = "minimax" # the image generation model, can be "minimax" or "siliconflow" or "tencent" or "baidu" or "stability"
4141
minimax_api_key = "" # Apply for your own Minimax API key at https://platform.minimaxi.com/user-center/basic-information/interface-key
4242
siliconflow_api_key = "" # Apply for your own SiliconFlow API key at https://cloud.siliconflow.cn/i/3Szr5BVg
4343
tencent_secret_id = "" # Apply for your own Tencent Cloud API key at https://console.cloud.tencent.com/cam/capi
4444
tencent_secret_key = "" # Apply for your own Tencent Cloud secret key as above
4545
baidu_api_key = "" # Apply for your own Baidu API key at https://console.bce.baidu.com/iam/key/list
46+
stability_api_key = "" # Apply for your own Stability API key at https://platform.stability.ai/account/keys

src/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@ def get_interface_config():
7878
SILICONFLOW_API_KEY = config.get('cover', {}).get('siliconflow_api_key')
7979
TENCENT_SECRET_ID = config.get('cover', {}).get('tencent_secret_id')
8080
TENCENT_SECRET_KEY = config.get('cover', {}).get('tencent_secret_key')
81-
BAIDU_API_KEY = config.get('cover', {}).get('baidu_api_key')
81+
BAIDU_API_KEY = config.get('cover', {}).get('baidu_api_key')
82+
STABILITY_API_KEY = config.get('cover', {}).get('stability_api_key')

src/cover/cover_generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def wrapper(video_path):
6767
from .image_model_sdk.baidu_sdk import baidu_generate_cover
6868

6969
return baidu_generate_cover(cover_path)
70+
elif model_type == "stability":
71+
from .image_model_sdk.stability_sdk import (
72+
stable_diffusion_generate_cover,
73+
)
74+
75+
return stable_diffusion_generate_cover(cover_path)
7076
else:
7177
upload_log.error(f"Unsupported model type: {model_type}")
7278
return None

src/cover/image_model_sdk/baidu_sdk.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from src.upload.bilitool.bilitool.model.model import Model
99
from src.config import BAIDU_API_KEY
1010

11+
1112
def cover_up(img: str):
1213
"""Upload the cover image
1314
Parameters
@@ -20,14 +21,15 @@ def cover_up(img: str):
2021
"""
2122
from PIL import Image
2223
from io import BytesIO
24+
2325
request = requests.Session()
2426
request.headers = {
25-
'user-agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/63.0.3239.108",
26-
'referer': "https://www.bilibili.com/",
27-
'connection': 'keep-alive'
27+
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/63.0.3239.108",
28+
"referer": "https://www.bilibili.com/",
29+
"connection": "keep-alive",
2830
}
2931
model = Model().get_config()
30-
request.cookies.set('SESSDATA', model['cookies']['SESSDATA'])
32+
request.cookies.set("SESSDATA", model["cookies"]["SESSDATA"])
3133
with Image.open(img) as im:
3234
# you should keep the image ratio 16:10
3335
xsize, ysize = im.size
@@ -44,7 +46,7 @@ def cover_up(img: str):
4446
data={
4547
"cover": b"data:image/jpeg;base64,"
4648
+ (base64.b64encode(buffered.getvalue())),
47-
"csrf": model['cookies']['bili_jct']
49+
"csrf": model["cookies"]["bili_jct"],
4850
},
4951
timeout=30,
5052
)
@@ -55,24 +57,33 @@ def cover_up(img: str):
5557
print(res["data"]["url"], flush=True)
5658
return res["data"]["url"]
5759

60+
5861
def baidu_generate_cover(your_file_path):
62+
"""Generater cover image using baidu api
63+
Args:
64+
your_file_path: str, path to the image file
65+
Returns:
66+
str, local download path of the generated cover image file
67+
"""
5968
try:
6069
cover_url = cover_up(your_file_path)
6170

6271
url = "https://qianfan.baidubce.com/v2/images/generations"
63-
payload = json.dumps({
64-
"model": "irag-1.0",
65-
"prompt": "这是视频截图,请根据该图生成对应的动漫类型的封面",
66-
"refer_image": cover_url
67-
})
72+
payload = json.dumps(
73+
{
74+
"model": "irag-1.0",
75+
"prompt": "这是视频截图,请根据该图生成对应的动漫类型的封面",
76+
"refer_image": cover_url,
77+
}
78+
)
6879
headers = {
69-
'Content-Type': 'application/json',
70-
'Authorization': f'Bearer {BAIDU_API_KEY}'
80+
"Content-Type": "application/json",
81+
"Authorization": f"Bearer {BAIDU_API_KEY}",
7182
}
72-
83+
7384
response = requests.request("POST", url, headers=headers, data=payload)
7485
if response.status_code == 200:
75-
image_url=response.json()['data'][0]['url']
86+
image_url = response.json()["data"][0]["url"]
7687
img_data = requests.get(image_url).content
7788
cover_name = time.strftime("%Y%m%d%H%M%S") + ".png"
7889
temp_cover_path = os.path.join(os.path.dirname(your_file_path), cover_name)
@@ -87,5 +98,6 @@ def baidu_generate_cover(your_file_path):
8798
print(e, flush=True)
8899
return None
89100

90-
if __name__ == '__main__':
91-
print(baidu_generate_cover(""))
101+
102+
if __name__ == "__main__":
103+
print(baidu_generate_cover(""))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import requests
2+
from src.config import STABILITY_API_KEY
3+
import time
4+
import os
5+
6+
7+
def stable_diffusion_generate_cover(your_file_path):
8+
"""Generater cover image using stability api
9+
Args:
10+
image_path: str, path to the image file
11+
Returns:
12+
str, local download path of the generated cover image file
13+
"""
14+
15+
cover_name = time.strftime("%Y%m%d%H%M%S") + ".jpeg"
16+
temp_cover_path = os.path.join(os.path.dirname(your_file_path), cover_name)
17+
18+
with open(your_file_path, "rb") as img_file:
19+
response = requests.post(
20+
f"https://api.stability.ai/v2beta/stable-image/generate/sd3",
21+
headers={
22+
"authorization": f"Bearer {STABILITY_API_KEY}",
23+
"accept": "image/*",
24+
},
25+
files={"image": ("image.jpg", img_file, "image/jpeg")},
26+
data={
27+
"prompt": "This is a video screenshot, please generate a cover in the style of a manga", # English only
28+
"strength": 0.75,
29+
"output_format": "jpeg",
30+
"mode": "image-to-image",
31+
"model": "sd3.5-large-turbo",
32+
},
33+
)
34+
35+
if response.status_code == 200:
36+
with open(temp_cover_path, "wb") as file:
37+
file.write(response.content)
38+
os.remove(your_file_path)
39+
return temp_cover_path
40+
else:
41+
raise Exception(str(response.json()))
42+
return None
43+
44+
45+
if __name__ == "__main__":
46+
print(stable_diffusion_generate_cover(""))

0 commit comments

Comments
 (0)