Spaces:
Runtime error

dzenzzz commited on
Commit
b7dc427
·
1 Parent(s): 5fcd040

update to configuration

Browse files
Files changed (2) hide show
  1. app.py +5 -5
  2. neural_searcher.py +25 -30
app.py CHANGED
@@ -1,13 +1,13 @@
1
  from fastapi import FastAPI
2
  from neural_searcher import NeuralSearcher
 
 
3
 
4
- app = FastAPI()
5
 
6
- neural_searcher = NeuralSearcher(collection_name="odluke-demo")
7
 
8
- @app.get("/")
9
- async def root():
10
- return {"message": "Hello World"}
11
 
12
  @app.get("/api/search")
13
  def search_startup(q: str):
 
1
  from fastapi import FastAPI
2
  from neural_searcher import NeuralSearcher
3
+ from huggingface_hub import login
4
+ import os
5
 
6
+ login(os.getenv('HUGGING_FACE_API_KEY'))
7
 
8
+ app = FastAPI()
9
 
10
+ neural_searcher = NeuralSearcher(collection_name=os.getenv('COLLECTION_NAME'))
 
 
11
 
12
  @app.get("/api/search")
13
  def search_startup(q: str):
neural_searcher.py CHANGED
@@ -1,46 +1,41 @@
1
  from qdrant_client import QdrantClient
2
  from fastembed import SparseTextEmbedding
3
- from huggingface_hub import login
4
- import os
5
  from qdrant_client import QdrantClient, models
6
  from sentence_transformers import SentenceTransformer
 
7
 
8
- # from config import API_KEY,HOST,DENSE_MODEL,SPARSE_MODEL,DENSE_MODEL_SHORT,SPARSE_MODEL_SHORT
9
  class NeuralSearcher:
10
 
11
  def __init__(self, collection_name):
12
  self.collection_name = collection_name
13
- self.dense_model = SentenceTransformer("dzenzzz/mne-lawic",device="cpu")
14
- self.sparse_model = SparseTextEmbedding("Qdrant/bm25")
15
- # self.qdrant_client = QdrantClient("http://localhost:6333/",api_key="")
16
 
17
- login(os.getenv('HUGGING_FACE_API_KEY'))
18
-
19
  def search(self, text: str):
20
 
21
  dense_query = self.dense_model.encode(text).tolist()
22
  sparse_query = self.sparse_model.query_embed(text)
23
 
24
- # # Use `vector` for search for closest vectors in the collection
25
- # search_result = self.qdrant_client.query_points(
26
- # collection_name= self.collection_name,
27
- # prefetch=[
28
- # models.Prefetch(
29
- # query=dense_query,
30
- # using="djovak/embedic-small",
31
- # limit=5
32
- # ),
33
- # models.Prefetch(
34
- # query=next(sparse_query).as_object(),
35
- # using="Qdrant/bm25",
36
- # limit=5
37
- # )
38
- # ],
39
- # query=models.FusionQuery(
40
- # fusion=models.Fusion.RRF
41
- # ),
42
- # limit = 9
43
- # ).points
44
 
45
- # payloads = [hit.payload for hit in search_result]
46
- return dense_query
 
1
  from qdrant_client import QdrantClient
2
  from fastembed import SparseTextEmbedding
 
 
3
  from qdrant_client import QdrantClient, models
4
  from sentence_transformers import SentenceTransformer
5
+ import os
6
 
 
7
  class NeuralSearcher:
8
 
9
  def __init__(self, collection_name):
10
  self.collection_name = collection_name
11
+ self.dense_model = SentenceTransformer(os.getenv('DENSE_MODEL'),device="cpu")
12
+ self.sparse_model = SparseTextEmbedding(os.getenv('SPARSE_MODEL'))
13
+ self.qdrant_client = QdrantClient(os.getenv('QDRANT_URL'),api_key=os.getenv('QDRANT_API_KEY'))
14
 
 
 
15
  def search(self, text: str):
16
 
17
  dense_query = self.dense_model.encode(text).tolist()
18
  sparse_query = self.sparse_model.query_embed(text)
19
 
20
+ search_result = self.qdrant_client.query_points(
21
+ collection_name= self.collection_name,
22
+ prefetch=[
23
+ models.Prefetch(
24
+ query=dense_query,
25
+ using=os.getenv('DENSE_MODEL'),
26
+ limit=5
27
+ ),
28
+ models.Prefetch(
29
+ query=next(sparse_query).as_object(),
30
+ using=os.getenv('SPARSE_MODEL'),
31
+ limit=5
32
+ )
33
+ ],
34
+ query=models.FusionQuery(
35
+ fusion=models.Fusion.RRF
36
+ ),
37
+ limit = 9
38
+ ).points
 
39
 
40
+ payloads = [hit.payload for hit in search_result]
41
+ return payloads