Spaces:
Runtime error
Runtime error
Download utils.py from Chaos0818/LLMDataGenerator: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/spaces/Chaos0818/LLMDataGenerator/resolve/main/utils.py
- Command line
-
hf download hf://spaces/Chaos0818/LLMDataGenerator/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Chaos0818/LLMDataGenerator/resolve/main/utils.py
1.67 kB
| import io | |
| import json | |
| import logging | |
| def read_all_text(file_path: str) -> str: | |
| with open(file_path, "r", encoding="utf-8") as file: | |
| content = file.read() | |
| return content | |
| def dump_jsonl(path, data): | |
| with open(path, "w") as f: | |
| for item in data: | |
| f.write( | |
| json.dumps(item, indent=2, ensure_ascii=False) + "\n" | |
| ) | |
| class RealTimeLogCapture: | |
| """实时日志捕获器,使用StringIO方式""" | |
| def __init__(self, logger_name=None): | |
| self.log_stream = io.StringIO() | |
| self.logger = logging.getLogger(logger_name) if logger_name else logging.getLogger() | |
| # 创建处理器和格式器 | |
| self.handler = logging.StreamHandler(self.log_stream) | |
| formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(pathname)s:%(lineno)d - %(message)s", | |
| datefmt='%Y-%m-%d %H:%M:%S') | |
| self.handler.setFormatter(formatter) | |
| # 添加处理器到logger | |
| self.logger.addHandler(self.handler) | |
| self.logger.setLevel(logging.INFO) | |
| def get_logs(self): | |
| """获取当前所有日志内容""" | |
| return self.log_stream.getvalue() | |
| def clear_logs(self): | |
| """清空日志内容""" | |
| self.log_stream.truncate(0) | |
| self.log_stream.seek(0) | |
| def cleanup(self): | |
| """清理资源""" | |
| self.logger.removeHandler(self.handler) | |
| self.handler.close() | |
| if __name__ == "__main__": | |
| file_path = "your_path" # 请替换为实际的文件路径 | |
| text = read_all_text(file_path) | |
| print("-" * 20) | |
| print(text) | |
| print("-" * 20) | |