-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (48 loc) · 1.69 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
from io import BytesIO
from uuid import uuid1
import uvicorn
from fastapi import FastAPI, UploadFile
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import StreamingResponse
from service.content_parser import parse_content
from service.ppt_gen import PPTGenerator
app = FastAPI()
# {upload_id: PresentationFile}
app.cache_storage = dict()
origins = [
"http://localhost",
"http://localhost:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/test")
async def test():
return {"message": "Congrats, tests passed"}
# UploadFile
@app.post("/upload")
async def upload_file(file: UploadFile, prompt: str = '', template: str = '', file_type: str = ''):
content = parse_content(file)
generator = PPTGenerator(content, prompt, template, file_type)
upload_id = str(uuid1())
pre_file = generator.generate()
app.cache_storage[upload_id] = pre_file
return {"filename": file.filename, 'upload_id': upload_id}
@app.get("/download")
async def download_file(upload_id: str) -> StreamingResponse:
output = BytesIO()
app.cache_storage[upload_id].save(output)
output.seek(0)
# FIXME: pop out the file when it is downloaded for safety
# app.cache_storage.pop(upload_id)
return StreamingResponse(output, media_type='application/octet-stream')
if __name__ == '__main__':
from util.config import Config
logging.basicConfig(level=logging.DEBUG)
logging.debug(f"Config loaded: openai_key -> {Config.OPEN_AI_API_KEY}, unsplash_key -> {Config.UNSPLASH_API_KEY}")
uvicorn.run(app, host="127.0.0.1", port=4000)