[代码重构中]重构Functor下的函数定义,修改为一个BaseFunctor+ModelLoader+DataCache进行基底构建。
This commit is contained in:
parent
1392168126
commit
040fc57e02
19
.cursorrules
Normal file
19
.cursorrules
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
You are an AI assistant specialized in Python development. Your approach emphasizes:
|
||||||
|
|
||||||
|
1. Clear project structure with separate directories for source code, tests, docs, and config.
|
||||||
|
2. Modular design with distinct files for models, services, controllers, and utilities.
|
||||||
|
3. Configuration management using environment variables.
|
||||||
|
4. Robust error handling and logging, including context capture.
|
||||||
|
5. Comprehensive testing with pytest.
|
||||||
|
6. Detailed documentation using docstrings and README files.
|
||||||
|
7. Dependency management via https://github.com/astral-sh/rye and virtual environments.
|
||||||
|
8. Code style consistency using Ruff.
|
||||||
|
9. CI/CD implementation with GitHub Actions or GitLab CI.
|
||||||
|
10. AI-friendly coding practices:
|
||||||
|
- Descriptive variable and function names
|
||||||
|
- Type hints
|
||||||
|
- Detailed comments for complex logic
|
||||||
|
- Rich error context for debugging
|
||||||
|
|
||||||
|
You provide code snippets and explanations tailored to these principles, optimizing for clarity and AI-assisted development.
|
102
src/functor/base.py
Normal file
102
src/functor/base.py
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
class BaseFunctor:
|
||||||
|
"""
|
||||||
|
基础函数器类,提供数据处理的基本框架
|
||||||
|
|
||||||
|
该类实现了数据处理的基本接口,包括数据推送、处理和回调机制。
|
||||||
|
所有具体的功能实现类都应该继承这个基类。
|
||||||
|
|
||||||
|
属性:
|
||||||
|
_data (dict): 存储处理数据的字典
|
||||||
|
_callback (Callable): 处理完成后的回调函数
|
||||||
|
_model (dict): 存储模型相关的配置和实例
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
data: dict or bytes = {},
|
||||||
|
callback: Callable = None,
|
||||||
|
model: dict = {},
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
初始化函数器
|
||||||
|
|
||||||
|
参数:
|
||||||
|
data (dict or bytes): 初始数据,可以是字典或字节数据
|
||||||
|
callback (Callable): 处理完成后的回调函数
|
||||||
|
model (dict): 模型相关的配置和实例
|
||||||
|
"""
|
||||||
|
self._data: dict = {}
|
||||||
|
self.push_data(data)
|
||||||
|
self._callback: Callable = callback
|
||||||
|
self._model: dict = model
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __call__(self, data = None):
|
||||||
|
"""
|
||||||
|
使类实例可调用,处理数据并触发回调
|
||||||
|
|
||||||
|
参数:
|
||||||
|
data: 要处理的数据,如果为None则处理已存储的数据
|
||||||
|
|
||||||
|
返回:
|
||||||
|
处理结果
|
||||||
|
"""
|
||||||
|
# 如果传入数据,则压入数据
|
||||||
|
if data is not None:
|
||||||
|
self.push_data(data)
|
||||||
|
# 处理数据
|
||||||
|
result = self.process()
|
||||||
|
# 如果回调函数存在,则触发回调
|
||||||
|
if self._callback is not None and callable(self._callback):
|
||||||
|
self._callback(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
"""
|
||||||
|
重载加法运算符,用于合并数据
|
||||||
|
|
||||||
|
参数:
|
||||||
|
other: 要合并的数据
|
||||||
|
|
||||||
|
返回:
|
||||||
|
self: 返回当前实例,支持链式调用
|
||||||
|
"""
|
||||||
|
self.push_data(other)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def set_callback(self, callback: Callable):
|
||||||
|
"""
|
||||||
|
设置回调函数
|
||||||
|
|
||||||
|
参数:
|
||||||
|
callback (Callable): 新的回调函数
|
||||||
|
"""
|
||||||
|
self._callback = callback
|
||||||
|
|
||||||
|
def set_model(self, model: dict):
|
||||||
|
"""
|
||||||
|
设置模型配置
|
||||||
|
|
||||||
|
参数:
|
||||||
|
model (dict): 新的模型配置
|
||||||
|
"""
|
||||||
|
self._model = model
|
||||||
|
|
||||||
|
def push_data(self, data):
|
||||||
|
"""
|
||||||
|
推送数据到处理器
|
||||||
|
|
||||||
|
参数:
|
||||||
|
data: 要处理的数据
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def process(self):
|
||||||
|
"""
|
||||||
|
处理数据的核心方法
|
||||||
|
|
||||||
|
返回:
|
||||||
|
处理结果
|
||||||
|
"""
|
||||||
|
pass
|
@ -6,6 +6,24 @@
|
|||||||
|
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
|
class ModelLoader:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __call__(self, args):
|
||||||
|
return self.load_models(args)
|
||||||
|
|
||||||
|
def load_models(self, args):
|
||||||
|
"""
|
||||||
|
加载所有需要的模型
|
||||||
|
|
||||||
|
参数:
|
||||||
|
args: 命令行参数,包含模型配置
|
||||||
|
|
||||||
|
返回:
|
||||||
|
dict: 包含所有加载的模型的字典
|
||||||
|
"""
|
||||||
|
|
||||||
def load_models(args):
|
def load_models(args):
|
||||||
"""
|
"""
|
||||||
加载所有需要的模型
|
加载所有需要的模型
|
||||||
|
43
src/functor/readme.md
Normal file
43
src/functor/readme.md
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
# 对于Functor的解释
|
||||||
|
|
||||||
|
## Functor 文件夹作用
|
||||||
|
|
||||||
|
Functor文件夹用于存放所有功能性的类,包括VAD、PUNC、ASR、SPK等。
|
||||||
|
|
||||||
|
## Functor 类的定义
|
||||||
|
|
||||||
|
所有类应继承于 **基类** `BaseFunctor` ,应遵从 *压入数据* 与 *数据处理* 解绑。
|
||||||
|
|
||||||
|
为了方便使用,我们对于 **基类** 的定义如下:
|
||||||
|
|
||||||
|
1. 函数内部使用的变量以单下划线开头,预定有 `_data`, `_callback`, `_model`等
|
||||||
|
|
||||||
|
2. 对于使用的模型,请从统一的 **模型管理类`ModelLoader`** 中获取,由模型管理类统一进行加载、缓存和释放,`_model`存放类型为`dict`。
|
||||||
|
|
||||||
|
3. 定义了
|
||||||
|
|
||||||
|
`__call__`:可传入`data`,默认调用`push_data`,随后默认调用`process`
|
||||||
|
|
||||||
|
`__add__`
|
||||||
|
|
||||||
|
```python
|
||||||
|
class BaseFunctor:
|
||||||
|
def __init__(self):
|
||||||
|
self._data: dict = {}
|
||||||
|
self._callback: function = null
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __call__(self, data):
|
||||||
|
result =
|
||||||
|
self._callback(process(data))
|
||||||
|
return self.process(data)
|
||||||
|
|
||||||
|
def set_callback(self, callback: Callable):
|
||||||
|
self._callback = callback
|
||||||
|
|
||||||
|
def push_data():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def process(self, data):
|
||||||
|
pass
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user