maolin.liu
commited on
Commit
·
f62fb80
1
Parent(s):
c08591d
[bugfix]Fix transcribed text not continually.
Browse files- cli.py +1 -1
- consumer/asr.py +25 -8
cli.py
CHANGED
|
@@ -3,7 +3,7 @@ from consumer.asr import TranscribeConsumer
|
|
| 3 |
|
| 4 |
def cli():
|
| 5 |
transcribe_consumer = TranscribeConsumer()
|
| 6 |
-
transcribe_consumer.consume_messages(transcribe_consumer.
|
| 7 |
|
| 8 |
|
| 9 |
if __name__ == '__main__':
|
|
|
|
| 3 |
|
| 4 |
def cli():
|
| 5 |
transcribe_consumer = TranscribeConsumer()
|
| 6 |
+
transcribe_consumer.consume_messages(transcribe_consumer.input_queue_name, transcribe_consumer.consume)
|
| 7 |
|
| 8 |
|
| 9 |
if __name__ == '__main__':
|
consumer/asr.py
CHANGED
|
@@ -50,8 +50,10 @@ class TranscribeConsumer(BasicMessageReceiver):
|
|
| 50 |
super().__init__()
|
| 51 |
|
| 52 |
self.exchange_name = 'transcribe'
|
| 53 |
-
self.
|
| 54 |
-
self.
|
|
|
|
|
|
|
| 55 |
|
| 56 |
self.setup_consume_parameters()
|
| 57 |
|
|
@@ -62,9 +64,24 @@ class TranscribeConsumer(BasicMessageReceiver):
|
|
| 62 |
logger.info('Load model finished.')
|
| 63 |
|
| 64 |
def setup_consume_parameters(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
self.declare_exchange(self.exchange_name)
|
| 66 |
-
self.declare_queue(self.
|
| 67 |
-
self.bind_queue(self.exchange_name, self.
|
| 68 |
|
| 69 |
def send_message(self, message: Union[dict, str]):
|
| 70 |
routing_key = 'transcribe-output'
|
|
@@ -109,11 +126,11 @@ class TranscribeConsumer(BasicMessageReceiver):
|
|
| 109 |
logger.info(f'Start transcribe input...')
|
| 110 |
segments, _ = self.asr_model.transcribe(audio_file, language=validated_message.language)
|
| 111 |
|
| 112 |
-
|
| 113 |
for segment in segments:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
except ValidationError as exc:
|
| 118 |
logging.exception('Message validated failed: \n message:\n %s\n\n exception info:\n %s', message, exc)
|
| 119 |
self.send_fail_message(body.get('uuid'), f'{exc}')
|
|
|
|
| 50 |
super().__init__()
|
| 51 |
|
| 52 |
self.exchange_name = 'transcribe'
|
| 53 |
+
self.input_queue_name = 'transcribe-input'
|
| 54 |
+
self.input_routing_key = 'transcribe-input'
|
| 55 |
+
self.output_queue_name = 'transcribe-output'
|
| 56 |
+
self.output_routing_key = 'transcribe-output'
|
| 57 |
|
| 58 |
self.setup_consume_parameters()
|
| 59 |
|
|
|
|
| 64 |
logger.info('Load model finished.')
|
| 65 |
|
| 66 |
def setup_consume_parameters(self):
|
| 67 |
+
logger.info(
|
| 68 |
+
f'Create consumer exchange: {self.exchange_name}, '
|
| 69 |
+
f'routing-key: {self.input_routing_key}, '
|
| 70 |
+
f'queue: {self.input_queue_name}'
|
| 71 |
+
)
|
| 72 |
+
self.declare_exchange(self.exchange_name)
|
| 73 |
+
self.declare_queue(self.input_queue_name, max_priority=-1)
|
| 74 |
+
self.bind_queue(self.exchange_name, self.input_queue_name, self.input_routing_key)
|
| 75 |
+
|
| 76 |
+
def setup_producer_parameters(self):
|
| 77 |
+
logger.info(
|
| 78 |
+
f'Create producer exchange: {self.exchange_name}, '
|
| 79 |
+
f'routing-key: {self.output_routing_key}, '
|
| 80 |
+
f'queue: {self.output_queue_name}'
|
| 81 |
+
)
|
| 82 |
self.declare_exchange(self.exchange_name)
|
| 83 |
+
self.declare_queue(self.output_queue_name, max_priority=-1)
|
| 84 |
+
self.bind_queue(self.exchange_name, self.output_queue_name, self.output_routing_key)
|
| 85 |
|
| 86 |
def send_message(self, message: Union[dict, str]):
|
| 87 |
routing_key = 'transcribe-output'
|
|
|
|
| 126 |
logger.info(f'Start transcribe input...')
|
| 127 |
segments, _ = self.asr_model.transcribe(audio_file, language=validated_message.language)
|
| 128 |
|
| 129 |
+
transcribed_segment_text = []
|
| 130 |
for segment in segments:
|
| 131 |
+
transcribed_segment_text.append(segment.text)
|
| 132 |
+
transcribed_text = ', '.join(transcribed_segment_text)
|
| 133 |
+
logger.info(f'Transcribed text: {transcribed_text}')
|
| 134 |
except ValidationError as exc:
|
| 135 |
logging.exception('Message validated failed: \n message:\n %s\n\n exception info:\n %s', message, exc)
|
| 136 |
self.send_fail_message(body.get('uuid'), f'{exc}')
|