-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
51 lines (36 loc) · 1.23 KB
/
api.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
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import base64
from modal.functions import FunctionCall
from main import scrape_score, app
from modal import asgi_app
web_app = FastAPI()
web_app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"], # List of allowed origins
allow_credentials=True,
allow_methods=["*"], # Allow all HTTP methods
allow_headers=["*"], # Allow all headers
)
@app.function()
@asgi_app()
def fastapi_app():
return web_app
@web_app.post("/accept")
async def accept_video(video: UploadFile = File(...)):
video_bytes = await video.read()
call = scrape_score.spawn(video.filename, video_bytes)
return call.object_id
@web_app.get("/result/{call_id}")
async def poll_results(call_id: str):
function_call = FunctionCall.from_id(call_id)
try:
result = function_call.get(timeout=0)
pdfBytes = base64.b64encode(result)
return {"pdfBytes": pdfBytes}
except TimeoutError:
return JSONResponse({"status": "processing"}, status_code=202)
if __name__ == "__main__":
import uvicorn
uvicorn.run(web_app, host="0.0.0.0", port=8000)