-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
91 lines (73 loc) · 2.72 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import logging
from typing import Dict, List, Optional
import pandas as pd
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from tqdm import tqdm
from search.index_store import EmbeddingInMemoryIndexStore
from search.indexer import EmbeddingIndexer
from search.processor import EmbeddingProcessor
from search.searcher import EmbeddingSearcher
app = FastAPI()
log = logging.getLogger("SearchEngine")
@app.on_event("startup")
async def startup_event():
app.state.index_store = EmbeddingInMemoryIndexStore()
processor = EmbeddingProcessor(n=3)
app.state.indexer = EmbeddingIndexer(
processor=processor, index_store=app.state.index_store
)
app.state.searcher = EmbeddingSearcher(
processor=processor, index_store=app.state.index_store, similarity_threshold=0.2
)
try:
app.state.index_store.load(path="/documentSearch/data")
except Exception as e:
logging.info(e)
@app.on_event("shutdown")
def shutdown_event():
try:
app.state.index_store.save(path="/documentSearch/data")
except Exception as e:
log.error(e)
class Document(BaseModel):
"""Model that represents a document."""
extracted_at: str
id: str
lang: Optional[str] = "english"
text: str
@app.post("/index", status_code=200, tags=["index"])
def index_docs(documents: List[Document]):
indexer: EmbeddingIndexer = app.state.indexer
indexer.index_docs(documents)
return JSONResponse(
content={"msg": f"{len(documents)} have been indexed"}, status_code=200
)
@app.post("/query", status_code=200, tags=["query"])
def run_query(query: List[str]):
searcher: EmbeddingSearcher = app.state.searcher
query_result = searcher.search(query)
return JSONResponse(
content={res[0]: res[1] for res in query_result}, status_code=200
)
@app.post("/initiate", status_code=200, tags=["index"])
def initiate():
def load_documents(excel_path) -> Dict[str, Document]:
documents_df = pd.read_excel(excel_path, sheet_name="documents")
documents_df = documents_df[~documents_df.text.isnull()]
documents = {}
for i, row in tqdm(documents_df.iterrows(), desc="Loading documents"):
documents[row["id"]] = Document(
id=row["id"],
extracted_at=row["extracted"],
lang=row["lang"],
text=row["text"],
)
return documents
indexer: EmbeddingIndexer = app.state.indexer
documents = load_documents("/documentSearch/data/input/documents.xlsx")
indexer.index_docs(list(documents.values()))
return JSONResponse(
content={"msg": f"{len(documents)} have been indexed"}, status_code=200
)