|
|
|
import uvicorn
|
|
|
|
from fastapi import FastAPI, Body
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from fastapi.responses import StreamingResponse, PlainTextResponse
|
|
|
|
|
|
|
|
from Util import *
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
# 添加 CORS 中间件
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=["*"],
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# 根路由,返回提示信息
|
|
|
|
@app.get("/")
|
|
|
|
def root():
|
|
|
|
return PlainTextResponse("Hello ApiStream")
|
|
|
|
|
|
|
|
@app.post("/api/tools/aippt_outline") # 仅支持 POST 方法
|
|
|
|
async def aippt_outline(
|
|
|
|
course_name: str = Body(..., embed=True, description="课程名称") # 从请求体中获取 course_name
|
|
|
|
):
|
|
|
|
# 返回流式响应
|
|
|
|
return StreamingResponse(
|
|
|
|
generate_stream_markdown(course_name),
|
|
|
|
media_type="text/event-stream",
|
|
|
|
headers={
|
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"X-Accel-Buffering": "no"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@app.post("/api/tools/aippt") # 修改为 POST 方法
|
|
|
|
async def aippt(content: str = Body(..., embed=True, description="Markdown 内容")): # 使用 Body 接收请求体参数
|
|
|
|
return StreamingResponse(
|
|
|
|
convert_markdown_to_json(content), # 传入 content
|
|
|
|
media_type="text/plain", # 使用 text/plain 格式
|
|
|
|
headers={
|
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"X-Accel-Buffering": "no",
|
|
|
|
"Content-Type": "text/plain; charset=utf-8" # 明确设置 Content-Type
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# 运行应用
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# 获取本机所有 IPv4 地址
|
|
|
|
ips = get_local_ips()
|
|
|
|
if not ips:
|
|
|
|
print("无法获取本机 IP 地址,使用默认地址 127.0.0.1")
|
|
|
|
ips = ["127.0.0.1"]
|
|
|
|
|
|
|
|
# 打印所有 IP 地址
|
|
|
|
print("服务将在以下 IP 地址上运行:")
|
|
|
|
for ip in ips:
|
|
|
|
print(f"http://{ip}:5173")
|
|
|
|
|
|
|
|
# 启动 FastAPI 应用,绑定到所有 IP 地址
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=5173)
|