import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

from tool.pdf2md import pdf2ai
from utils import dmQuery
from utils.shellUtil import execute_command_on_linux
from utils.text_to_html import save_html_to_file

app = FastAPI() # 创建API实例

@app.get("/")
async def root():
    return {"message": "Hello World"}





# 将数据模型定义为继承BaseModel的类
class Item(BaseModel):
    sql: str


class Pdf2md(BaseModel):
    pdfUrl: str
    prompt: str
    pageNum: int


class Saved2text(BaseModel):
    htmlContent: str
    fileName: str


@app.put("/exsql")
async def create_item( item: Item, q: str = None):
    result = {**item.dict()}
    results = dmQuery.execute_query(item.sql)

    return results



@app.put("/exshell")
async def create_item( item: Item, q: str = None):
    # 配置 Linux 服务器信息
    host = '119.29.146.254'
    port = 22
    username = 'root'
    password = 'zrd1236987!'
    # 要执行的命令

    result = execute_command_on_linux(host, port, username, password, item.sql)

    return result


@app.put("/pdf2md")
async def pdf2md(item: Pdf2md, q: str = None):
    pdf_url = 'http://42.194.163.46:9007/ywd/%E6%89%AB%E6%8F%8F%E5%85%A8%E8%83%BD%E7%8E%8B%202025-03-20%2021.38.pdf'
    prompt = "提取有效信息，只输出图片有效信息无效信息不用。"

    result = pdf2ai(item.pdfUrl, item.prompt, item.pageNum)

    return result

@app.put("/pdf2md")
async def saved2text(item: Saved2text, q: str = None):
    pdf_url = 'http://42.194.163.46:9007/ywd/%E6%89%AB%E6%8F%8F%E5%85%A8%E8%83%BD%E7%8E%8B%202025-03-20%2021.38.pdf'
    prompt = "提取有效信息，只输出图片有效信息无效信息不用。"

    result = save_html_to_file(item.htmlContent, item.fileName)

    return result


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=29015, timeout_keep_alive=600)
