Almaatla commited on
Commit
a0edacc
·
verified ·
1 Parent(s): a953fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from fastapi import FastAPI, Request, Query
2
  from fastapi.templating import Jinja2Templates
 
3
  from sentence_transformers import SentenceTransformer
4
  import faiss
5
  import numpy as np
@@ -10,15 +11,19 @@ index = faiss.IndexFlatL2(384) # 384 is the dimensionality of the MiniLM model
10
 
11
  templates = Jinja2Templates(directory=".")
12
 
 
 
 
 
13
  @app.get("/")
14
  def read_root(request: Request):
15
  return templates.TemplateResponse("index.html", {"request": request})
16
 
17
  @app.post("/embed")
18
- def embed_string(text: str):
19
- embedding = model.encode([text])
20
- index.add(np.array(embedding))
21
- return {"message": "String embedded and added to FAISS database"}
22
 
23
  @app.post("/search")
24
  def search_string(text: str, n: int = 5):
 
1
  from fastapi import FastAPI, Request, Query
2
  from fastapi.templating import Jinja2Templates
3
+ from pydantic import BaseModel
4
  from sentence_transformers import SentenceTransformer
5
  import faiss
6
  import numpy as np
 
11
 
12
  templates = Jinja2Templates(directory=".")
13
 
14
+ class EmbedRequest(BaseModel):
15
+ texts: list[str]
16
+
17
+
18
  @app.get("/")
19
  def read_root(request: Request):
20
  return templates.TemplateResponse("index.html", {"request": request})
21
 
22
  @app.post("/embed")
23
+ def embed_strings(request: EmbedRequest):
24
+ embeddings = model.encode(request.texts)
25
+ index.add(np.array(embeddings))
26
+ return {"message": "Strings embedded and added to FAISS database"}
27
 
28
  @app.post("/search")
29
  def search_string(text: str, n: int = 5):