9 changed files with 651 additions and 436 deletions
@ -0,0 +1,220 @@ |
|||
# -*- coding: utf-8 -*- |
|||
""" |
|||
@author: bingyl123@163.com |
|||
@version: 1.0.0 |
|||
@file: OutLog.py |
|||
@time: 2023/2/23 20:25 |
|||
""" |
|||
# import logging |
|||
# import logging.config |
|||
# import re |
|||
# import datetime |
|||
# import queue |
|||
# |
|||
# |
|||
# class OutLog: |
|||
# _instance = None |
|||
# logger = None |
|||
# |
|||
# def __new__(cls): |
|||
# if cls._instance is None: |
|||
# cls._instance = super(OutLog, cls).__new__(cls) |
|||
# cls.logger = logging.getLogger("app") # 默认logger名称为"app" |
|||
# cls._instance.queue_dict = {} |
|||
# cls._instance.done_dict = {} |
|||
# return cls._instance |
|||
# |
|||
# def get_queue(self, user_id): |
|||
# if user_id not in self.queue_dict: |
|||
# self.queue_dict[user_id] = [] |
|||
# self.done_dict[user_id] = {} # 初始化为未完成的字典 |
|||
# return self.queue_dict[user_id] |
|||
# |
|||
# def mark_done(self, user_id, producer_name): |
|||
# self.done_dict[user_id][producer_name] = True |
|||
# |
|||
# def is_done(self, user_id): |
|||
# return all(self.done_dict.get(user_id, {}).values()) # 检查所有生产者是否完成 |
|||
# @staticmethod |
|||
# def put(item: str, level="INFO"): |
|||
# dtf = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|||
# mq.put(f"{dtf}[{level}]: {item}") |
|||
# |
|||
# @staticmethod |
|||
# def debug(item, log=True): |
|||
# OutLog.put(item, level="DEBUG") |
|||
# if log: |
|||
# OutLog._instance.logger.debug(item) |
|||
# |
|||
# @staticmethod |
|||
# def info(item, log=True): |
|||
# OutLog.put(item, level="INFO") |
|||
# if log: |
|||
# OutLog._instance.logger.info(item) |
|||
# |
|||
# @staticmethod |
|||
# def warning(item, log=True): |
|||
# OutLog.put(item, level="WARNING") |
|||
# if log: |
|||
# OutLog._instance.logger.warning(item) |
|||
# |
|||
# @staticmethod |
|||
# def error(item, log=True): |
|||
# OutLog.put(item, level="ERROR") |
|||
# if log: |
|||
# OutLog._instance.logger.error(item) |
|||
# |
|||
# @staticmethod |
|||
# def critical(item, log=True): |
|||
# OutLog.put(item, level="CRITICAL") |
|||
# if log: |
|||
# OutLog._instance.logger.critical(item) |
|||
# |
|||
# |
|||
# |
|||
# # 日志配置 |
|||
# log_config = { |
|||
# 'version': 1, |
|||
# 'disable_existing_loggers': False, |
|||
# 'formatters': { |
|||
# 'standard': { |
|||
# 'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
|||
# }, |
|||
# }, |
|||
# 'handlers': { |
|||
# 'console': { |
|||
# 'class': 'logging.StreamHandler', |
|||
# 'formatter': 'standard', |
|||
# 'level': logging.INFO, |
|||
# }, |
|||
# 'file': { |
|||
# 'class': 'logging.FileHandler', |
|||
# 'filename': 'Logger.log', |
|||
# 'formatter': 'standard', |
|||
# 'level': logging.WARNING, |
|||
# }, |
|||
# }, |
|||
# 'loggers': { |
|||
# '': { |
|||
# 'handlers': ['console', 'file'], |
|||
# 'level': logging.WARNING, |
|||
# 'propagate': True, |
|||
# }, |
|||
# } |
|||
# } |
|||
# |
|||
# logging.config.dictConfig(log_config) |
|||
# |
|||
# outLog = OutLog() # 获取单例实例 |
|||
|
|||
|
|||
|
|||
import logging |
|||
import logging.config |
|||
import datetime |
|||
|
|||
class OutLog: |
|||
_instance = None |
|||
logger = None |
|||
|
|||
def __new__(cls): |
|||
if cls._instance is None: |
|||
cls._instance = super(OutLog, cls).__new__(cls) |
|||
cls.logger = logging.getLogger("app") # 默认logger名称为"app" |
|||
cls._instance.queue_dict = {} |
|||
cls._instance.done_dict = {} |
|||
return cls._instance |
|||
|
|||
def get_queue(self, user_id,producer_name): |
|||
if user_id not in self.queue_dict: |
|||
self.queue_dict[user_id] = [] |
|||
self.done_dict[user_id] = {} # 初始化为未完成的字典 |
|||
if user_id not in self.done_dict: |
|||
self.done_dict[user_id][producer_name] = False |
|||
return self.UserLogger(user_id) |
|||
def get_queueData(self, user_id): |
|||
if user_id in self.queue_dict: |
|||
return OutLog._instance.queue_dict[self.user_id] |
|||
def del_queue(self,user_id): |
|||
if self.is_done(user_id): |
|||
del self.queue_dict[user_id] |
|||
del self.done_dict[user_id] |
|||
class UserLogger: |
|||
def __init__(self, user_id): |
|||
self.user_id = user_id |
|||
self.logger = OutLog._instance.logger |
|||
|
|||
def log(self, item: str, level: str): |
|||
dtf = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|||
log_entry = f"{dtf}[{level}]: {item}" |
|||
OutLog._instance.queue_dict[self.user_id].append(log_entry) # 保存到对应用户的队列 |
|||
self._log_to_logger(item, level) |
|||
|
|||
def _log_to_logger(self, item: str, level: str): |
|||
if level == "DEBUG": |
|||
self.logger.debug(item) |
|||
elif level == "INFO": |
|||
self.logger.info(item) |
|||
elif level == "WARNING": |
|||
self.logger.warning(item) |
|||
elif level == "ERROR": |
|||
self.logger.error(item) |
|||
elif level == "CRITICAL": |
|||
self.logger.critical(item) |
|||
|
|||
def info(self, item: str): |
|||
self.log(item, "INFO") |
|||
|
|||
def warning(self, item: str): |
|||
self.log(item, "WARNING") |
|||
|
|||
def debug(self, item: str): |
|||
self.log(item, "DEBUG") |
|||
|
|||
def error(self, item: str): |
|||
self.log(item, "ERROR") |
|||
|
|||
def critical(self, item: str): |
|||
self.log(item, "CRITICAL") |
|||
|
|||
def mark_done(self, user_id, producer_name): |
|||
self.done_dict[user_id][producer_name] = True |
|||
|
|||
def is_done(self, user_id): |
|||
return all(self.done_dict.get(user_id, {}).values()) # 检查所有生产者是否完成 |
|||
|
|||
|
|||
# 日志配置 |
|||
log_config = { |
|||
'version': 1, |
|||
'disable_existing_loggers': False, |
|||
'formatters': { |
|||
'standard': { |
|||
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
|||
}, |
|||
}, |
|||
'handlers': { |
|||
'console': { |
|||
'class': 'logging.StreamHandler', |
|||
'formatter': 'standard', |
|||
'level': logging.INFO, |
|||
}, |
|||
'file': { |
|||
'class': 'logging.FileHandler', |
|||
'filename': 'Logger.log', |
|||
'formatter': 'standard', |
|||
'level': logging.WARNING, |
|||
}, |
|||
}, |
|||
'loggers': { |
|||
'': { |
|||
'handlers': ['console', 'file'], |
|||
'level': logging.WARNING, |
|||
'propagate': True, |
|||
}, |
|||
} |
|||
} |
|||
|
|||
logging.config.dictConfig(log_config) |
|||
|
|||
outLog = OutLog() # 获取单例实例 |
@ -1,109 +1,79 @@ |
|||
import time |
|||
import json |
|||
import math |
|||
from flask import Flask,Response,request |
|||
from flask_sse import sse |
|||
from flask_cors import CORS |
|||
import re |
|||
import qwen_agenttext |
|||
app = Flask(__name__) |
|||
cros = CORS(app) |
|||
# SSE 推送函数 |
|||
import paddle; |
|||
paddle.device.get_available_device() |
|||
# -*- coding:utf-8 -*- |
|||
# from spire.doc import * |
|||
# from spire.doc.common import * |
|||
# |
|||
# # 创建一个 Document 对象 |
|||
# document = Document() |
|||
# # 加载一个 Word DOCX 文档 |
|||
# # document.LoadFromFile("C:\\Users\\gy051\\Desktop\\1223.doc") |
|||
# document.LoadFromFile("D:\\数据集\\数据集\\3.doc") |
|||
# print(document.Sections.Count) |
|||
# for i in range(document.Sections.Count): |
|||
# section=document.Sections[i] |
|||
# for x in range(section.Paragraphs.Count): |
|||
# paragraph=section.Paragraphs[x] |
|||
# print(paragraph.Text) |
|||
# print("---------------------------------") |
|||
# # 或加载一个 Word DOC 文档 |
|||
# # document.LoadFromFile("1223.xml") |
|||
# |
|||
# # # # 设置是否在 HTML 中嵌入图片 |
|||
# # document.HtmlExportOptions.ImageEmbedded = True |
|||
# # # document.XHTMLValidateOption.ImageEmbedded = True |
|||
# # # |
|||
# # # # 设置是否将表单字段导出为纯文本在 HTML 中显示 |
|||
# # document.HtmlExportOptions.IsTextInputFormFieldAsText = True |
|||
# # # document.XHTMLValidateOption.IsTextInputFormFieldAsText = True |
|||
# # # |
|||
# # # # 设置是否在 HTML 中导出页眉和页脚 |
|||
# # document.HtmlExportOptions.HasHeadersFooters = False |
|||
# # # document.XHTMLValidateOption.HasHeadersFooters = True |
|||
# # |
|||
# # # 将 Word 文档保存为 HTML 文件 |
|||
# # document.SaveToFile("1223.html", FileFormat.Html) |
|||
# # # |
|||
# document.Close() |
|||
from bs4 import BeautifulSoup |
|||
# 读取HTML文件 |
|||
with open('D:\\models\\1223.html', 'r',encoding="utf-8") as file: |
|||
html_content = file.read() |
|||
|
|||
# 解析HTML文档 |
|||
soup = BeautifulSoup(html_content, 'html.parser') |
|||
|
|||
# SSE 推送路由 |
|||
# 用于存储结果的字典 |
|||
headings = {} |
|||
current_heading = None |
|||
|
|||
# 遍历所有的h1, h2, h3等标题 |
|||
for element in soup.find_all(['h1', 'h2', 'h3',"h4","h5","h6"]): |
|||
level = int(element.name[1]) # 获取标题级别 |
|||
title = element.get_text(strip=True) # 获取标题文本 |
|||
|
|||
# @app.route('/register', methods=["GET"]) |
|||
# def register(): |
|||
# 获取客户端标识符 |
|||
# client_id = str(uuid.uuid4()) |
|||
# |
|||
# # 返回 SSE 响应 |
|||
# return jsonify({"client_id": client_id}) |
|||
# 设置当前标题 |
|||
current_heading = { |
|||
'title': title, |
|||
'level': level, |
|||
'content': [] |
|||
} |
|||
|
|||
# 将当前标题添加到字典中 |
|||
headings[title] = current_heading |
|||
|
|||
# SSE 推送路由 |
|||
# 寻找当前标题下的内容 |
|||
next_element = element.find_next_sibling() |
|||
while next_element and next_element.name not in ['h1', 'h2', 'h3',"h4","h5","h6"]: |
|||
# 判断内容的标签 |
|||
if next_element.name in ['p', 'div']: |
|||
current_heading['content'].append(next_element.get_text(strip=False)) |
|||
next_element = next_element.find_next_sibling() |
|||
|
|||
# 输出结果 |
|||
for heading in headings.values(): |
|||
print(f"标题: {heading['title']} (级别: {heading['level']})") |
|||
print("内容:") |
|||
for content in heading['content']: |
|||
print(f" - {content}") |
|||
print() |
|||
|
|||
|
|||
# @app.route('/sse', methods=['POST']) |
|||
# def stream(): |
|||
# # 获取客户端标识符 |
|||
# client_id = 1 |
|||
# print("client_id", client_id) |
|||
# |
|||
# def aa(): |
|||
# # 循环发送 SSE 数据 |
|||
# for i in range(10): |
|||
# data = 'Hello, %s!' % client_id + str(i) |
|||
# print(data) |
|||
# sse.publish(data, channel=client_id, type='message') |
|||
# time.sleep(1) |
|||
# sse.publish("end", channel=client_id, type='message') |
|||
# |
|||
# # 返回 SSE 响应 |
|||
# response = Response(aa(), mimetype='text/event-stream') |
|||
# response.headers.add('Cache-Control', 'no-cache') |
|||
# response.headers.add('Connection', 'keep-alive') |
|||
# response.headers.add('X-Accel-Buffering', 'no') |
|||
# return response |
|||
# |
|||
# |
|||
# |
|||
# @app.route('/stream' ,methods=["GET", "POST"]) |
|||
# def stream_numbers(): |
|||
# context= request.args.get('context') |
|||
# |
|||
# |
|||
# headers = { |
|||
# "Content-Type": "text/event-stream", |
|||
# "Cache-Control": "no-cache", |
|||
# "X-Accel-Buffering": "no", |
|||
# "Access-Control-Allow-Origin": "*", |
|||
# "Access-Control-Allow-Methods": "GET,POST", |
|||
# "Access-Control-Allow-Headers": "x-requested-with,content-type", |
|||
# } |
|||
# return Response(generate_numbers(),headers=headers) |
|||
# def generate_numbers(): |
|||
# event_id=0 |
|||
# # for number in range(1, 10): |
|||
# # json_data = json.dumps({"number": number}) |
|||
# # print(json_data) |
|||
# # event_id += 1 |
|||
# # yield f"id: {event_id}\n" |
|||
# # yield f"event: time-update\n" |
|||
# # yield f"data: {json_data}\n\n" # 每次生成一个数字就发送 |
|||
# json_data = json.dumps({"number": "done"}) |
|||
# yield f"id: {1}\n" |
|||
# yield f"event: time-update\n" |
|||
# yield f"data: 34568\n\n" # 发送完成信号 |
|||
# if __name__ == '__main__': |
|||
# |
|||
# |
|||
# # 读取文件内容 |
|||
# with open("checkPlaceName.txt", "r", encoding='utf-8') as f: |
|||
# gettext = f.read() |
|||
# batchNum=20 |
|||
# sentences = re.split(r'[。\n]', gettext) |
|||
# # 去掉空字符 |
|||
# sentences = [sentence.strip() for sentence in sentences if sentence.strip()] |
|||
# # 计算总字符数 |
|||
# total_chars = len(sentences) |
|||
# |
|||
# # 计算有多少份 |
|||
# num_chunks = math.ceil(total_chars / batchNum) |
|||
# |
|||
# # 按batchNum字为一份进行处理 |
|||
# chunks = [sentences[i:i + batchNum] for i in range(0, total_chars, batchNum)] |
|||
# |
|||
# # 打印每一份的内容 |
|||
# for i, chunk in enumerate(chunks): |
|||
# print(f"Chunk {i + 1}:") |
|||
# print(chunk) |
|||
# print("-" * 40) |
|||
# |
|||
# # 打印总份数 |
|||
# print(f"Total chunks: {num_chunks}") |
|||
# app.run(debug=True,port=80) |
Loading…
Reference in new issue