Spaces:
Sleeping
Sleeping
true collection
Browse files
app.py
CHANGED
|
@@ -5,63 +5,63 @@ import pandas as pd
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from qdrant_client import QdrantClient
|
| 7 |
from qdrant_client.models import Filter, FieldCondition, MatchValue
|
| 8 |
-
|
| 9 |
import os
|
| 10 |
-
|
| 11 |
|
| 12 |
qdrant_client = QdrantClient(
|
| 13 |
url=os.environ.get("Qdrant_url"),
|
| 14 |
-
api_key=os.environ.get("Qdrant_api")
|
| 15 |
)
|
| 16 |
|
| 17 |
# โมเดลที่โหลดล่วงหน้า
|
| 18 |
models = {
|
| 19 |
"E5 (intfloat/multilingual-e5-small)": SentenceTransformer('intfloat/multilingual-e5-small'),
|
| 20 |
-
"
|
| 21 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
# Global memory to hold feedback state
|
| 25 |
latest_query_result = {"query": "", "result": "", "model": ""}
|
| 26 |
|
| 27 |
-
|
| 28 |
-
# 🔍 Search Functions
|
| 29 |
-
def search_with_e5(query):
|
| 30 |
-
embed = models["E5 (intfloat/multilingual-e5-small)"].encode("query: " + query)
|
| 31 |
-
return embed
|
| 32 |
-
|
| 33 |
-
def search_with_minilm(query):
|
| 34 |
-
embed = models["MiniLM (paraphrase-multilingual-MiniLM-L12-v2)"].encode(query)
|
| 35 |
-
return embed
|
| 36 |
-
|
| 37 |
-
def search_with_distiluse(query):
|
| 38 |
-
embed = models["DistilUSE (distiluse-base-multilingual-cased-v1)"].encode(query)
|
| 39 |
-
return embed
|
| 40 |
-
|
| 41 |
-
|
| 42 |
# 🌟 Main search function
|
| 43 |
def search_product(query, model_name):
|
| 44 |
start_time = time.time()
|
| 45 |
|
| 46 |
-
|
| 47 |
-
if "E5" in model_name:
|
| 48 |
-
query_embed = search_with_e5(query)
|
| 49 |
-
elif "MiniLM" in model_name:
|
| 50 |
-
query_embed = search_with_minilm(query)
|
| 51 |
-
elif "DistilUSE" in model_name:
|
| 52 |
-
query_embed = search_with_distiluse(query)
|
| 53 |
-
else:
|
| 54 |
return "❌ ไม่พบโมเดล"
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Query Qdrant
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
elapsed = time.time() - start_time
|
| 67 |
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from qdrant_client import QdrantClient
|
| 7 |
from qdrant_client.models import Filter, FieldCondition, MatchValue
|
|
|
|
| 8 |
import os
|
| 9 |
+
|
| 10 |
|
| 11 |
qdrant_client = QdrantClient(
|
| 12 |
url=os.environ.get("Qdrant_url"),
|
| 13 |
+
api_key=os.environ.get("Qdrant_api"),
|
| 14 |
)
|
| 15 |
|
| 16 |
# โมเดลที่โหลดล่วงหน้า
|
| 17 |
models = {
|
| 18 |
"E5 (intfloat/multilingual-e5-small)": SentenceTransformer('intfloat/multilingual-e5-small'),
|
| 19 |
+
"E5 large instruct (multilingual-e5-large-instruct)": SentenceTransformer("intfloat/multilingual-e5-large-instruct"),
|
| 20 |
+
"Kalm (KaLM-embedding-multilingual-mini-v1)": SentenceTransformer('HIT-TMG/KaLM-embedding-multilingual-mini-v1')
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
model_config = {
|
| 24 |
+
"E5 (intfloat/multilingual-e5-small)": {
|
| 25 |
+
"func": lambda query: models["E5 (intfloat/multilingual-e5-small)"].encode("query: " + query),
|
| 26 |
+
"collection": "product_E5"
|
| 27 |
+
},
|
| 28 |
+
"E5 large instruct (multilingual-e5-large-instruct)": {
|
| 29 |
+
"func": lambda query: models["E5 large instruct (multilingual-e5-large-instruct)"].encode(
|
| 30 |
+
"Instruct: Given a product search query, retrieve relevant product listings\nQuery: " + query, convert_to_tensor=False, normalize_embeddings=True),
|
| 31 |
+
"collection": "product_E5_large_instruct"
|
| 32 |
+
},
|
| 33 |
+
"Kalm (KaLM-embedding-multilingual-mini-v1)": {
|
| 34 |
+
"func": lambda query: models["Kalm (KaLM-embedding-multilingual-mini-v1)"].encode(query, normalize_embeddings=True),
|
| 35 |
+
"collection": "product_kalm"
|
| 36 |
+
}
|
| 37 |
}
|
| 38 |
|
| 39 |
# Global memory to hold feedback state
|
| 40 |
latest_query_result = {"query": "", "result": "", "model": ""}
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# 🌟 Main search function
|
| 43 |
def search_product(query, model_name):
|
| 44 |
start_time = time.time()
|
| 45 |
|
| 46 |
+
if model_name not in model_config:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
return "❌ ไม่พบโมเดล"
|
| 48 |
|
| 49 |
+
query_embed = model_config[model_name]["func"](query)
|
| 50 |
+
collection_name = model_config[model_name]["collection"]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
# Query Qdrant
|
| 54 |
+
try:
|
| 55 |
+
result = qdrant_client.query_points(
|
| 56 |
+
collection_name=collection_name,
|
| 57 |
+
query=query_embed.tolist(),
|
| 58 |
+
with_payload=True,
|
| 59 |
+
query_filter=Filter(
|
| 60 |
+
must=[FieldCondition(key="type", match=MatchValue(value="product"))]
|
| 61 |
+
)
|
| 62 |
+
).points
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"❌ Qdrant error: {str(e)}"
|
| 65 |
|
| 66 |
elapsed = time.time() - start_time
|
| 67 |
|