| | |
| | | |
| | | |
| | | class qwen_thread: |
| | | def __init__(self, config): |
| | | def __init__(self, config,logger): |
| | | self.config = config |
| | | self.max_workers = int(config.get("threadnum")) |
| | | self.executor = ThreadPoolExecutor(max_workers=int(config.get("threadnum"))) |
| | | self.semaphore = threading.Semaphore(int(config.get("threadnum"))) |
| | | self.logger = logger |
| | | |
| | | # 初始化Milvus集合 |
| | | connections.connect("default", host=config.get("milvusurl"), port=config.get("milvusport")) |
| | |
| | | # 共享的处理器 (线程安全) |
| | | self.processor = AutoProcessor.from_pretrained(config.get("qwenaddr"), use_fast=True) |
| | | |
| | | # 创建实例专属logger |
| | | self.logger = logging.getLogger(f"{self.__class__}_{id(self)}") |
| | | self.logger.setLevel(logging.INFO) |
| | | # 避免重复添加handler |
| | | if not self.logger.handlers: |
| | | handler = RotatingFileHandler( |
| | | filename=os.path.join("logs", 'thread_log.log'), |
| | | maxBytes=10 * 1024 * 1024, |
| | | backupCount=3, |
| | | encoding='utf-8' |
| | | ) |
| | | formatter = logging.Formatter( |
| | | '%(asctime)s - %(filename)s:%(lineno)d - %(funcName)s() - %(levelname)s: %(message)s' |
| | | ) |
| | | handler.setFormatter(formatter) |
| | | self.logger.addHandler(handler) |
| | | |
| | | def submit(self,res_a): |
| | | # 尝试获取信号量(非阻塞) |
| | |
| | | # 调用规则匹配方法,判断是否预警 |
| | | is_waning = self.image_rule_chat(desc, res['waning_value'], ragurl,rag_mode,max_tokens) |
| | | # 如果预警,则生成隐患描述和处理建议 |
| | | #if is_waning == 1: |
| | | if is_waning == 1: |
| | | # 获取规章制度数据 |
| | | filedata = self.get_filedata(res['waning_value'],res['suggestion'], ragurl) |
| | | # 生成隐患描述 |
| | |
| | | "rule_id": res["rule_id"], |
| | | "video_point_id": res['video_point_id'], # video_point_id |
| | | "video_point_name": res['video_point_name'], |
| | | "is_waning": 1, |
| | | "is_waning": is_waning, |
| | | "is_desc": is_desc, |
| | | "zh_desc_class": desc, # text_vector |
| | | "bounding_box": res['bounding_box'], # bounding_box |
| | |
| | | # 调用rag |
| | | asyncio.run(self.insert_json_data(ragurl, data)) |
| | | rag_time = datetime.now() - current_time |
| | | self.logger.info(f"{image_id}运行结束总体用时:{datetime.now() - ks_time},图片描述用时{desc_time},RAG用时{rag_time}") |
| | | self.logger.info(f"{res['video_point_id']}执行完毕:{image_id}运行结束总体用时:{datetime.now() - ks_time},图片描述用时{desc_time},RAG用时{rag_time}") |
| | | except Exception as e: |
| | | self.logger.info(f"线程:执行模型解析时出错::{e}") |
| | | return 0 |