diff --git a/AI/Text2Sql/vanna.db b/AI/Text2Sql/Db/vanna.db similarity index 70% rename from AI/Text2Sql/vanna.db rename to AI/Text2Sql/Db/vanna.db index 11abe5af..758cc82d 100644 Binary files a/AI/Text2Sql/vanna.db and b/AI/Text2Sql/Db/vanna.db differ diff --git a/AI/Text2Sql/Excel/b32a3221-2863-4851-a62b-92da959298eb.xlsx b/AI/Text2Sql/Excel/b32a3221-2863-4851-a62b-92da959298eb.xlsx new file mode 100644 index 00000000..a8564945 Binary files /dev/null and b/AI/Text2Sql/Excel/b32a3221-2863-4851-a62b-92da959298eb.xlsx differ diff --git a/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc b/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc index 7d3db5a1..8e9bf588 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 ed21772b..24412b07 100644 --- a/AI/Text2Sql/Model/biModel.py +++ b/AI/Text2Sql/Model/biModel.py @@ -15,6 +15,14 @@ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # 创建 Base 类 Base = declarative_base() +# 获取数据库会话 +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + # 定义 t_bi_question 表模型 class TBIQuestion(Base): __tablename__ = "t_bi_question" diff --git a/AI/Text2Sql/Util/SaveToExcel.py b/AI/Text2Sql/Util/SaveToExcel.py index e9d85010..32a5eed5 100644 --- a/AI/Text2Sql/Util/SaveToExcel.py +++ b/AI/Text2Sql/Util/SaveToExcel.py @@ -1,6 +1,4 @@ -import io import pandas as pd -from openpyxl import Workbook from openpyxl.styles import Font, PatternFill, Alignment, Border, Side from openpyxl.utils import get_column_letter @@ -66,75 +64,3 @@ def save_to_excel(data, filename): column_letter = get_column_letter(idx + 1) worksheet.column_dimensions[column_letter].width = column_width - -def save_to_excel_stream(data): - """ - 将数据集保存为格式化的Excel文件流 - - 参数: - data - 数据集 (列表字典格式,例如:[{"列1": "值1", "列2": "值2"}, ...]) - - 返回: - BytesIO - 包含Excel文件内容的流 - """ - # 转换数据为DataFrame - df = pd.DataFrame(data) - - # 创建一个 BytesIO 对象作为缓冲区 - output = io.BytesIO() - - # 创建Excel工作簿 - wb = Workbook() - ws = wb.active - ws.title = '统计报表' - - # 写入数据 - for row in pd.DataFrame(data).itertuples(index=False): - ws.append(row) - - # 定义边框样式 - thin_border = Border(left=Side(style='thin'), - right=Side(style='thin'), - top=Side(style='thin'), - bottom=Side(style='thin')) - - # 设置全局行高 - for row in ws.iter_rows(): - ws.row_dimensions[row[0].row].height = 20 - # 为所有单元格添加边框 - for cell in row: - cell.border = thin_border - - # 设置标题样式 - header_font = Font(bold=True, size=14) - header_fill = PatternFill(start_color='ADD8E6', end_color='ADD8E6', fill_type='solid') - - for cell in ws[1]: - cell.font = header_font - cell.fill = header_fill - cell.alignment = Alignment(horizontal='center', vertical='center') - - # 设置数据行样式 - data_font = Font(size=14) - for row in ws.iter_rows(min_row=2): - for cell in row: - cell.font = data_font - cell.alignment = Alignment(vertical='center', wrap_text=True) - - # 动态设置列宽 - for idx, column in enumerate(df.columns): - # 获取列的最大长度 - max_length = max( - df[column].astype(str).map(len).max(), # 数据列的最大长度 - len(str(column)) # 列名的长度 - ) - # 计算列宽,确保在 10 到 120 之间 - column_width = min(max(max_length + 2, 10) * 2, 120) # 加 2 是为了留出一些空白 - # 设置列宽 - column_letter = get_column_letter(idx + 1) - ws.column_dimensions[column_letter].width = column_width - - # 将工作簿保存到 BytesIO 对象 - wb.save(output) - output.seek(0) # 将指针移动到流的开头 - return output \ No newline at end of file diff --git a/AI/Text2Sql/Util/VannaUtil.py b/AI/Text2Sql/Util/VannaUtil.py index d7e0e5a1..431f1c9f 100644 --- a/AI/Text2Sql/Util/VannaUtil.py +++ b/AI/Text2Sql/Util/VannaUtil.py @@ -15,7 +15,7 @@ class VannaUtil(VannaBase): self.training_data = [] self.chat_history = [] self.db_type = db_type - self.db_uri = db_uri or 'vanna.db' # 默认使用 SQLite + self.db_uri = db_uri or 'Db/vanna.db' # 默认使用 SQLite self._init_db() def _init_db(self): diff --git a/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc b/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc index 9744971e..65680ad0 100644 Binary files a/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc and b/AI/Text2Sql/Util/__pycache__/SaveToExcel.cpython-310.pyc differ diff --git a/AI/Text2Sql/Util/__pycache__/VannaUtil.cpython-310.pyc b/AI/Text2Sql/Util/__pycache__/VannaUtil.cpython-310.pyc index c710a730..0641d26d 100644 Binary files a/AI/Text2Sql/Util/__pycache__/VannaUtil.cpython-310.pyc and b/AI/Text2Sql/Util/__pycache__/VannaUtil.cpython-310.pyc differ diff --git a/AI/Text2Sql/__pycache__/app.cpython-310.pyc b/AI/Text2Sql/__pycache__/app.cpython-310.pyc index f4681ea9..ea9ea73f 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 d0df49f7..5f8936d8 100644 --- a/AI/Text2Sql/app.py +++ b/AI/Text2Sql/app.py @@ -1,29 +1,24 @@ -import re +import uuid import uvicorn # 导入 uvicorn -from fastapi import FastAPI, HTTPException, Depends +from fastapi import FastAPI, HTTPException, Depends, Form from sqlalchemy.orm import Session -from starlette.responses import StreamingResponse +from starlette.responses import JSONResponse +from starlette.staticfiles import StaticFiles + +from Model.biModel import * from Text2Sql.Util.PostgreSQLUtil import PostgreSQLUtil -from Text2Sql.Util.SaveToExcel import save_to_excel_stream +from Text2Sql.Util.SaveToExcel import save_to_excel from Text2Sql.Util.VannaUtil import VannaUtil -from Model.biModel import * # 初始化 FastAPI app = FastAPI() +# 配置静态文件目录 +app.mount("/static", StaticFiles(directory="static"), name="static") @app.get("/") def read_root(): - return {"message": "Hello, World!"} - - -# 获取数据库会话 -def get_db(): - db = SessionLocal() - try: - yield db - finally: - db.close() + return {"message": "Welcome to Vanna AI SQL !"} # 创建记录 @@ -71,8 +66,11 @@ def delete_question(question_id: int, db: Session = Depends(get_db)): # 通过语义生成Excel +# http://10.10.21.20:8000/questions/get_excel +# 参数:question @app.post("/questions/get_excel") -def get_excel(question: str): +def get_excel(question: str = Form(...)): + vn = VannaUtil() # 指定学段 # question = ''' # 查询: @@ -92,18 +90,16 @@ def get_excel(question: str): # 执行SQL查询 with PostgreSQLUtil() as db: _data = db.execute_query(sql) - # 将数据保存为Excel文件流 - excel_stream = save_to_excel_stream(_data) - # 返回Excel文件流 - return StreamingResponse( - excel_stream, - media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - headers={"Content-Disposition": "attachment; filename=导出信息.xlsx"} - ) + print(_data) + # 在static目录下,生成一个guid号的临时文件 + uuidStr = str(uuid.uuid4()) + filename = f"static/{uuidStr}.xlsx" + save_to_excel(_data, filename) + # 返回静态文件URL + return {"success": True, "message": "Excel文件生成成功", "download_url": f"/static/{uuidStr}.xlsx"} # 确保直接运行脚本时启动 FastAPI 应用 if __name__ == "__main__": - vn = VannaUtil() uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True) diff --git a/AI/Text2Sql/static/3612417f-53ae-4284-80ec-e0502c4ee299.xlsx b/AI/Text2Sql/static/3612417f-53ae-4284-80ec-e0502c4ee299.xlsx new file mode 100644 index 00000000..717cf3b6 Binary files /dev/null and b/AI/Text2Sql/static/3612417f-53ae-4284-80ec-e0502c4ee299.xlsx differ