-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (28 loc) · 1.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import asyncio
import datetime
import json
from typing import AsyncGenerator, Dict, List
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import StreamingResponse
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
import uvicorn
app = FastAPI()
templates = Jinja2Templates(directory="templates") # 设置模板目录
class Message(BaseModel):
content: str
async def generate_stream_response():
while True:
message = Message(
content=f"time from server:{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
)
yield f"data: {json.dumps(message.dict())}\n\n"
await asyncio.sleep(1)
@app.get("/nice_gui/message_channel")
async def chat_completions(request: Request):
return StreamingResponse(generate_stream_response(), media_type="text/event-stream")
@app.get("/")
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
if __name__ == "__main__":
uvicorn.run(app="main:app", host="127.0.0.1", port=5000, workers=3)