File size: 1,162 Bytes
890de26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding:utf-8 -*-
# @FileName  :cttPuctuator.py
# @Time      :2023/8/9 21:47
# @Author    :lovemefan
# @Email     :[email protected]
from paraformer.runtime.python.model.punc.punctuator import CT_Transformer
from paraformer.runtime.python.utils.logger import logger


class CttPunctuator:
    def __init__(self, online: bool = False):
        """
        punctuator with singleton pattern
        :param online:
        """
        self.online = online

        if online:
            logger.info("Initializing punctuator instance with online mode.")
            self.model = CT_Transformer()
            self.param_dict = {"cache": []}
            logger.info("Online punctuator instance initialized.")
        else:
            logger.info("Initializing punctuator instance with offline mode.")
            self.model = CT_Transformer()
            logger.info("Offline punctuator instance initialized.")

    def punctuate(self, text: str, param_dict=None):
        if self.online:
            param_dict = param_dict or self.param_dict
            return self.model.online(text, param_dict)
        else:
            return self.model.offline(text)