hotword.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import re
  2. from typing import Any, Optional, AsyncGenerator
  3. from sqlalchemy import select, func, delete
  4. from app.errors.hotword import HotwordOutofLimit, HotwordNotFound
  5. from app.models.db import get_async_session
  6. from app.models.common import Page
  7. from app.models.hotword import (
  8. ReplaceHotword,
  9. ASRHotword,
  10. ASRHotwordModel,
  11. ReplaceHotwordModel,
  12. )
  13. class ReplaceHotwordService:
  14. @staticmethod
  15. async def get_replace_hotword(
  16. replace_hotword: ReplaceHotwordModel,
  17. ) -> Optional[ReplaceHotword]:
  18. async with get_async_session() as session:
  19. result = await session.execute(
  20. select(ReplaceHotword).where(ReplaceHotword.id == replace_hotword.id)
  21. )
  22. res = result.scalar_one_or_none()
  23. if res is None:
  24. raise HotwordNotFound
  25. return res
  26. @staticmethod
  27. async def list_replace_hotwords(page: Page) -> Optional[Any]:
  28. async with get_async_session() as session:
  29. result = await session.execute(
  30. page(select(ReplaceHotword).order_by(ReplaceHotword.id.desc()))
  31. )
  32. return result.scalars().all()
  33. @staticmethod
  34. async def replace_hotwords_total() -> Optional[Any]:
  35. async with get_async_session() as session:
  36. # 计算总记录数
  37. total_query = select(func.count()).select_from(ReplaceHotword)
  38. total_result = await session.execute(total_query)
  39. return total_result.scalar()
  40. @staticmethod
  41. async def get_all_replace_hotwords() -> AsyncGenerator[ReplaceHotword, None]:
  42. async with get_async_session() as session:
  43. result = await session.execute(select(ReplaceHotword))
  44. for i in result.scalars():
  45. yield i
  46. @staticmethod
  47. async def create_replace_hotword(
  48. replace_hotword: ReplaceHotwordModel,
  49. ) -> int:
  50. async with get_async_session() as session:
  51. res = await session.execute(select(func.count()).select_from(ReplaceHotword))
  52. count = res.scalar_one()
  53. if count + len(replace_hotword.replace_list) > 200:
  54. # await session.rollback()
  55. raise HotwordOutofLimit
  56. for replace in replace_hotword.replace_list:
  57. new_replace_hotword = ReplaceHotword(
  58. origin_word=replace["origin_word"],
  59. hotword=replace["hotword"],
  60. )
  61. session.add(new_replace_hotword)
  62. await session.commit()
  63. return count + len(replace_hotword.replace_list)
  64. @staticmethod
  65. async def update_replace_hotword(
  66. replace_hotword: ReplaceHotwordModel,
  67. ) -> Optional[ReplaceHotword]:
  68. async with get_async_session() as session:
  69. result = await session.execute(
  70. select(ReplaceHotword).
  71. where(ReplaceHotword.id == replace_hotword.id)
  72. )
  73. update_replace_hotword = result.scalar_one_or_none()
  74. if update_replace_hotword is None:
  75. raise HotwordNotFound
  76. if replace_hotword.origin_word:
  77. update_replace_hotword.origin_word = replace_hotword.origin_word
  78. if replace_hotword.hotword:
  79. update_replace_hotword.hotword = replace_hotword.hotword
  80. await session.commit()
  81. return replace_hotword
  82. @staticmethod
  83. async def delete_replace_hotword(
  84. replace_hotword: ReplaceHotwordModel,
  85. ) -> Optional[ReplaceHotword]:
  86. async with get_async_session() as session:
  87. result = await session.execute(
  88. select(ReplaceHotword).where(ReplaceHotword.id == replace_hotword.id)
  89. )
  90. replace_result = result.scalar_one_or_none()
  91. if replace_result is None:
  92. return None
  93. await session.delete(replace_result)
  94. await session.commit()
  95. return None
  96. class ASRHotwordService:
  97. @staticmethod
  98. async def get_asr_hotword(asr_hotword: ASRHotwordModel) -> Optional[ASRHotword]:
  99. async with get_async_session() as session:
  100. result = await session.execute(
  101. select(ASRHotword).where(ASRHotword.id == asr_hotword.id)
  102. )
  103. res = result.scalar_one_or_none()
  104. if res is None:
  105. raise HotwordNotFound
  106. return res
  107. @staticmethod
  108. async def list_asr_hotwords(page: Page) -> Optional[Any]:
  109. async with get_async_session() as session:
  110. result = await session.execute(
  111. page(select(ASRHotword).order_by(ASRHotword.id.desc()))
  112. )
  113. return result.scalars().all()
  114. @staticmethod
  115. async def asr_hotwords_total() -> Optional[Any]:
  116. async with get_async_session() as session:
  117. # 计算总记录数
  118. total_query = select(func.count()).select_from(ASRHotword)
  119. total_result = await session.execute(total_query)
  120. return total_result.scalar()
  121. @staticmethod
  122. async def get_all_asr_hotwords() -> AsyncGenerator[ASRHotword, None]:
  123. async with get_async_session() as session:
  124. result = await session.execute(select(ASRHotword))
  125. for i in result.scalars():
  126. yield i
  127. @staticmethod
  128. async def create_asr_hotword(asr_hotword: ASRHotwordModel) -> int:
  129. async with get_async_session() as session:
  130. res = await session.execute(select(func.count()).select_from(ASRHotword))
  131. count = res.scalar_one()
  132. if count + len(asr_hotword.hotword_list) > 200:
  133. # await session.rollback()
  134. raise HotwordOutofLimit
  135. for hotword in asr_hotword.hotword_list:
  136. new_hotword = ASRHotword(
  137. hotword=hotword
  138. )
  139. session.add(new_hotword)
  140. await session.commit()
  141. return count + len(asr_hotword.hotword_list)
  142. @staticmethod
  143. async def update_asr_hotword(asr_hotword: ASRHotwordModel):
  144. async with get_async_session() as session:
  145. result = await session.execute(
  146. select(ASRHotword).where(ASRHotword.id == asr_hotword.id)
  147. )
  148. update_asr_hotword = result.scalar_one_or_none()
  149. if update_asr_hotword is None:
  150. raise HotwordNotFound
  151. if asr_hotword.hotword:
  152. update_asr_hotword.hotword = asr_hotword.hotword
  153. if asr_hotword.weight:
  154. update_asr_hotword.weight = asr_hotword.weight
  155. await session.commit()
  156. @staticmethod
  157. async def delete_asr_hotword(asr_hotword: ASRHotwordModel):
  158. async with get_async_session() as session:
  159. res = await session.execute(
  160. delete(ASRHotword).where(ASRHotword.id == asr_hotword.id)
  161. )
  162. await session.commit()
  163. if res.rowcount == 0:
  164. raise HotwordNotFound