fish_reference_audio_sync.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from typing import List, Dict, Any
  2. from client.FishClient import FishClient
  3. from helper.MySQLHelper import MySQLHelper
  4. official_fish_client = FishClient("https://api.fish.audio")
  5. mysql_helper = MySQLHelper(
  6. host="rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com",
  7. username="readonly",
  8. password="HdkZ4TDmeK6SQ3BRtJBk",
  9. database="aigc-admin-prod"
  10. )
  11. def get_fish_pq_ip() -> List[str]:
  12. sql = "select * from base_config where config_key = 'fish_pq_ip_list';"
  13. result = mysql_helper.execute_query(sql)
  14. if not result:
  15. return []
  16. value = result[0]['config_value']
  17. return value.split(',')
  18. def get_all_reference_by_db() -> List[Dict[str, Any]]:
  19. sql = "select * from ai_model_tts where model = 33;"
  20. return mysql_helper.execute_query(sql)
  21. def _main():
  22. db_all_reference = get_all_reference_by_db()
  23. reference_id_and_text_map = {}
  24. all_ip = get_fish_pq_ip()
  25. print(f"当前配置的Fish服务器IP列表为: {all_ip}")
  26. for ip in all_ip:
  27. print(f"开始将音频同步到实例【{ip}】")
  28. fish_client = FishClient(f"http://{ip}:8080")
  29. exist_references_ids = fish_client.get_all_references_id()
  30. for reference_info in db_all_reference:
  31. reference_id = reference_info['speaker_id']
  32. try:
  33. if reference_id in exist_references_ids:
  34. print(f"音频ID【{reference_id}】在实例【{ip}】上已经存在,跳过")
  35. continue
  36. if reference_id not in reference_id_and_text_map:
  37. model_info = official_fish_client.get_model_info_by_id(reference_id)
  38. text = model_info['samples'][0]['text']
  39. reference_id_and_text_map[reference_id] = text
  40. audio_url = reference_info['audio_url']
  41. reference_text = reference_id_and_text_map[reference_id]
  42. fish_client.add_reference_id_by_url(reference_id=reference_id, reference_text=reference_text, audio_url=audio_url)
  43. print(f"音频ID【{reference_id}】同步到实例【{ip}】上完成")
  44. except Exception as e:
  45. print(f"音频ID【{reference_id}】同步到实例【{ip}】上异常 {str(e)}")
  46. print(f"将音频同步到实例【{ip}】完成")
  47. if __name__ == '__main__':
  48. _main()