Spaces:
Running
Running
File size: 8,818 Bytes
3c85915 24b5168 3c85915 24b5168 3c85915 bb771d6 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 f86ae3e 24b5168 3c85915 24b5168 3c85915 bb771d6 24b5168 3c85915 f86ae3e 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 f86ae3e 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 3c85915 24b5168 | 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | import streamlit as st
import pandas as pd
import numpy as np
import re
from sklearn.feature_extraction.text import TfidfVectorizer
from sentence_transformers import SentenceTransformer
from rank_bm25 import BM25Okapi
from rapidfuzz import fuzz
import faiss
import nltk
# ==============================
# NLTK FIX
# ==============================
nltk.download('wordnet', quiet=True)
from nltk.corpus import wordnet
# ==============================
# PAGE CONFIG
# ==============================
st.set_page_config(page_title="Multi Search Engine", layout="wide")
st.title("π Advanced Multi-Search Product Engine")
# ==============================
# LOAD MODEL
# ==============================
if "model" not in st.session_state:
with st.spinner("Loading AI model..."):
st.session_state.model = SentenceTransformer(
'all-MiniLM-L6-v2',
device='cpu'
)
model = st.session_state.model
# ==============================
# SEARCH INFO (UPDATED)
# ==============================
search_info = {
"Keyword": ("Exact match", "iphone"),
"Regex": ("Pattern match", "^Samsung"),
"Boolean": ("AND / OR logic", "nike AND shoes"),
"Fuzzy": ("Spelling mistakes", "iphon"),
"N-Gram": ("Partial word", "iph"),
"Prefix": ("Word starts with", "Sam"),
"Suffix": ("Word ends with", "phone"),
"TF-IDF": ("Keyword ranking", "wireless headphones"),
"BM25": ("Advanced ranking", "gaming laptop"),
"Semantic": ("Meaning search", "sports footwear"),
"FAISS": ("Fast semantic", "music device"),
"Hybrid": ("TF-IDF + Semantic", "running shoes"),
"Query Expansion": ("Auto synonyms", "speaker"),
"Weighted Hybrid": ("TF-IDF + Semantic + BM25", "best laptop"),
"Ensemble": ("Combine all scores", "smartphone")
}
# ==============================
# FILE LOAD (KEEP YOUR LOGIC)
# ==============================
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
if uploaded_file:
df = pd.read_csv(uploaded_file)
else:
st.info("Using sample dataset")
df = pd.DataFrame({
"product_name": [
"iPhone 14 Pro",
"Samsung Galaxy S23",
"Nike Running Shoes",
"Dell Gaming Laptop",
"Bluetooth Speaker"
],
"category": ["Mobile", "Mobile", "Footwear", "Laptop", "Electronics"],
"brand": ["Apple", "Samsung", "Nike", "Dell", "JBL"],
"description": [
"Latest smartphone",
"Android flagship phone",
"Comfort sports shoes",
"High performance laptop",
"Portable music device"
]
})
# ==============================
# DATA PREVIEW CONTROL
# ==============================
st.subheader("π Data Preview")
rows_to_show = st.selectbox("Select rows to view", [10, 20, 50, 100])
st.dataframe(df.head(rows_to_show))
# ==============================
# COMBINE TEXT
# ==============================
df["combined"] = (
df["product_name"].astype(str) + " " +
df["category"].astype(str) + " " +
df["brand"].astype(str) + " " +
df["description"].astype(str)
)
products = df["combined"].tolist()
# ==============================
# PREPROCESS
# ==============================
@st.cache(allow_output_mutation=True)
def preprocess_data(products):
tfidf = TfidfVectorizer()
tfidf_matrix = tfidf.fit_transform(products)
embeddings = model.encode(products, batch_size=64, show_progress_bar=False)
faiss.normalize_L2(embeddings)
index = faiss.IndexFlatIP(embeddings.shape[1])
index.add(np.array(embeddings))
tokenized = [p.split() for p in products]
bm25 = BM25Okapi(tokenized)
return tfidf, tfidf_matrix, embeddings, index, bm25
tfidf, tfidf_matrix, embeddings, index, bm25 = preprocess_data(products)
# ==============================
# SYNONYMS
# ==============================
def get_synonyms(word):
synonyms = set()
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
synonyms.add(lemma.name())
return synonyms
# ==============================
# SEARCH FUNCTIONS
# ==============================
def keyword_search(q):
return [(i, 1) for i, p in enumerate(products) if q.lower() in p.lower()]
def regex_search(q):
return [(i, 1) for i, p in enumerate(products) if re.search(q, p, re.IGNORECASE)]
def boolean_search(q):
if "AND" in q:
terms = q.split("AND")
return [(i, 1) for i, p in enumerate(products)
if all(t.strip().lower() in p.lower() for t in terms)]
elif "OR" in q:
terms = q.split("OR")
return [(i, 1) for i, p in enumerate(products)
if any(t.strip().lower() in p.lower() for t in terms)]
return []
def fuzzy_search(q):
scores = [(i, fuzz.ratio(q, p)) for i, p in enumerate(products)]
return sorted(scores, key=lambda x: x[1], reverse=True)
def ngram_search(q):
return [(i, 1) for i, p in enumerate(products) if q.lower() in p.lower()]
# β
FIXED PREFIX (word-level)
def prefix_search(q):
return [(i, 1) for i, p in enumerate(products)
if any(word.startswith(q.lower()) for word in p.lower().split())]
# β
FIXED SUFFIX (word-level)
def suffix_search(q):
return [(i, 1) for i, p in enumerate(products)
if any(word.endswith(q.lower()) for word in p.lower().split())]
def tfidf_search(q):
q_vec = tfidf.transform([q])
scores = (tfidf_matrix @ q_vec.T).toarray().flatten()
return list(enumerate(scores))
def bm25_search(q):
scores = bm25.get_scores(q.split())
return list(enumerate(scores))
def semantic_search(q):
q_emb = model.encode([q], show_progress_bar=False)
faiss.normalize_L2(q_emb)
scores = np.dot(embeddings, q_emb.T).flatten()
return list(enumerate(scores))
def faiss_search(q):
q_emb = model.encode([q], show_progress_bar=False)
faiss.normalize_L2(q_emb)
D, I = index.search(np.array(q_emb), 10)
return [(i, float(D[0][idx])) for idx, i in enumerate(I[0])]
def hybrid_search(q):
tfidf_res = dict(tfidf_search(q))
sem_res = dict(semantic_search(q))
return [(i, tfidf_res.get(i, 0) + sem_res.get(i, 0)) for i in range(len(products))]
# β
IMPROVED QUERY EXPANSION
def query_expansion_search(q):
expanded = q.split()
for word in q.split():
expanded += list(get_synonyms(word))
return tfidf_search(" ".join(expanded))
# β
IMPROVED WEIGHTED HYBRID
def weighted_hybrid(q):
tfidf_res = dict(tfidf_search(q))
sem_res = dict(semantic_search(q))
bm25_res = dict(bm25_search(q))
return [(i,
0.4 * tfidf_res.get(i, 0) +
0.4 * sem_res.get(i, 0) +
0.2 * bm25_res.get(i, 0))
for i in range(len(products))]
# β
FIXED ENSEMBLE (NORMALIZED)
def ensemble_search(q):
tfidf_res = np.array([s for _, s in tfidf_search(q)])
sem_res = np.array([s for _, s in semantic_search(q)])
bm25_res = np.array([s for _, s in bm25_search(q)])
combined = tfidf_res/np.max(tfidf_res+1e-6) + \
sem_res/np.max(sem_res+1e-6) + \
bm25_res/np.max(bm25_res+1e-6)
return list(enumerate(combined))
# ==============================
# UI
# ==============================
search_type = st.selectbox("π Select Search Type", list(search_info.keys()))
explanation, example = search_info[search_type]
st.markdown(f"""
### π {search_type}
- **Explanation:** {explanation}
- **Example:** `{example}`
""")
query = st.text_input("Enter your search query")
if st.button("Try Example"):
query = example
st.success(f"Loaded: {query}")
top_k = st.slider("Top Results", 5, 20, 10)
# ==============================
# SEARCH EXECUTION
# ==============================
if st.button("Search"):
if not query:
st.warning("Enter query")
else:
func_map = {
"Keyword": keyword_search,
"Regex": regex_search,
"Boolean": boolean_search,
"Fuzzy": fuzzy_search,
"N-Gram": ngram_search,
"Prefix": prefix_search,
"Suffix": suffix_search,
"TF-IDF": tfidf_search,
"BM25": bm25_search,
"Semantic": semantic_search,
"FAISS": faiss_search,
"Hybrid": hybrid_search,
"Query Expansion": query_expansion_search,
"Weighted Hybrid": weighted_hybrid,
"Ensemble": ensemble_search
}
results = func_map[search_type](query)
# Sort results
results = sorted(results, key=lambda x: x[1], reverse=True)[:top_k]
indices = [i for i, _ in results]
result_df = df.iloc[indices].copy()
result_df["Score"] = [round(score, 4) for _, score in results]
st.subheader("π Results")
st.dataframe(result_df) |