You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.1 KiB

5 months ago
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()
# SSE 推送路由
# @app.route('/register', methods=["GET"])
# def register():
# 获取客户端标识符
# client_id = str(uuid.uuid4())
#
# # 返回 SSE 响应
# return jsonify({"client_id": client_id})
# SSE 推送路由
# @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)