forked from denys-potapov/doc-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (49 loc) · 1.79 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
68
69
70
71
72
"""Main entry point."""
from uuid import UUID
from fastapi import FastAPI, File, BackgroundTasks
from fastapi.concurrency import run_in_threadpool
from fastapi.openapi.utils import get_openapi
from ocr import get_pages
import db
import models
from pydantic import Json
app = FastAPI()
def openapi_schema():
schema = get_openapi(
title="Document search",
version="0.1",
description="Search documents from any forrmat",
routes=app.routes,
)
app.openapi_schema = schema
return app.openapi_schema
app.openapi = openapi_schema
@app.on_event("startup")
async def startup():
await db.database.connect()
@app.on_event("shutdown")
async def shutdown():
await db.database.disconnect()
async def process_document(document_id, stream: bytes):
"""Processs uploaded document."""
pages = await run_in_threadpool(lambda: get_pages(stream))
await db.update_document_text(document_id, pages)
@app.get("/documents/{document_id}", response_model=models.Document)
async def get_document(
document_id: UUID):
return await db.get_document(document_id)
@app.get(
"/documents/{document_id}/highlights",
response_model=list[models.Highlight])
async def highlights(document_id: UUID, query: str):
return await db.highlight_document(document_id, query)
@app.get("/search", response_model=list[models.SearchResult])
async def search(query: str, limit: int = 50, offset: int = 0):
return await db.search_documents(query, limit, offset)
@app.post("/documents/", response_model=models.Document)
async def create_document(
background_tasks: BackgroundTasks,
meta: Json, file: bytes = File()):
document = await db.create_document(meta)
background_tasks.add_task(process_document, document["id"], file)
return document