-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmain.py
67 lines (56 loc) · 1.75 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
64
65
66
67
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from handlers.processor import process_file
from config import *
from handlers.ocr import create_ocr_task, deprecated_could_enable_ocr
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=CORS_ALLOW_ORIGINS,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def root():
return FileResponse("index.html", media_type="text/html")
@app.get("/favicon.ico")
def favicon():
return FileResponse("favicon.ico")
@app.post("/upload")
async def upload(
file: UploadFile = File(...),
enable_ocr: bool = Form(default=False),
enable_vision: bool = Form(default=True),
save_all: bool = Form(default=False),
model: str = Form(default=""), # deprecated
):
"""Accepts file and returns its contents."""
if model and len(model) > 0:
# compatibility with deprecated model parameter
enable_ocr = deprecated_could_enable_ocr(model)
enable_vision = not enable_ocr
if len(OCR_ENDPOINT) == 0:
enable_ocr = False
try:
filetype, contents = await process_file(
file,
enable_ocr=enable_ocr,
enable_vision=enable_vision,
save_all=save_all,
)
return {
"status": True,
"content": contents,
"type": filetype,
"error": "",
}
except Exception as e:
return {
"status": False,
"content": "",
"type": "error",
"error": str(e),
}