1515# See the License for the specific language governing permissions and
1616# limitations under the License.
1717
18- from typing import Union
18+ from typing import ByteString
1919from fastapi import APIRouter
2020from neural_chat .cli .log import logger
21- from neural_chat .server . restful . request import VoiceRequest , TextRequest
22- from neural_chat .server . restful . response import VoiceResponse , TextResponse
21+ from neural_chat .config import NeuralChatConfig
22+ from neural_chat .chatbot import build_chatbot
2323
2424
2525class VoiceChatAPIRouter (APIRouter ):
@@ -36,28 +36,33 @@ def get_chatbot(self):
3636 raise RuntimeError ("Chatbot instance has not been set." )
3737 return self .chatbot
3838
39- async def handle_voice2text_request (self , request : VoiceRequest ) -> TextResponse :
39+ async def handle_voice2text_request (self , request : ByteString ) -> str :
4040 # TODO: implement voice to text
4141 chatbot = self .get_chatbot ()
4242 # TODO: chatbot.voice2text()
43- return TextResponse (content = None )
43+ result = chatbot .predict (request .voice )
44+ return result
4445
45- async def handle_text2voice_request (self , request : TextRequest ) -> VoiceResponse :
46+ async def handle_text2voice_request (self , text : str ) -> ByteString :
4647 # TODO: implement text to voice
4748 chatbot = self .get_chatbot ()
4849 # TODO: chatbot.text2voice()
49- return VoiceResponse (content = None )
50+ result = chatbot .predict (text )
51+ return result
5052
5153
5254router = VoiceChatAPIRouter ()
55+ config = NeuralChatConfig ()
56+ bot = build_chatbot (config )
57+ router .set_chatbot (bot )
5358
5459# voice to text
5560@router .post ("/v1/voice/asr" )
56- async def voice2text (requst : VoiceRequest ) -> TextResponse :
57- return await router .handle_voice2text_request (requst )
61+ async def voice2text (request : ByteString ) -> str :
62+ return await router .handle_voice2text_request (request )
5863
5964
6065# text to voice
6166@router .post ("/v1/voice/tts" )
62- async def voice2text (requst : TextRequest ) -> VoiceResponse :
67+ async def voice2text (requst : str ) -> ByteString :
6368 return await router .handle_text2voice_request (requst )
0 commit comments