Spaces:
Sleeping
Sleeping
File size: 12,694 Bytes
d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 9aa44ca d868fc5 |
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
import gradio as gr
import pandas as pd
import numpy as np
from datasets import load_dataset
from sentence_transformers import SentenceTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from rank_bm25 import BM25Okapi
import re
from typing import List, Tuple, Dict
import threading
import time
# Configuration
DATASET_NAME = "hoololi/AI_Act_with_embeddings"
EMBEDDING_MODEL_NAME = "all-MiniLM-L6-v2"
TOP_K = 5
class AIActSearchEngine:
def __init__(self):
self.dataset = None
self.embedding_model = None
self.tfidf_vectorizer = None
self.tfidf_matrix = None
self.bm25_model = None
self.processed_docs = None
self.load_data()
self.setup_models()
def load_data(self):
"""Load dataset from Hugging Face"""
print("Loading dataset...")
dataset = load_dataset(DATASET_NAME, split="train")
self.dataset = dataset.to_pandas()
print(f"Dataset loaded: {len(self.dataset)} articles")
def setup_models(self):
"""Initialize models and vectorizers"""
print("Initializing models...")
# Embedding model
self.embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
# TF-IDF
self.tfidf_vectorizer = TfidfVectorizer(
max_features=10000,
stop_words='english',
lowercase=True,
ngram_range=(1, 2)
)
self.tfidf_matrix = self.tfidf_vectorizer.fit_transform(self.dataset['processed_content'])
# BM25 (using optimized library)
self.setup_bm25()
print("Models initialized successfully!")
def setup_bm25(self):
"""Setup BM25 using optimized library"""
print("Setting up BM25...")
# Tokenize documents for BM25
self.processed_docs = [doc.split() for doc in self.dataset['processed_content']]
# Create BM25 model (much faster than manual implementation)
self.bm25_model = BM25Okapi(self.processed_docs)
print("BM25 setup complete!")
def search_tfidf(self, query: str) -> List[Tuple[str, str, float]]:
"""TF-IDF search"""
query_vector = self.tfidf_vectorizer.transform([query])
similarities = cosine_similarity(query_vector, self.tfidf_matrix).flatten()
# Create a list of (score, index) for stable sorting
scored_docs = [(similarities[i], i) for i in range(len(similarities)) if similarities[i] > 0]
# Sort by descending score, then by ascending index for tie-breaking
scored_docs.sort(key=lambda x: (-x[0], x[1]))
# Take top K and deduplicate
seen_articles = set()
results = []
for score, idx in scored_docs:
article_num = self.dataset.iloc[idx]['article_number']
if article_num not in seen_articles and len(results) < TOP_K:
seen_articles.add(article_num)
results.append((
article_num,
self.dataset.iloc[idx]['article_content'],
score
))
return results
def search_bm25(self, query: str) -> List[Tuple[str, str, float]]:
"""BM25 search using optimized library"""
# Tokenize query
query_tokens = query.lower().split()
# Get BM25 scores (much faster!)
scores = self.bm25_model.get_scores(query_tokens)
# Create scored documents list
scored_docs = [(scores[i], i) for i in range(len(scores)) if scores[i] > 0]
# Sort by descending score, then by ascending index for tie-breaking
scored_docs.sort(key=lambda x: (-x[0], x[1]))
# Take top K and deduplicate
seen_articles = set()
results = []
for score, idx in scored_docs:
article_num = self.dataset.iloc[idx]['article_number']
if article_num not in seen_articles and len(results) < TOP_K:
seen_articles.add(article_num)
results.append((
article_num,
self.dataset.iloc[idx]['article_content'],
score
))
return results
def search_embeddings(self, query: str) -> List[Tuple[str, str, float]]:
"""Embedding similarity search"""
# Encode the query
query_embedding = self.embedding_model.encode([query])
# Get stored embeddings
stored_embeddings = np.array(self.dataset['embedding'].tolist())
# Calculate cosine similarity
similarities = cosine_similarity(query_embedding, stored_embeddings).flatten()
# Create a list of (score, index) for stable sorting
scored_docs = [(similarities[i], i) for i in range(len(similarities))]
# Sort by descending score, then by ascending index for tie-breaking
scored_docs.sort(key=lambda x: (-x[0], x[1]))
# Take top K and deduplicate
seen_articles = set()
results = []
for score, idx in scored_docs:
article_num = self.dataset.iloc[idx]['article_number']
if article_num not in seen_articles and len(results) < TOP_K:
seen_articles.add(article_num)
results.append((
article_num,
self.dataset.iloc[idx]['article_content'],
score
))
return results
def search_all(self, query: str) -> Dict[str, List[Tuple[str, str, float]]]:
"""Perform all searches"""
if not query.strip():
return {
'tfidf': [],
'bm25': [],
'embeddings': []
}
return {
'tfidf': self.search_tfidf(query),
'bm25': self.search_bm25(query),
'embeddings': self.search_embeddings(query)
}
def format_results_table(results: List[Tuple[str, str, float]],
search_type: str,
highlight_articles: set) -> str:
"""Format results as HTML table"""
if not results:
return f"""
<div style="text-align: center; padding: 20px;">
<h3>{search_type}</h3>
<p>No results found</p>
</div>
"""
html = f"""
<div style="margin: 10px;">
<h3 style="text-align: center; margin-bottom: 15px;">{search_type}</h3>
<table style="width: 100%; border-collapse: collapse; font-size: 12px;">
<thead>
<tr style="background-color: #f0f0f0;">
<th style="border: 1px solid #ddd; padding: 8px; width: 80px;">Score</th>
<th style="border: 1px solid #ddd; padding: 8px; width: 80px;">Article</th>
<th style="border: 1px solid #ddd; padding: 8px;">Content</th>
</tr>
</thead>
<tbody>
"""
for article_num, content, score in results:
# Highlight if article is in all 3 searches
bg_color = "background-color: #90EE90;" if article_num in highlight_articles else ""
# Limit content for display
truncated_content = content[:300] + "..." if len(content) > 300 else content
html += f"""
<tr>
<td style="border: 1px solid #ddd; padding: 8px; text-align: center;">{score:.3f}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: center; {bg_color}">{article_num}</td>
<td style="border: 1px solid #ddd; padding: 8px; max-width: 400px; overflow: hidden;">
<details>
<summary style="cursor: pointer; font-weight: bold;">View content</summary>
<div style="margin-top: 10px; white-space: pre-wrap;">{content}</div>
</details>
</td>
</tr>
"""
html += """
</tbody>
</table>
</div>
"""
return html
def search_articles_progressive(query: str, search_engine: AIActSearchEngine):
"""Progressive search function with sequential updates"""
if not query.strip():
empty_table = """
<div style="text-align: center; padding: 20px;">
<p>Enter a query to start searching</p>
</div>
"""
return empty_table, empty_table, empty_table
# Initialize loading states
loading_table = """
<div style="text-align: center; padding: 20px;">
<div style="display: inline-block; width: 20px; height: 20px; border: 3px solid #f3f3f3; border-top: 3px solid #3498db; border-radius: 50%; animation: spin 1s linear infinite;"></div>
<p style="margin-top: 10px;">Searching...</p>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</div>
"""
# Results storage
results = {'tfidf': [], 'bm25': [], 'embeddings': []}
# 1. TF-IDF Search (fastest)
print("Starting TF-IDF search...")
start_time = time.time()
results['tfidf'] = search_engine.search_tfidf(query)
print(f"TF-IDF completed in {time.time() - start_time:.2f}s")
# 2. BM25 Search (fast with optimized library)
print("Starting BM25 search...")
start_time = time.time()
results['bm25'] = search_engine.search_bm25(query)
print(f"BM25 completed in {time.time() - start_time:.2f}s")
# 3. Embeddings Search (slowest)
print("Starting embeddings search...")
start_time = time.time()
results['embeddings'] = search_engine.search_embeddings(query)
print(f"Embeddings completed in {time.time() - start_time:.2f}s")
# Identify articles present in all 3 searches
tfidf_articles = {r[0] for r in results['tfidf']}
bm25_articles = {r[0] for r in results['bm25']}
embedding_articles = {r[0] for r in results['embeddings']}
# Articles present in all 3 methods
highlight_articles = tfidf_articles & bm25_articles & embedding_articles
# Format final results
tfidf_html = format_results_table(results['tfidf'], "TF-IDF", highlight_articles)
bm25_html = format_results_table(results['bm25'], "BM25", highlight_articles)
embeddings_html = format_results_table(results['embeddings'], "Embeddings", highlight_articles)
return tfidf_html, bm25_html, embeddings_html
def search_articles(query: str, search_engine: AIActSearchEngine):
"""Main search function (kept for compatibility)"""
return search_articles_progressive(query, search_engine)
def main():
"""Main function to launch the application"""
print("Initializing application...")
# Initialize search engine
search_engine = AIActSearchEngine()
# Create Gradio interface
with gr.Blocks(title="AI Act Search Tool", theme=gr.themes.Default()) as app:
gr.Markdown("# ๐ AI Act Textual Search Tool")
gr.Markdown("Compare results from different textual search methods on AI Act articles")
with gr.Row():
with gr.Column(scale=3):
query_input = gr.Textbox(
label="Search Query",
placeholder="Enter your keywords...",
lines=2
)
with gr.Column(scale=1):
search_button = gr.Button("๐ Search", variant="primary")
gr.Markdown("---")
gr.Markdown("### Search Results")
gr.Markdown("Articles found by all 3 methods are highlighted in **light green**")
with gr.Row():
tfidf_output = gr.HTML(label="TF-IDF")
bm25_output = gr.HTML(label="BM25")
embeddings_output = gr.HTML(label="Embeddings")
# Define actions
search_button.click(
fn=lambda q: search_articles(q, search_engine),
inputs=query_input,
outputs=[tfidf_output, bm25_output, embeddings_output]
)
# Allow search with Enter
query_input.submit(
fn=lambda q: search_articles(q, search_engine),
inputs=query_input,
outputs=[tfidf_output, bm25_output, embeddings_output]
)
# Launch application
print("Launching Gradio application...")
app.launch(debug=True, share=False)
if __name__ == "__main__":
main() |