Skip to content

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Anning01 committed Sep 6, 2023
1 parent 7fe42c1 commit d9c885a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
11 changes: 3 additions & 8 deletions SDK/Baidu/Voice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ def create_access_token(self):
json.dump(access_token, f)
return access_token

def get_access_token(self, token):
if self.client_id == "A4MCvofRU3qNBXCUWlcLAq9Q":
headers = {"Authorization": token, "Content-Type": "application/json"}
res = requests.get(f"http://8.134.91.58/baidu/access_token/", headers=headers)
if res.json().get("access_token"):
return res.json().get("access_token")
def get_access_token(self):
if os.path.exists('access_token.json'):
with open('access_token.json', 'r', encoding='utf-8') as f:
data = json.load(f)
Expand All @@ -49,12 +44,12 @@ def get_access_token(self, token):
return data
return self.create_access_token()

def text_to_audio(self, text: str, index: int, bookname=None, config=None, token=None):
def text_to_audio(self, text: str, index: int, bookname=None, config=None):
url = "https://tsn.baidu.com/text2audio"
text = text.encode('utf8')
FORMATS = {3: "mp3", 4: "pcm", 5: "pcm", 6: "wav"}
FORMAT = FORMATS[6]
access_token = self.get_access_token(token)
access_token = self.get_access_token()
data = {
# 合成的文本,文本长度必须小于1024GBK字节。建议每次请求文本不超过120字节,约为60个汉字或者字母数字。
"tex": text,
Expand Down
15 changes: 7 additions & 8 deletions admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ async def create_book(file: UploadFile = File(...), db: SessionLocal = Depends(g

@router.get("/book/create/{id}")
async def create_video(id, request: Request, db: SessionLocal = Depends(get_db)):
token = request.headers['authorization']
book = db.query(Book).get(id)
config = db.query(SystemConfig).first()
scene_tag = db.query(PromptTags).filter(PromptTags.book_id == book.id).all()
Expand Down Expand Up @@ -153,7 +152,7 @@ async def create_video(id, request: Request, db: SessionLocal = Depends(get_db))
book.status = StatusEnum.underway
db.commit()
t = threading.Thread(target=CreateVideo().thread_func,
args=[book, path, db, config, scene_tag, sd_config, prompt_dict, token])
args=[book, path, db, config, scene_tag, sd_config, prompt_dict])
t.daemon = True
t.start()
return success_data({}, message="视频生成任务启动成功!")
Expand All @@ -162,11 +161,11 @@ async def create_video(id, request: Request, db: SessionLocal = Depends(get_db))

class CreateVideo:

def thread_func(self, book, path, db, config, scene_tag, sd_config, prompt_dict, token):
def thread_func(self, book, path, db, config, scene_tag, sd_config, prompt_dict):
status = StatusEnum.complete
fail_info = ''
try:
self.main(book, path, db, config, scene_tag, sd_config, prompt_dict, token)
self.main(book, path, db, config, scene_tag, sd_config, prompt_dict)
except Exception as error:
status = StatusEnum.failure
fail_info = str(error)
Expand All @@ -176,7 +175,7 @@ def thread_func(self, book, path, db, config, scene_tag, sd_config, prompt_dict,
db.commit()
return

def main(self, book, path, db, config, scene_tag, sd_config, prompt_dict, token):
def main(self, book, path, db, config, scene_tag, sd_config, prompt_dict):
"""
启动此方法,异步生成图片,语音,提示词
:return:
Expand Down Expand Up @@ -226,7 +225,7 @@ def main(self, book, path, db, config, scene_tag, sd_config, prompt_dict, token)
db.commit()
config = db.merge(config)
# 生成音频任务
audio_list = self.text_to_audio(text_list, book.name, config, token)
audio_list = self.text_to_audio(text_list, book.name, config)
audio_model_list = []
for index, value in enumerate(audio_list):
audio_model_list.append(BookVoice(
Expand Down Expand Up @@ -281,13 +280,13 @@ def txt_handle(self, path):
text_list = t.txt_long(content.split('。'))
return t.txt_short(text_list)

def text_to_audio(self, text_list, bookname, config, token):
def text_to_audio(self, text_list, bookname, config):
"""
生成音频
:return:
"""
s = SMain()
audio_list = s.text_to_audio(text_list, bookname=bookname, config=config, token=token)
audio_list = s.text_to_audio(text_list, bookname=bookname, config=config)
return audio_list

def create_prompt_words(self, text_list, tags=None, prompt_dict=None):
Expand Down
4 changes: 2 additions & 2 deletions apps/SpeechSynthesis/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Main:

def text_to_audio(self, text_list: list, bookname=None, config=None, token=None):
def text_to_audio(self, text_list: list, bookname=None, config=None):
"""
文字转语音方法
:param text_list:
Expand All @@ -24,7 +24,7 @@ def text_to_audio(self, text_list: list, bookname=None, config=None, token=None)
print("----------text_to_speech 方法传入文本长度不能超过60个字----------")
print("-----------下个版本对60字以上自动做长文本转语音操作----------------")
m = M()
result = m.text_to_audio(value, index, bookname, config, token)
result = m.text_to_audio(value, index, bookname, config)
print(f"-----------生成第{index}段音频-----------")
if not result:
print("----------text_to_speech 百度语音库异常----------")
Expand Down

0 comments on commit d9c885a

Please sign in to comment.