-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
96 lines (76 loc) · 2.23 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from dataclasses import asdict, dataclass
import io
from uuid import uuid4
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import meilisearch
import copy
import shutil
from pydantic import BaseModel, Field
from env import MEILISEARCH_MASTER_KEY, MEILISEARCH_URI
from file_upload import upload_file
from pdf import get_pdf_content_tags
app = FastAPI(
docs_url="/docs",
title="NoteWise V0.0.1",
)
origins = [
"http://localhost:3000",
"http://www.notewise.study",
"https://www.notewise.study",
"http://notewise.study",
"https://notewise.study",
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
max_age=3600,
)
# Initialize MeiliSearch
client = meilisearch.Client(
MEILISEARCH_URI,
MEILISEARCH_MASTER_KEY,
)
@app.get("/")
async def hello():
return "Hello World"
@app.post("/files")
async def create_file(file: UploadFile):
file_obj = io.BytesIO(file.file.read())
try:
pdf = await add_to_meilisearch(file.filename, file_obj)
upload_file(file_obj, pdf)
finally:
file.file.close()
return pdf
async def add_to_meilisearch(filename: str, file: io.BytesIO):
content = await get_pdf_content_tags(filename, file)
pdf_index = client.index("pdfs")
pdf_page_index = client.index("pdf_pages")
task = pdf_page_index.add_documents(
[asdict(document) for document in content.documents], primary_key="page_id"
)
task = pdf_index.add_documents(asdict(content.pdf), primary_key="pdf_id")
return content.pdf
def delete_all(index: str):
content = client.index(index)
content.delete_all_documents()
content.delete()
class NoteItem(BaseModel):
note_id: str = Field(default_factory=lambda: uuid4().__str__())
description: str
pdf_id: str
page_id: str
page_number: int
@app.post("/notes")
async def create_note(note: NoteItem):
note_index = client.index("notes")
task = note_index.add_documents(asdict(note), primary_key="note_id")
return note
if __name__ == "__main__":
import uvicorn
uvicorn.run("app", host="0.0.0.0", port=8000, reload=True)