set up project
Browse files- app.py +88 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
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 |
+
# Load model
|
| 10 |
+
model = SentenceTransformer('intfloat/multilingual-e5-small')
|
| 11 |
+
|
| 12 |
+
qdrant_client = QdrantClient(
|
| 13 |
+
url=url,
|
| 14 |
+
api_key=api_key,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Global cache to hold current query/result
|
| 18 |
+
latest_query_result = {"query": "", "result": ""}
|
| 19 |
+
|
| 20 |
+
# Feedback logger
|
| 21 |
+
def log_feedback(feedback):
|
| 22 |
+
now = datetime.now().isoformat()
|
| 23 |
+
log_entry = {
|
| 24 |
+
"timestamp": now,
|
| 25 |
+
"query": latest_query_result["query"],
|
| 26 |
+
"result": latest_query_result["result"],
|
| 27 |
+
"feedback": feedback
|
| 28 |
+
}
|
| 29 |
+
df = pd.DataFrame([log_entry])
|
| 30 |
+
df.to_csv("feedback_log.csv", mode='a', header=not pd.io.common.file_exists("feedback_log.csv"), index=False)
|
| 31 |
+
return f"✅ Feedback saved: {feedback}"
|
| 32 |
+
|
| 33 |
+
# Main search function
|
| 34 |
+
def search_product(query):
|
| 35 |
+
if (query == ""):
|
| 36 |
+
return
|
| 37 |
+
start_search = time.time()
|
| 38 |
+
start_embed = time.time()
|
| 39 |
+
query_embed = model.encode("query: " + query)
|
| 40 |
+
embed_time = time.time() - start_embed
|
| 41 |
+
|
| 42 |
+
start_query = time.time()
|
| 43 |
+
result = qdrant_client.query_points(
|
| 44 |
+
collection_name="product_E5",
|
| 45 |
+
query=query_embed.tolist(),
|
| 46 |
+
with_payload=True,
|
| 47 |
+
query_filter=Filter(
|
| 48 |
+
must=[FieldCondition(key="type", match=MatchValue(value="product"))]
|
| 49 |
+
)
|
| 50 |
+
).points
|
| 51 |
+
query_time = time.time() - start_query
|
| 52 |
+
search_time = time.time() - start_search
|
| 53 |
+
|
| 54 |
+
output = f"🕐 Embedding Time: {embed_time:.4f} sec\n"
|
| 55 |
+
output += f"🔍 Query Time: {query_time:.4f} sec\n"
|
| 56 |
+
output += f"⏱ Total Search Time: {search_time:.4f} sec\n\n"
|
| 57 |
+
output += "📦 ผลลัพธ์:\n"
|
| 58 |
+
|
| 59 |
+
result_summary = ""
|
| 60 |
+
for res in result:
|
| 61 |
+
line = f"- {res.payload.get('name', '')} (score: {res.score:.4f})"
|
| 62 |
+
output += line + "\n"
|
| 63 |
+
result_summary += line + " | "
|
| 64 |
+
|
| 65 |
+
latest_query_result["query"] = query
|
| 66 |
+
latest_query_result["result"] = result_summary.strip()
|
| 67 |
+
|
| 68 |
+
return output
|
| 69 |
+
|
| 70 |
+
# Gradio UI
|
| 71 |
+
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("## 🔎 Product Semantic Search (E5 + Qdrant)")
|
| 73 |
+
|
| 74 |
+
query_input = gr.Textbox(label="ใส่คำค้นหาสินค้า")
|
| 75 |
+
result_output = gr.Textbox(label="📋 ผลลัพธ์")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
match_btn = gr.Button("✅ ตรงกับที่ต้องการ")
|
| 79 |
+
not_match_btn = gr.Button("❌ ไม่ตรง")
|
| 80 |
+
|
| 81 |
+
feedback_status = gr.Textbox(label="📬 สถานะ Feedback")
|
| 82 |
+
|
| 83 |
+
query_input.submit(search_product, inputs=query_input, outputs=result_output)
|
| 84 |
+
match_btn.click(lambda: log_feedback("match"), outputs=feedback_status)
|
| 85 |
+
not_match_btn.click(lambda: log_feedback("not_match"), outputs=feedback_status)
|
| 86 |
+
|
| 87 |
+
# Launch
|
| 88 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
sentence-transformers
|
| 3 |
+
qdrant-client
|