Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
import faiss
|
4 |
import numpy as np
|
@@ -7,12 +9,21 @@ app = FastAPI()
|
|
7 |
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
8 |
index = faiss.IndexFlatL2(384) # 384 is the dimensionality of the MiniLM model
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
@app.post("/embed")
|
15 |
def embed_string(text: str):
|
16 |
embedding = model.encode([text])
|
17 |
index.add(np.array(embedding))
|
18 |
return {"message": "String embedded and added to FAISS database"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.staticfiles import StaticFiles
|
3 |
+
from fastapi.templating import Jinja2Templates
|
4 |
from sentence_transformers import SentenceTransformer
|
5 |
import faiss
|
6 |
import numpy as np
|
|
|
9 |
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
10 |
index = faiss.IndexFlatL2(384) # 384 is the dimensionality of the MiniLM model
|
11 |
|
12 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
13 |
+
templates = Jinja2Templates(directory="templates")
|
14 |
+
|
15 |
+
@app.get("/")
|
16 |
+
def read_root(request: Request):
|
17 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
18 |
|
19 |
@app.post("/embed")
|
20 |
def embed_string(text: str):
|
21 |
embedding = model.encode([text])
|
22 |
index.add(np.array(embedding))
|
23 |
return {"message": "String embedded and added to FAISS database"}
|
24 |
+
|
25 |
+
@app.post("/search")
|
26 |
+
def search_string(text: str, n: int = 5):
|
27 |
+
embedding = model.encode([text])
|
28 |
+
distances, indices = index.search(np.array(embedding), n)
|
29 |
+
return {"distances": distances[0].tolist(), "indices": indices[0].tolist()}
|