-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi_streaming.py
48 lines (35 loc) · 1.12 KB
/
api_streaming.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
42
43
44
45
46
47
48
import json
import logging
import time
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import StreamingResponse
from gpt_analyze import summary_text
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def fake_streamer():
for i in range(20):
time.sleep(0.5)
yield json.dumps({"data": i})
@app.get("/")
async def main():
return StreamingResponse(fake_streamer(), media_type="text/plain")
@app.post("/")
async def create_item(request: Request):
json_post_raw = await request.json()
json_post = json.dumps(json_post_raw)
json_post_list = json.loads(json_post)
text = json_post_list.get('text')
api_key = json_post_list.get('apiKey')
logging.info('input %s', text)
return StreamingResponse(fake_streamer())
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000, workers=1)