ChatGPT + Stable Diffusion + 百度AI + MoviePy 實(shí)現(xiàn)文字生成視頻,小說轉(zhuǎn)視頻,自媒體神器!(一)
前言
最近大模型頻出,但是對(duì)于我們普通人來說,如何使用這些AI工具來輔助我們的工作呢,或者參與進(jìn)入我們的生活,就著現(xiàn)在比較熱門的幾個(gè)AI,寫個(gè)一個(gè)提高生產(chǎn)力工具,現(xiàn)在在邏輯上已經(jīng)走通了,后面會(huì)針對(duì)web頁面、后臺(tái)進(jìn)行優(yōu)化。
github鏈接 https://github.com/Anning01/TextCreateVideo
B站教程視頻 https://www.bilibili.com/video/BV18M4y1H7XN/
那么從一個(gè)用戶輸入文本到生成視頻,我分成了五個(gè)步驟來做。
其中2、3 和 4 沒有關(guān)系,后期做成異步并行。
第一步、將用戶輸入的文本進(jìn)行段落切割。
我這里默認(rèn)用戶輸入的為txt文件,也是建議一章一章來,太大并不是不可以執(zhí)行,只是時(shí)間上耗費(fèi)太多,當(dāng)然4080用戶除外!
from config import file_path
class Main:
def txt_handle(self, filepath):
"""
txt文件處理
:return:
"""
file = open(file_path + filepath, 'r')
content = file.read().replace('\n', '')
return content.split('。')
這里比較簡單,現(xiàn)在也沒有做前端頁面,現(xiàn)在將文件放在指定的目錄下,會(huì)將txt文件按照中文“?!眮砬衅?。后期考慮有傳整本的需求,會(huì)加上數(shù)據(jù)庫進(jìn)行持久化,按照章節(jié)區(qū)分,按章節(jié)來生成視頻。
第二步、使用chatGPT生成提示詞
我ChatGPT的免費(fèi)調(diào)用API次數(shù)沒了,最優(yōu)選肯定是原生調(diào)用ChatGPT的api,但是沒有這個(gè)條件,我選擇了一些提供ChatGPT的API中間商
fastapi 和 API2D文章來源:http://www.zghlxwxcb.cn/news/detail-630024.html
from SDK.ChatGPT.FastGPT.app import Main as FM
from SDK.ChatGPT.API2D.app import Main as AM
from config import apikey, appId, ForwardKey
class Main:
# 默認(rèn)反向提升詞
negative = "NSFW,sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,(long hair:1.4),DeepNegative,(fat:1.2),facing away, looking away,tilted head, {Multiple people}, lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms, extra leg, extra foot,"
# 默認(rèn)提示詞
prompt = "best quality,masterpiece,illustration, an extremely delicate and beautiful,extremely detailed,CG,unity,8k wallpaper, "
def create_prompt_words(self, text_list: list):
"""
生成英文提示詞
:return: [{prompt, negative, text, index},...]
"""
# 包含著 坐標(biāo)、英文提示詞、英文反向提示詞、中文文本 列表
data = []
instance_class_list = []
if all([apikey, appId]):
instance_class_list.append(FM())
if ForwardKey:
instance_class_list.append(AM())
for index, value in enumerate(text_list):
prompt = instance_class_list[0].prompt_generation_chatgpt(value)
if not prompt:
if len(instance_class_list) >= 1:
instance_class_list.pop(0)
prompt = instance_class_list[0].prompt_generation_chatgpt(value)
if not prompt:
print("------fastgpt和API2D都無法使用---------")
raise Exception("請(qǐng)檢查代碼")
else:
print("------fastgpt和API2D都無法使用---------")
raise Exception("請(qǐng)檢查代碼")
print(f"-----------生成第{index}段提示詞-----------")
data.append({
"index": index,
"text": value,
"prompt": self.prompt + prompt,
"negative": self.negative,
})
return data
我將兩個(gè)api接口做成插件式的,并且保證一個(gè)壞了可以去使用另一個(gè)文章來源地址http://www.zghlxwxcb.cn/news/detail-630024.html
fastGPT
class Main:
apikey = apikey
appId = appId
url = "https://fastgpt.run/api/openapi/v1/chat/completions"
def prompt_generation_chatgpt(self, param):
# 發(fā)送HTTP POST請(qǐng)求
headers = {
'Content-Type': 'application/json',
'User-Agent': 'Apifox/1.0.0 (https://www.apifox.cn)',
'Authorization': f'Bearer {self.apikey}-{self.appId}'
}
data = {
"stream": False,
# "chatId": "3232",
"messages": [
{
"content": '根據(jù)下面的內(nèi)容描述,生成一副畫面并用英文單詞表示:' + param,
"role": "user"
}
]
}
json_data = json.dumps(data)
# 發(fā)送HTTP POST請(qǐng)求
response = requests.post(self.url, data=json_data, headers=headers)
result_json = json.loads(response.text)
if response.status_code != 200:
print("-----------FastAPI出錯(cuò)了-----------")
return False
# 輸出結(jié)果
return result_json['responseData'][0]['answer']
API2D
import requests
from config import ForwardKey
class Main:
ForwardKey = ForwardKey
url = "https://openai.api2d.net/v1/chat/completions"
def prompt_generation_chatgpt(self, param):
# 發(fā)送HTTP POST請(qǐng)求
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {ForwardKey}'
# <-- 把 fkxxxxx 替換成你自己的 Forward Key,注意前面的 Bearer 要保留,并且和 Key 中間有一個(gè)空格。
}
data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": '根據(jù)下面的內(nèi)容描述,生成一副畫面并用英文單詞表示:' + param, }]
}
response = requests.post(self.url, headers=headers, json=data)
print("-----------進(jìn)入API2D-----------")
if response.status_code != 200:
return False
# 發(fā)送HTTP POST請(qǐng)求
result_json = response.json()
# 輸出結(jié)果
return result_json["choices"][0]["message"]["content"]
到了這里,關(guān)于ChatGPT + Stable Diffusion + 百度AI + MoviePy 實(shí)現(xiàn)文字生成視頻,小說轉(zhuǎn)視頻,自媒體神器!(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!