Spaces:
Running
Running
add picture in search result
Browse files
app.py
CHANGED
@@ -18,8 +18,8 @@ qdrant_client = QdrantClient(
|
|
18 |
)
|
19 |
|
20 |
AIRTABLE_API_KEY = os.environ.get("airtable_api")
|
21 |
-
BASE_ID = os.environ.get("airtable_baseid")
|
22 |
-
TABLE_NAME = "Feedback_search"
|
23 |
api = Api(AIRTABLE_API_KEY)
|
24 |
table = api.table(BASE_ID, TABLE_NAME)
|
25 |
|
@@ -33,16 +33,16 @@ models = {
|
|
33 |
model_config = {
|
34 |
"E5 (intfloat/multilingual-e5-small)": {
|
35 |
"func": lambda query: models["E5 (intfloat/multilingual-e5-small)"].encode("query: " + query),
|
36 |
-
"collection": "product_E5"
|
37 |
},
|
38 |
"E5 large instruct (multilingual-e5-large-instruct)": {
|
39 |
"func": lambda query: models["E5 large instruct (multilingual-e5-large-instruct)"].encode(
|
40 |
"Instruct: Given a product search query, retrieve relevant product listings\nQuery: " + query, convert_to_tensor=False, normalize_embeddings=True),
|
41 |
-
"collection": "product_E5_large_instruct"
|
42 |
},
|
43 |
"Kalm (KaLM-embedding-multilingual-mini-v1)": {
|
44 |
"func": lambda query: models["Kalm (KaLM-embedding-multilingual-mini-v1)"].encode(query, normalize_embeddings=True),
|
45 |
-
"collection": "product_kalm"
|
46 |
}
|
47 |
}
|
48 |
|
@@ -81,11 +81,9 @@ def correct_query_with_symspell(query: str) -> str:
|
|
81 |
# 🌟 Main search function
|
82 |
def search_product(query, model_name):
|
83 |
start_time = time.time()
|
84 |
-
|
85 |
if model_name not in model_config:
|
86 |
-
return "
|
87 |
|
88 |
-
# ✨ แทรกขั้นตอน fuzzy correction
|
89 |
latest_query_result["raw_query"] = query
|
90 |
corrected_query = correct_query_with_symspell(query)
|
91 |
|
@@ -97,38 +95,43 @@ def search_product(query, model_name):
|
|
97 |
collection_name=collection_name,
|
98 |
query=query_embed.tolist(),
|
99 |
with_payload=True,
|
100 |
-
query_filter=Filter(
|
101 |
-
must=[FieldCondition(key="type", match=MatchValue(value="product"))]
|
102 |
-
),
|
103 |
limit=10
|
104 |
).points
|
105 |
except Exception as e:
|
106 |
-
return f"
|
107 |
|
108 |
elapsed = time.time() - start_time
|
109 |
-
|
110 |
-
output = f"⏱ Time: {elapsed:.2f}s\n"
|
111 |
if corrected_query != query:
|
112 |
-
|
113 |
-
|
114 |
|
115 |
result_summary = ""
|
116 |
for res in result:
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
latest_query_result["query"] = corrected_query
|
122 |
latest_query_result["result"] = result_summary.strip()
|
123 |
latest_query_result["model"] = model_name
|
124 |
|
125 |
-
return
|
126 |
|
127 |
|
128 |
# 📝 Logging feedback
|
129 |
def log_feedback(feedback):
|
130 |
try:
|
131 |
-
now = datetime.now().strftime("%Y-%m-%d")
|
132 |
table.create({
|
133 |
"timestamp": now,
|
134 |
"raw_query": latest_query_result["raw_query"],
|
@@ -154,7 +157,7 @@ with gr.Blocks() as demo:
|
|
154 |
)
|
155 |
query_input = gr.Textbox(label="พิมพ์คำค้นหา")
|
156 |
|
157 |
-
result_output = gr.
|
158 |
|
159 |
with gr.Row():
|
160 |
match_btn = gr.Button("✅ ตรง")
|
@@ -162,9 +165,7 @@ with gr.Blocks() as demo:
|
|
162 |
|
163 |
feedback_status = gr.Textbox(label="📬 สถานะ Feedback")
|
164 |
|
165 |
-
|
166 |
-
submit_fn = lambda q, m: search_product(q, m)
|
167 |
-
query_input.submit(submit_fn, inputs=[query_input, model_selector], outputs=result_output)
|
168 |
match_btn.click(lambda: log_feedback("match"), outputs=feedback_status)
|
169 |
not_match_btn.click(lambda: log_feedback("not_match"), outputs=feedback_status)
|
170 |
|
|
|
18 |
)
|
19 |
|
20 |
AIRTABLE_API_KEY = os.environ.get("airtable_api")
|
21 |
+
BASE_ID = os.environ.get("airtable_baseid")
|
22 |
+
TABLE_NAME = "Feedback_search" # หรือเปลี่ยนชื่อให้ชัดเช่น 'Feedback'
|
23 |
api = Api(AIRTABLE_API_KEY)
|
24 |
table = api.table(BASE_ID, TABLE_NAME)
|
25 |
|
|
|
33 |
model_config = {
|
34 |
"E5 (intfloat/multilingual-e5-small)": {
|
35 |
"func": lambda query: models["E5 (intfloat/multilingual-e5-small)"].encode("query: " + query),
|
36 |
+
"collection": "product_E5",
|
37 |
},
|
38 |
"E5 large instruct (multilingual-e5-large-instruct)": {
|
39 |
"func": lambda query: models["E5 large instruct (multilingual-e5-large-instruct)"].encode(
|
40 |
"Instruct: Given a product search query, retrieve relevant product listings\nQuery: " + query, convert_to_tensor=False, normalize_embeddings=True),
|
41 |
+
"collection": "product_E5_large_instruct",
|
42 |
},
|
43 |
"Kalm (KaLM-embedding-multilingual-mini-v1)": {
|
44 |
"func": lambda query: models["Kalm (KaLM-embedding-multilingual-mini-v1)"].encode(query, normalize_embeddings=True),
|
45 |
+
"collection": "product_kalm",
|
46 |
}
|
47 |
}
|
48 |
|
|
|
81 |
# 🌟 Main search function
|
82 |
def search_product(query, model_name):
|
83 |
start_time = time.time()
|
|
|
84 |
if model_name not in model_config:
|
85 |
+
return "<p>❌ ไม่พบโมเดล</p>"
|
86 |
|
|
|
87 |
latest_query_result["raw_query"] = query
|
88 |
corrected_query = correct_query_with_symspell(query)
|
89 |
|
|
|
95 |
collection_name=collection_name,
|
96 |
query=query_embed.tolist(),
|
97 |
with_payload=True,
|
98 |
+
query_filter=Filter(must=[FieldCondition(key="type", match=MatchValue(value="product"))]),
|
|
|
|
|
99 |
limit=10
|
100 |
).points
|
101 |
except Exception as e:
|
102 |
+
return f"<p>❌ Qdrant error: {str(e)}</p>"
|
103 |
|
104 |
elapsed = time.time() - start_time
|
105 |
+
html_output = f"<p>⏱ Time: {elapsed:.2f}s</p>"
|
|
|
106 |
if corrected_query != query:
|
107 |
+
html_output += f"<p>🔧 แก้คำค้นจาก: <code>{query}</code> → <code>{corrected_query}</code></p>"
|
108 |
+
html_output += "<h4>📦 ผลลัพธ์:</h4><ul style='list-style:none;'>"
|
109 |
|
110 |
result_summary = ""
|
111 |
for res in result:
|
112 |
+
name = res.payload.get("name", "ไม่ทราบชื่อสินค้า")
|
113 |
+
score = f"{res.score:.4f}"
|
114 |
+
img_url = res.payload.get("imageUrl", "")
|
115 |
+
|
116 |
+
html_output += "<li style='margin-bottom: 10px;'>"
|
117 |
+
if img_url:
|
118 |
+
html_output += f"<img src='{img_url}' width='100' style='margin-right:10px; vertical-align:middle;'>"
|
119 |
+
html_output += f"<strong>{name}</strong> (score: {score})</li>"
|
120 |
+
result_summary += f"{name} (score: {score}) | "
|
121 |
+
|
122 |
+
html_output += "</ul>"
|
123 |
|
124 |
latest_query_result["query"] = corrected_query
|
125 |
latest_query_result["result"] = result_summary.strip()
|
126 |
latest_query_result["model"] = model_name
|
127 |
|
128 |
+
return html_output
|
129 |
|
130 |
|
131 |
# 📝 Logging feedback
|
132 |
def log_feedback(feedback):
|
133 |
try:
|
134 |
+
now = datetime.now().strftime("%Y-%m-%d")
|
135 |
table.create({
|
136 |
"timestamp": now,
|
137 |
"raw_query": latest_query_result["raw_query"],
|
|
|
157 |
)
|
158 |
query_input = gr.Textbox(label="พิมพ์คำค้นหา")
|
159 |
|
160 |
+
result_output = gr.HTML(label="📋 ผลลัพธ์") # HTML แสดงผลลัพธ์พร้อมรูป
|
161 |
|
162 |
with gr.Row():
|
163 |
match_btn = gr.Button("✅ ตรง")
|
|
|
165 |
|
166 |
feedback_status = gr.Textbox(label="📬 สถานะ Feedback")
|
167 |
|
168 |
+
query_input.submit(search_product, inputs=[query_input, model_selector], outputs=result_output)
|
|
|
|
|
169 |
match_btn.click(lambda: log_feedback("match"), outputs=feedback_status)
|
170 |
not_match_btn.click(lambda: log_feedback("not_match"), outputs=feedback_status)
|
171 |
|