You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

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