Spaces:
Running
Running
add fuzzy
Browse files
app.py
CHANGED
@@ -6,11 +6,14 @@ 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 |
# โมเดลที่โหลดล่วงหน้า
|
@@ -39,6 +42,16 @@ model_config = {
|
|
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()
|
@@ -46,35 +59,39 @@ def search_product(query, model_name):
|
|
46 |
if model_name not in model_config:
|
47 |
return "❌ ไม่พบโมเดล"
|
48 |
|
49 |
-
|
|
|
|
|
|
|
50 |
collection_name = model_config[model_name]["collection"]
|
51 |
-
|
52 |
|
53 |
-
# Query Qdrant
|
54 |
try:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
except Exception as e:
|
64 |
-
|
65 |
|
66 |
elapsed = time.time() - start_time
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
70 |
result_summary = ""
|
71 |
for res in result:
|
72 |
line = f"- {res.payload.get('name', '')} (score: {res.score:.4f})"
|
73 |
output += line + "\n"
|
74 |
result_summary += line + " | "
|
75 |
|
76 |
-
|
77 |
-
latest_query_result["query"] = query
|
78 |
latest_query_result["result"] = result_summary.strip()
|
79 |
latest_query_result["model"] = model_name
|
80 |
|
|
|
6 |
from qdrant_client import QdrantClient
|
7 |
from qdrant_client.models import Filter, FieldCondition, MatchValue
|
8 |
import os
|
9 |
+
from symspellpy.symspellpy import SymSpell, Verbosity
|
10 |
|
11 |
|
12 |
qdrant_client = QdrantClient(
|
13 |
+
#url=os.environ.get("Qdrant_url"),
|
14 |
+
#api_key=os.environ.get("Qdrant_api"),
|
15 |
+
url=userdata.get("Qdrant_url"),
|
16 |
+
api_key=userdata.get("Qdrant_api"),
|
17 |
)
|
18 |
|
19 |
# โมเดลที่โหลดล่วงหน้า
|
|
|
42 |
# Global memory to hold feedback state
|
43 |
latest_query_result = {"query": "", "result": "", "model": ""}
|
44 |
|
45 |
+
symspell = SymSpell(max_dictionary_edit_distance=2)
|
46 |
+
symspell.load_dictionary("symspell_dict_pythainlp.txt", term_index=0, count_index=1)
|
47 |
+
|
48 |
+
# แก้คำผิด
|
49 |
+
def correct_query_with_symspell(query: str) -> str:
|
50 |
+
suggestions = symspell.lookup_compound(query, Verbosity.CLOSEST, max_edit_distance=2)
|
51 |
+
if suggestions:
|
52 |
+
return suggestions[0].term
|
53 |
+
return query
|
54 |
+
|
55 |
# 🌟 Main search function
|
56 |
def search_product(query, model_name):
|
57 |
start_time = time.time()
|
|
|
59 |
if model_name not in model_config:
|
60 |
return "❌ ไม่พบโมเดล"
|
61 |
|
62 |
+
# ✨ แทรกขั้นตอน fuzzy correction
|
63 |
+
corrected_query = correct_query_with_symspell(query)
|
64 |
+
|
65 |
+
query_embed = model_config[model_name]["func"](corrected_query)
|
66 |
collection_name = model_config[model_name]["collection"]
|
|
|
67 |
|
|
|
68 |
try:
|
69 |
+
result = qdrant_client.query_points(
|
70 |
+
collection_name=collection_name,
|
71 |
+
query=query_embed.tolist(),
|
72 |
+
with_payload=True,
|
73 |
+
query_filter=Filter(
|
74 |
+
must=[FieldCondition(key="type", match=MatchValue(value="product"))]
|
75 |
+
),
|
76 |
+
limit=10
|
77 |
+
).points
|
78 |
except Exception as e:
|
79 |
+
return f"❌ Qdrant error: {str(e)}"
|
80 |
|
81 |
elapsed = time.time() - start_time
|
82 |
|
83 |
+
output = f"⏱ Time: {elapsed:.2f}s\n"
|
84 |
+
if corrected_query != query:
|
85 |
+
output += f"🔧 แก้คำค้นจาก: `{query}` → `{corrected_query}`\n\n"
|
86 |
+
output += f"📦 ผลลัพธ์:\n"
|
87 |
+
|
88 |
result_summary = ""
|
89 |
for res in result:
|
90 |
line = f"- {res.payload.get('name', '')} (score: {res.score:.4f})"
|
91 |
output += line + "\n"
|
92 |
result_summary += line + " | "
|
93 |
|
94 |
+
latest_query_result["query"] = corrected_query
|
|
|
95 |
latest_query_result["result"] = result_summary.strip()
|
96 |
latest_query_result["model"] = model_name
|
97 |
|