fish_reference_audio_sync.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from typing import List, Dict, Any
  2. import pandas as pd
  3. from tabulate import tabulate
  4. from client.FishClient import FishClient
  5. from helper.MySQLHelper import MySQLHelper
  6. from util import feishu_inform_util
  7. official_fish_client = FishClient("https://api.fish.audio")
  8. mysql_helper = MySQLHelper(
  9. host="rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com",
  10. username="readonly",
  11. password="HdkZ4TDmeK6SQ3BRtJBk",
  12. database="aigc-admin-prod"
  13. )
  14. webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/c09712a8-22cd-4bfa-93a5-30ae7b1db11b"
  15. def get_fish_pq_ip() -> List[str]:
  16. sql = "select * from base_config where config_key = 'fish_pq_ip_list';"
  17. result = mysql_helper.execute_query(sql)
  18. if not result:
  19. return []
  20. value = result[0]['config_value']
  21. return value.split(',')
  22. def get_all_reference_by_db() -> List[Dict[str, Any]]:
  23. sql = "select * from ai_model_tts where model = 33;"
  24. return mysql_helper.execute_query(sql)
  25. def build_card_json(msg: str):
  26. is_success = True if msg else False
  27. return {
  28. "config": {},
  29. "i18n_elements": {
  30. "zh_cn": [
  31. {
  32. "tag": "markdown",
  33. "content": "",
  34. "text_align": "left",
  35. "text_size": "normal"
  36. },
  37. {
  38. "tag": "markdown",
  39. "content": f"```\n{msg}\n```" if is_success else "全部同步成功",
  40. "text_align": "left",
  41. "text_size": "normal"
  42. }
  43. ]
  44. },
  45. "i18n_header": {
  46. "zh_cn": {
  47. "title": {
  48. "tag": "plain_text",
  49. "content": "Fish音频同步完成通知"
  50. },
  51. "subtitle": {
  52. "tag": "plain_text",
  53. "content": ""
  54. },
  55. "template": "green" if True else "yellow"
  56. }
  57. }
  58. }
  59. def _main():
  60. db_all_reference = get_all_reference_by_db()
  61. reference_id_and_text_map = {}
  62. all_ip = get_fish_pq_ip()
  63. print(f"当前配置的Fish服务器IP列表为: {all_ip}")
  64. sync_fail_list = []
  65. for ip in all_ip:
  66. print(f"开始将音频同步到实例【{ip}】")
  67. fish_client = FishClient(f"http://{ip}:8080")
  68. exist_references_ids = fish_client.get_all_references_id()
  69. for reference_info in db_all_reference:
  70. reference_id = reference_info['speaker_id']
  71. try:
  72. if reference_id in exist_references_ids:
  73. print(f"音频ID【{reference_id}】在实例【{ip}】上已经存在,跳过")
  74. continue
  75. if reference_id not in reference_id_and_text_map:
  76. model_info = official_fish_client.get_model_info_by_id(reference_id)
  77. text = model_info['samples'][0]['text']
  78. reference_id_and_text_map[reference_id] = text
  79. audio_url = reference_info['audio_url']
  80. reference_text = reference_id_and_text_map[reference_id]
  81. fish_client.add_reference_id_by_url(reference_id=reference_id, reference_text=reference_text, audio_url=audio_url)
  82. print(f"音频ID【{reference_id}】同步到实例【{ip}】上完成")
  83. except Exception as e:
  84. print(f"音频ID【{reference_id}】同步到实例【{ip}】上异常 {str(e)}")
  85. sync_fail_list.append({
  86. "实例IP": ip,
  87. "音频ID": reference_id,
  88. "失败原因": str(e),
  89. })
  90. print(f"将音频同步到实例【{ip}】完成")
  91. # 同步失败的告警通知
  92. df = pd.DataFrame(sync_fail_list)
  93. msg = tabulate(df, headers='keys', tablefmt='grid', showindex=False)
  94. print("同步失败的音频信息")
  95. print(msg)
  96. print("=" * 300)
  97. feishu_inform_util.send_card_msg_to_feishu(
  98. webhook=webhook_url,
  99. card_json=build_card_json(msg)
  100. )
  101. if __name__ == '__main__':
  102. _main()