-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
49 lines (31 loc) · 960 Bytes
/
parser.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
"""
usage: python3 parser.py
"""
from contextlib import asynccontextmanager
from fastapi import FastAPI, UploadFile
from fastapi.responses import FileResponse
from sd_parsers import ParserManager
import webbrowser
import uvicorn
import os
PORT = int(os.environ.get("PORT", 8000))
parser_manager = ParserManager()
@asynccontextmanager
async def lifespan(app: FastAPI):
webbrowser.open(f"http://127.0.0.1:{PORT}")
yield
app = FastAPI(lifespan=lifespan)
@app.get("/")
def index():
return FileResponse("index.html")
@app.post("/api/parse")
def parse(image: UploadFile):
try:
info = parser_manager.parse(image.file)
if info is None:
return {"success": False, "error": "no metadata found"}
return {"success": True, "metadata": info}
except Exception as error:
return {"success": False, "error": str(error)}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=PORT)