|
@@ -11,6 +11,8 @@ import re
|
|
|
import os
|
|
|
import shutil
|
|
|
import json
|
|
|
+import time
|
|
|
+
|
|
|
from config import get_logger
|
|
|
import requests
|
|
|
import base64
|
|
@@ -71,30 +73,34 @@ def get_voice(content, is_local, silence_duration):
|
|
|
"silence_duration":silence_duration
|
|
|
}
|
|
|
}
|
|
|
- try:
|
|
|
- resp = requests.post(api_url, json.dumps(request_json), headers=header)
|
|
|
- rfile =f"{rid}.wav"
|
|
|
- path, file=f"{VOICE_DIR}/{rfile}", f"../voice/{rfile}"
|
|
|
- if "data" in resp.json():
|
|
|
- data = resp.json()["data"]
|
|
|
- file_to_save = open(file, "wb")
|
|
|
- file_to_save.write(base64.b64decode(data))
|
|
|
- if is_local:
|
|
|
- if os.path.exists(file):
|
|
|
- logger.info(f"voice local file ::session_id={rid}, res={file}")
|
|
|
- return path
|
|
|
- else:
|
|
|
- files = {'file': open(file, 'rb')}
|
|
|
- response = requests.post(DEFAULT_FILE_UPLOAD_URL, files=files)
|
|
|
- if response.ok:
|
|
|
- result = json.loads(response.text)
|
|
|
- url = result.get('data')
|
|
|
+ retry = 1
|
|
|
+ while retry <=3:
|
|
|
+ try:
|
|
|
+ resp = requests.post(api_url, json.dumps(request_json), headers=header)
|
|
|
+ rfile = f"{rid}.wav"
|
|
|
+ path, file = f"{VOICE_DIR}/{rfile}", f"../voice/{rfile}"
|
|
|
+ if "data" in resp.json():
|
|
|
+ data = resp.json()["data"]
|
|
|
+ file_to_save = open(file, "wb")
|
|
|
+ file_to_save.write(base64.b64decode(data))
|
|
|
+ if is_local:
|
|
|
if os.path.exists(file):
|
|
|
- os.system(f"rm -fr {file}")
|
|
|
- logger.info(f"voice upload_plot::session_id={rid}, res={url}")
|
|
|
- return url
|
|
|
- except Exception as e:
|
|
|
- logger.info(f"voice generate 错误{e}")
|
|
|
+ logger.info(f"voice local file ::session_id={rid}, res={file}")
|
|
|
+ return path
|
|
|
+ else:
|
|
|
+ files = {'file': open(file, 'rb')}
|
|
|
+ response = requests.post(DEFAULT_FILE_UPLOAD_URL, files=files)
|
|
|
+ if response.ok:
|
|
|
+ result = json.loads(response.text)
|
|
|
+ url = result.get('data')
|
|
|
+ if os.path.exists(file):
|
|
|
+ os.system(f"rm -fr {file}")
|
|
|
+ logger.info(f"voice upload_plot::session_id={rid}, res={url}")
|
|
|
+ return url
|
|
|
+ except Exception as e:
|
|
|
+ retry +=1
|
|
|
+ time.sleep(2)
|
|
|
+ logger.info(f"voice generate 错误{e} retry:{retry}")
|
|
|
|
|
|
@timetic
|
|
|
def voice_service(content, local=False, silence_duration="125"):
|