22
33import logging
44from pathlib import Path
5- from typing import Any , Dict
5+ from typing import Any , Dict , List , Union
66
77from flask import request
88
99from wyoming .asr import Transcript
1010from wyoming .client import AsyncClient
1111from wyoming .error import Error
1212from wyoming .handle import Handled , NotHandled
13- from wyoming .intent import Intent , NotRecognized
13+ from wyoming .intent import Intent , IntentsStart , IntentsStop , NotRecognized
1414
1515from .shared import get_app , get_argument_parser
1616
@@ -27,7 +27,7 @@ def main():
2727 app = get_app ("intent" , CONF_PATH , args )
2828
2929 @app .route ("/api/recognize-intent" , methods = ["POST" , "GET" ])
30- async def api_stt () -> Dict [str , Any ]:
30+ async def api_recognize_intent () -> Union [ Dict [str , Any ], List [ Dict [ str , Any ]] ]:
3131 uri = request .args .get ("uri" , args .uri )
3232 if not uri :
3333 raise ValueError ("URI is required" )
@@ -45,42 +45,60 @@ async def api_stt() -> Dict[str, Any]:
4545 async with AsyncClient .from_uri (uri ) as client :
4646 await client .write_event (Transcript (text = text , language = language ).event ())
4747
48+ type_name = "unknown"
49+ results : List [Dict [str , Any ]] = []
50+ is_intent_list = False
51+
4852 while True :
4953 event = await client .read_event ()
5054 if event is None :
5155 raise RuntimeError ("Client disconnected" )
5256
53- success = False
54- type_name = "unknown"
55- result : Dict [ str , Any ] = {}
57+ if IntentsStart . is_type ( event . type ):
58+ is_intent_list = True
59+ continue
5660
5761 if Intent .is_type (event .type ):
58- success = True
5962 type_name = "intent"
6063 intent = Intent .from_event (event )
61- result = intent .to_dict ()
62- elif Handled .is_type (event .type ):
63- success = True
64+ results .append (intent .to_dict ())
65+ if not is_intent_list :
66+ break
67+
68+ if IntentsStop .is_type (event .type ):
69+ break
70+
71+ if Handled .is_type (event .type ):
6472 type_name = "handled"
6573 handled = Handled .from_event (event )
66- result = handled .to_dict ()
67- elif NotRecognized .is_type (event .type ):
68- success = False
74+ results .append (handled .to_dict ())
75+ break
76+
77+ if NotRecognized .is_type (event .type ):
6978 type_name = "not-recognized"
7079 not_recognized = NotRecognized .from_event (event )
71- result = not_recognized .to_dict ()
72- elif NotHandled .is_type (event .type ):
73- success = False
80+ results .append (not_recognized .to_dict ())
81+ break
82+
83+ if NotHandled .is_type (event .type ):
7484 type_name = "not-handled"
7585 not_handled = NotHandled .from_event (event )
76- result = not_handled .to_dict ()
77- elif Error .is_type (event .type ):
86+ results .append (not_handled .to_dict ())
87+ break
88+
89+ if Error .is_type (event .type ):
7890 error = Error .from_event (event )
7991 raise RuntimeError (
8092 f"Unexpected error from client: code={ error .code } , text={ error .text } "
8193 )
8294
83- return {"success" : success , "type" : type_name , "result" : result }
95+ if len (results ) == 0 :
96+ return {"success" : False , "type" : "unknown" , "result" : {}}
97+
98+ if len (results ) == 1 :
99+ return {"success" : True , "type" : type_name , "result" : results [0 ]}
100+
101+ return {"success" : True , "type" : type_name , "result" : results }
84102
85103 app .run (args .host , args .port )
86104
0 commit comments