|
|
|
@ -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)
|
|
|
|
|