-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
40 lines (26 loc) · 932 Bytes
/
api.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
import json
from fastapi import FastAPI
from pydantic import BaseModel
from index_generator import generate_index
from index_queries import compute_vector_score
# generate_index()
class Item(BaseModel):
query: str
k: int
with open("./index_output/filenames.json", "r", encoding='utf-8') as file:
filenames_dict = json.load(file)
app = FastAPI()
@app.post("/api/index/")
def post_api(item: Item):
results = compute_vector_score(item.query, "./index_output/merged_weighted_index.txt", item.k)
response = {}
for i, (doc_id, score) in enumerate(results):
filenames_result = filenames_dict[f"doc{doc_id}"]
response[f"doc{i}"] = {
"score": score,
"id": filenames_result["id"],
"name": filenames_result["name"],
"artist": filenames_result["artist"],
"album_name": filenames_result["album_name"]
}
return response