File size: 8,495 Bytes
28b8e02
 
 
 
 
 
 
 
9ddaa27
ee48c56
7c23eb0
 
9ddaa27
 
39b722c
dbd7784
5629bb7
6d417ec
28b8e02
41cf03d
 
c05c4ca
28b8e02
 
6d417ec
41cf03d
 
6d417ec
7c23eb0
 
 
6d417ec
41cf03d
9ddaa27
 
08defce
 
 
 
 
 
 
 
 
 
 
dbd7784
08defce
 
 
 
dbd7784
 
6d417ec
374e7c8
 
 
dbd7784
 
 
 
 
 
9ddaa27
374e7c8
 
9ddaa27
6d417ec
9ddaa27
50c341d
 
6d417ec
50c341d
 
 
 
 
 
 
 
 
 
374e7c8
 
 
 
 
 
 
 
 
 
 
50c341d
 
 
 
 
 
 
 
dbd7784
50c341d
6d417ec
 
50c341d
08defce
 
28b8e02
cddab55
08defce
 
 
 
 
 
 
6d417ec
08defce
5629bb7
 
dbd7784
49c543f
 
 
 
66a3591
d3b2a2e
49c543f
5629bb7
66a3591
28b8e02
dbd7784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28b8e02
8c1aede
49c543f
66a3591
6d417ec
 
08defce
28b8e02
08defce
6d417ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c1aede
6d417ec
 
66a3591
8c1aede
28b8e02
c68ca70
6d417ec
 
28b8e02
6d417ec
 
 
 
 
28b8e02
6d417ec
28b8e02
08defce
 
7c23eb0
66a3591
7c23eb0
08defce
7c23eb0
cddab55
7c23eb0
 
ef6809f
7c23eb0
 
 
 
 
28b8e02
6d417ec
28b8e02
 
 
08defce
 
 
 
6d417ec
28b8e02
 
 
 
 
 
 
08defce
 
 
28b8e02
6d417ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import gradio as gr
import time
from datetime import datetime
import pandas as pd
from sentence_transformers import SentenceTransformer
from qdrant_client import QdrantClient
from qdrant_client.models import Filter, FieldCondition, MatchValue
import os
from rapidfuzz import process, fuzz
from pythainlp.tokenize import word_tokenize
from pyairtable import Table
from pyairtable import Api
import pickle
import re
import unicodedata
from FlagEmbedding import FlagReranker

# Setup Qdrant Client
qdrant_client = QdrantClient(
    url=os.environ.get("Qdrant_url"),
    api_key=os.environ.get("Qdrant_api"),
    timeout=30.0
)

# Airtable Config
AIRTABLE_API_KEY = os.environ.get("airtable_api")
BASE_ID = os.environ.get("airtable_baseid")
TABLE_NAME = "Feedback_search"
api = Api(AIRTABLE_API_KEY)
table = api.table(BASE_ID, TABLE_NAME)

# Load whitelist
with open("keyword_whitelist.pkl", "rb") as f:
    keyword_whitelist = pickle.load(f)

# Preload Models
models = {
    "E5 Finetuned": {
        "model": SentenceTransformer("e5_finetuned"),
        "collection": "product_E5_finetune",
        "threshold": 0.8,
        "prefix": "query: "
    },
    "BGE M3": {
        "model": SentenceTransformer("BAAI/bge-m3"),
        "collection": "product_bge-m3",
        "threshold": 0.45,
        "prefix": ""
    }
}

reranker = FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=True)

# Utils
def is_non_thai(text):
    return re.match(r'^[A-Za-z0-9&\-\s]+$', text) is not None

def join_corrected_tokens(corrected: list) -> str:
    if corrected and is_non_thai("".join(corrected)):
        return " ".join([w for w in corrected if len(w) > 1 or w in keyword_whitelist])
    else:
        return "".join([w for w in corrected if len(w) > 1 or w in keyword_whitelist])

def normalize(text: str) -> str:
    if is_non_thai(text):
        return text.strip()
    text = unicodedata.normalize("NFC", text)
    return text.replace("เแ", "แ").replace("เเ", "แ").strip().lower()

def smart_tokenize(text: str) -> list:
    tokens = word_tokenize(text.strip(), engine="newmm")
    return tokens if tokens and len("".join(tokens)) >= len(text.strip()) * 0.5 else [text.strip()]

def correct_query_merge_phrases(query: str, whitelist, threshold=80, max_ngram=3):
    query_norm = normalize(query)
    tokens = smart_tokenize(query_norm)
    corrected = []
    i = 0
    while i < len(tokens):
        matched = False
        for n in range(min(max_ngram, len(tokens) - i), 0, -1):
            phrase = "".join(tokens[i:i+n])
            if phrase in whitelist:
                corrected.append(phrase)
                i += n
                matched = True
                break
            match, score, _ = process.extractOne(
                phrase,
                whitelist,
                scorer=fuzz.token_sort_ratio,
                processor=lambda x: x.lower()
            )
            if score >= threshold:
                corrected.append(match)
                i += n
                matched = True
                break
        if not matched:
            corrected.append(tokens[i])
            i += 1
    return join_corrected_tokens(corrected)

# Global state
latest_query_result = {"query": "", "result": "", "raw_query": "", "time": ""}

# Search Function
def search_product(query, model_choice):
    start_time = time.time()
    latest_query_result["raw_query"] = query

    selected = models[model_choice]
    model = selected["model"]
    collection_name = selected["collection"]
    threshold = selected["threshold"]
    prefix = selected["prefix"]

    corrected_query = correct_query_merge_phrases(query, keyword_whitelist)
    query_embed = model.encode(prefix + corrected_query)

    try:
        # 🔍 ดึง top-50 ก่อน rerank
        result = qdrant_client.query_points(
            collection_name=collection_name,
            query=query_embed.tolist(),
            with_payload=True,
            query_filter=Filter(must=[FieldCondition(key="type", match=MatchValue(value="product"))]),
            limit=50
        ).points
    except Exception as e:
        return f"<p>❌ Qdrant error: {str(e)}</p>"

    # ✅ Rerank Top 10 ด้วย Cross-Encoder (เฉพาะ BGE M3 เท่านั้น)
    if model_choice == "BGE M3" and len(result) > 0:
        topk = 10
        docs = [r.payload.get("name", "") for r in result[:topk]]
        pairs = [[corrected_query, d] for d in docs]
        scores = reranker.compute_score(pairs, normalize=True)

        # ผสมคะแนน: 0.6 จาก embedding, 0.4 จาก reranker
        result[:topk] = sorted(
            zip(result[:topk], scores),
            key=lambda x: 0.6 * x[0].score + 0.4 * x[1],
            reverse=True
        )
        result[:topk] = [r[0] for r in result[:topk]]

    elapsed = time.time() - start_time
    html_output = f"<p>⏱ <strong>{elapsed:.2f} วินาที</strong></p>"
    if corrected_query != query:
        html_output += f"<p>🔧 แก้คำค้นจาก: <code>{query}</code> → <code>{corrected_query}</code></p>"
    html_output += '<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 20px;">'
    result_summary, found = "", False

    for res in result:
        if res.score >= threshold:
            found = True
            name = res.payload.get("name", "ไม่ทราบชื่อสินค้า")
            score = f"{res.score:.4f}"
            img_url = res.payload.get("imageUrl", "")
            price = res.payload.get("price", "ไม่ระบุ")
            brand = res.payload.get("brand", "")

            html_output += f"""
            <div style="border: 1px solid #ddd; border-radius: 8px; padding: 10px; text-align: center; box-shadow: 1px 1px 5px rgba(0,0,0,0.1); background: #fff;">
                <img src="{img_url}" style="width: 100%; max-height: 150px; object-fit: contain; border-radius: 4px;">
                <div style="margin-top: 10px;">
                    <div style="font-weight: bold; font-size: 14px;">{name}</div>
                    <div style="color: gray; font-size: 12px;">{brand}</div>
                    <div style="color: green; margin: 4px 0;">฿{price}</div>
                    <div style="font-size: 12px; color: #555;">score: {score}</div>
                </div>
            </div>
            """
            result_summary += f"{name} (score: {score}) | "

    html_output += "</div>"

    if not found:
        html_output += '<div style="text-align: center; font-size: 18px; color: #a00; padding: 30px;">❌ ไม่พบสินค้าที่เกี่ยวข้องกับคำค้นนี้</div>'
        return html_output

    latest_query_result.update({
        "query": corrected_query,
        "result": result_summary.strip(),
        "time": elapsed,
    })

    return html_output

# Feedback Function
def log_feedback(feedback, model_choice):
    try:
        now = datetime.now().strftime("%Y-%m-%d")
        table.create({
            "model": model_choice,
            "timestamp": now,
            "raw_query": latest_query_result["raw_query"],
            "query": latest_query_result["query"],
            "result": latest_query_result["result"],
            "time(second)": latest_query_result["time"],
            "feedback": feedback
        })
        return "✅ Feedback saved to Airtable!"
    except Exception as e:
        return f"❌ Failed to save feedback: {str(e)}"

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🔎 Product Semantic Search (Vector Search + Qdrant)")

    with gr.Row():
        model_selector = gr.Dropdown(label="🔍 เลือกโมเดล", choices=list(models.keys()), value="E5 Finetuned")
        query_input = gr.Textbox(label="พิมพ์คำค้นหา")

    result_output = gr.HTML(label="📋 ผลลัพธ์")

    with gr.Row():
        match_btn = gr.Button("✅ ตรง")
        not_match_btn = gr.Button("❌ ไม่ตรง")

    feedback_status = gr.Textbox(label="📬 สถานะ Feedback")

    query_input.submit(search_product, inputs=[query_input, model_selector], outputs=result_output)
    match_btn.click(fn=lambda model: log_feedback("match", model), inputs=model_selector, outputs=feedback_status)
    not_match_btn.click(fn=lambda model: log_feedback("not_match", model), inputs=model_selector, outputs=feedback_status)

demo.launch(share=True)