Spaces:
Runtime error
Runtime error
update to configuration
Browse files- app.py +5 -5
- 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 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 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(
|
| 14 |
-
self.sparse_model = SparseTextEmbedding(
|
| 15 |
-
|
| 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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# ).points
|
| 44 |
|
| 45 |
-
|
| 46 |
-
return
|
|
|
|
| 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
|