diff --git a/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc b/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc index 25562e4c..9a0dceb2 100644 Binary files a/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc and b/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc differ diff --git a/AI/Text2Sql/Model/biModel.py b/AI/Text2Sql/Model/biModel.py index 0c6f6dc7..4cf652b9 100644 --- a/AI/Text2Sql/Model/biModel.py +++ b/AI/Text2Sql/Model/biModel.py @@ -70,10 +70,9 @@ def update_question_by_id(db: PostgreSQLUtil, question_id: str, **kwargs): return False # 根据 问题id 查询 sql -def get_question_sql_by_id(db, question_id: str): +def get_question_by_id(db, question_id: str): select_sql = """ select * from t_bi_question where id=%s """ _data = db.execute_query(select_sql, (question_id,)) - sql = _data[0]['sql'] - return sql + return _data diff --git a/AI/Text2Sql/__pycache__/app.cpython-310.pyc b/AI/Text2Sql/__pycache__/app.cpython-310.pyc index a29552b0..2e2b081c 100644 Binary files a/AI/Text2Sql/__pycache__/app.cpython-310.pyc and b/AI/Text2Sql/__pycache__/app.cpython-310.pyc differ diff --git a/AI/Text2Sql/app.py b/AI/Text2Sql/app.py index 46e9841c..c09ac32a 100644 --- a/AI/Text2Sql/app.py +++ b/AI/Text2Sql/app.py @@ -66,15 +66,13 @@ def get_excel(question_id: str = Form(...), question_str: str = Form(...), db: P return {"success": True, "message": "Excel文件生成成功", "download_url": f"/static/{uuid_str}.xlsx"} -# http://10.10.21.20:8000/questions/get_docx?question_id_get=af15d834-e7f5-46b4-a0f6-15f1f888f443 +# http://10.10.21.20:8000/questions/get_docx_stream?question_id_get=af15d834-e7f5-46b4-a0f6-15f1f888f443 - - -@app.api_route("/questions/get_docx", methods=["POST", "GET"]) -async def get_docx( - question_id: str = Form(None, description="问题ID(POST请求)"), # POST 请求参数 - question_id_get: str = Query(None, description="问题ID(GET请求)"), # GET 请求参数 - db: PostgreSQLUtil = Depends(get_db) +@app.api_route("/questions/get_docx_stream", methods=["POST", "GET"]) +async def get_docx_stream( + question_id: str = Form(None, description="问题ID(POST请求)"), # POST 请求参数 + question_id_get: str = Query(None, description="问题ID(GET请求)"), # GET 请求参数 + db: PostgreSQLUtil = Depends(get_db) ): # 根据请求方式获取 question_id if question_id is not None: # POST 请求 @@ -85,7 +83,7 @@ async def get_docx( return {"success": False, "message": "缺少问题ID参数"} # 根据问题ID获取查询sql - sql = get_question_sql_by_id(db, question_id) + sql = get_question_by_id(db, question_id)[0]['sql'] # 4、生成word报告 prompt = ''' 请根据以下 JSON 数据,整理出2000字左右的话描述当前数据情况。要求: @@ -129,28 +127,22 @@ async def get_docx( chunk_content = chunk.choices[0].delta.content # 逐字拆分并返回 for char in chunk_content: - print(char, end="", flush=True) # 逐字输出到控制台 + # print(char, end="", flush=True) # 逐字输出到控制台 yield char.encode("utf-8") # 将字符编码为 UTF-8 字节 summary += char # 将内容拼接到 summary 中 # 流式传输完成后,生成 Word 文档 markdown_to_docx(summary, output_file=filename) - # 返回最终的 JSON 数据 - final_response = json.dumps({ - "success": True, - "message": "Word文件生成成功", - "download_url": f"/static/{uuid_str}.docx" - }) - print(final_response) # 输出最终 JSON 到控制台 - yield final_response.encode("utf-8") # 将 JSON 数据编码为 UTF-8 字节 + # 记录到数据库 + update_question_by_id(db, question_id, docx_file_name=filename) except Exception as e: # 如果发生异常,返回错误信息 error_response = json.dumps({ "success": False, "message": f"生成Word文件失败: {str(e)}" }) - print(error_response) # 输出错误信息到控制台 + # print(error_response) # 输出错误信息到控制台 yield error_response.encode("utf-8") # 将错误信息编码为 UTF-8 字节 finally: # 确保资源释放 @@ -167,6 +159,29 @@ async def get_docx( } ) +# 返回生成的Word文件下载地址 +# http://10.10.21.20:8000/questions/get_docx_file?question_id_get=af15d834-e7f5-46b4-a0f6-15f1f888f443 +@app.api_route("/questions/get_docx_file", methods=["POST", "GET"]) +async def get_docx_file( + question_id: str = Form(None, description="问题ID(POST请求)"), # POST 请求参数 + question_id_get: str = Query(None, description="问题ID(GET请求)"), # GET 请求参数 + db: PostgreSQLUtil = Depends(get_db) +): + # 根据请求方式获取 question_id + if question_id is not None: # POST 请求 + question_id = question_id + elif question_id_get is not None: # GET 请求 + question_id = question_id_get + else: + return {"success": False, "message": "缺少问题ID参数"} + + # 根据问题ID获取查询docx_file_name + docx_file_name = get_question_by_id(db, question_id)[0]['docx_file_name'] + + # 返回成功和静态文件的URL + return {"success": True, "message": "Word文件生成成功", "download_url": f"{docx_file_name}"} + + # 确保直接运行脚本时启动 FastAPI 应用 if __name__ == "__main__": uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True) diff --git a/AI/Text2Sql/static/44ed77a9-26f9-45c6-94d9-4bec2d6f0be5.docx b/AI/Text2Sql/static/44ed77a9-26f9-45c6-94d9-4bec2d6f0be5.docx new file mode 100644 index 00000000..96f40459 Binary files /dev/null and b/AI/Text2Sql/static/44ed77a9-26f9-45c6-94d9-4bec2d6f0be5.docx differ diff --git a/AI/Text2Sql/static/75d4d893-fd1b-4d6b-96e4-cd123c3802cb.docx b/AI/Text2Sql/static/75d4d893-fd1b-4d6b-96e4-cd123c3802cb.docx new file mode 100644 index 00000000..11b332e6 Binary files /dev/null and b/AI/Text2Sql/static/75d4d893-fd1b-4d6b-96e4-cd123c3802cb.docx differ diff --git a/AI/Text2Sql/static/e098f920-0b0c-461f-a07f-de106d35da88.docx b/AI/Text2Sql/static/e098f920-0b0c-461f-a07f-de106d35da88.docx new file mode 100644 index 00000000..2411d9c2 Binary files /dev/null and b/AI/Text2Sql/static/e098f920-0b0c-461f-a07f-de106d35da88.docx differ