Update app.py
Browse files
app.py
CHANGED
|
@@ -1,264 +1,72 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colab.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1nLqIbyBDiBI96gDZ0TziLNX8I4uWnl9G
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
"""Picking subreddits, split=sub as the data on huggingface datasets is split w.r.t subreddits and not train/test/validation.
|
| 11 |
-
|
| 12 |
-
Streaming = True, because we don't want to load all the data into local memory
|
| 13 |
-
|
| 14 |
-
loading and combining all the iterables together.
|
| 15 |
-
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
from huggingface_hub import hf_hub_download
|
| 21 |
import json
|
| 22 |
from itertools import chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
target_subreddits = ["askscience", "gaming", "technology", "todayilearned", "programming"]
|
|
|
|
| 25 |
|
|
|
|
| 26 |
def load_reddit_split(subreddit_name):
|
| 27 |
-
""
|
| 28 |
-
|
| 29 |
-
file_path = hf_hub_download(repo_id="HuggingFaceGECLM/REDDIT_comments", filename=f"{subreddit_name}.jsonl")
|
| 30 |
-
with open(file_path, "r") as f:
|
| 31 |
for line in f:
|
| 32 |
yield json.loads(line)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
combined_dataset = chain(*(load_reddit_split(sub) for sub in target_subreddits))
|
| 36 |
-
|
| 37 |
-
"""# Chunking Logic
|
| 38 |
-
- Group Reddit comments into small textual chunks to create a unit of meaning for embedding.
|
| 39 |
-
|
| 40 |
-
- Short Reddit comments are noisy and lack semantic depth. Chunking lets us:
|
| 41 |
-
|
| 42 |
-
- Aggregate context across comments
|
| 43 |
-
|
| 44 |
-
- Improve embedding quality for semantic search
|
| 45 |
-
|
| 46 |
-
- Normalize input length for vector similarity
|
| 47 |
-
|
| 48 |
-
- We'll group n comments (3-5) per chunk or limit chunk size by token count (100 words).
|
| 49 |
-
|
| 50 |
-
**Use PySpark for handling the large concatenantion of chunked data**
|
| 51 |
-
"""
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
from pyspark.sql.types import StringType
|
| 56 |
-
import re
|
| 57 |
-
from itertools import islice
|
| 58 |
-
|
| 59 |
-
spark = SparkSession.builder.getOrCreate()
|
| 60 |
-
|
| 61 |
-
# Load generator into pandas or write out sample file and read into Spark
|
| 62 |
-
df = spark.createDataFrame([{"body": ex["body"]} for ex in islice(combined_dataset, 100000)])
|
| 63 |
-
|
| 64 |
-
# Clean text UDF
|
| 65 |
-
def clean_body(text):
|
| 66 |
text = text.lower()
|
| 67 |
text = re.sub(r"http\S+|www\S+|https\S+", "", text)
|
| 68 |
text = re.sub(r"[^a-zA-Z\s]", "", text)
|
| 69 |
return re.sub(r"\s+", " ", text).strip()
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# Add row numbers to chunk
|
| 75 |
-
df_indexed = df_clean.withColumn("row_num", monotonically_increasing_id())
|
| 76 |
-
chunk_size = 5
|
| 77 |
-
df_indexed = df_indexed.withColumn("chunk_id", (col("row_num") / chunk_size).cast("int"))
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
df_chunked = df_indexed.groupBy("chunk_id").agg(concat_ws(" ", collect_list("clean")).alias("chunk_text"))
|
| 82 |
-
|
| 83 |
-
chunked_comments = df_chunked.select("chunk_text").rdd.map(lambda x: x[0]).collect()
|
| 84 |
-
|
| 85 |
-
subreddit_labels = []
|
| 86 |
-
for example in combined_dataset:
|
| 87 |
-
subreddit_labels.append(example["subreddit_name_prefixed"])
|
| 88 |
-
if len(subreddit_labels) >= len(chunked_comments):
|
| 89 |
-
break
|
| 90 |
-
|
| 91 |
-
"""Cleaner text = better embeddings. Noise like markdown or links pollute meaning.
|
| 92 |
-
|
| 93 |
-
We'll use regex and basic string methods.
|
| 94 |
-
|
| 95 |
-
Normalize the text: remove URLs, HTML tags, Reddit-specific formatting, etc.
|
| 96 |
-
"""
|
| 97 |
-
|
| 98 |
-
from gensim.models import Word2Vec
|
| 99 |
-
from tqdm import tqdm
|
| 100 |
-
import re
|
| 101 |
-
|
| 102 |
-
def clean_text(text):
|
| 103 |
-
# Lowercase, remove URLs, special chars
|
| 104 |
-
text = text.lower()
|
| 105 |
-
text = re.sub(r"http\S+|www\S+|https\S+", "", text, flags=re.MULTILINE)
|
| 106 |
-
text = re.sub(r"[^a-zA-Z\s]", "", text)
|
| 107 |
-
text = re.sub(r"\s+", " ", text).strip()
|
| 108 |
-
return text
|
| 109 |
-
|
| 110 |
-
tokenized_chunks = []
|
| 111 |
-
for chunk in tqdm(chunked_comments):
|
| 112 |
-
cleaned = clean_text(chunk)
|
| 113 |
-
tokens = cleaned.split() # Simple whitespace tokenizer
|
| 114 |
-
tokenized_chunks.append(tokens)
|
| 115 |
-
|
| 116 |
-
"""Chunking + Tokenizing, removing urls, reddit slang words and unnecessary noisy text information.
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
vector_size=100, # Size of word embeddings (dimensionality)
|
| 120 |
-
|
| 121 |
-
window=5, # Context window size (how many words to look left/right)
|
| 122 |
-
|
| 123 |
-
min_count=2, # Ignores words with frequency < 2 (reduces noise)
|
| 124 |
-
|
| 125 |
-
workers=4, # Parallel training threads (CPU cores)
|
| 126 |
-
|
| 127 |
-
sg=1 # 1 = Skip-Gram (better for rare words); 0 =CBOW
|
| 128 |
-
"""
|
| 129 |
|
|
|
|
| 130 |
model = Word2Vec(sentences=tokenized_chunks, vector_size=100, window=5, min_count=2, workers=4, sg=1)
|
| 131 |
model.save("reddit_word2vec.model")
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
Word2Vec learns dense vector representations (embeddings) for words by capturing their semantic context in a corpus. It enables semantic similarity, clustering, and search.
|
| 136 |
-
|
| 137 |
-
Skip-gram learns to predict surrounding words for a given center word. It performs better on small to medium-sized datasets and captures rare word semantics effectively.
|
| 138 |
-
|
| 139 |
-
- Word2Vec only generates vectors for individual words, not entire sentences or documents.
|
| 140 |
-
|
| 141 |
-
- Each word gets mapped to a dense vector (e.g., 100-dim) that captures its semantic relationships with other words.
|
| 142 |
-
|
| 143 |
-
# Why Averaging?
|
| 144 |
-
- It's a simple and surprisingly strong baseline:
|
| 145 |
-
|
| 146 |
-
- -Works well in low-resource or custom-trained embedding settings
|
| 147 |
-
|
| 148 |
-
- Keeps computation cheap
|
| 149 |
-
|
| 150 |
-
- Captures the "semantic center" of the chunk
|
| 151 |
-
|
| 152 |
-
Alternative strategies:
|
| 153 |
-
|
| 154 |
-
- Weighted average (e.g., using TF-IDF or word frequency)
|
| 155 |
-
|
| 156 |
-
- Doc2Vec (learns doc embeddings directly)
|
| 157 |
-
|
| 158 |
-
- Transformers (e.g., BERT) for sentence embeddings (but heavier)
|
| 159 |
-
"""
|
| 160 |
-
|
| 161 |
-
import numpy as np
|
| 162 |
-
|
| 163 |
-
def get_chunk_embedding(chunk_tokens, model):
|
| 164 |
-
vectors = []
|
| 165 |
-
for token in chunk_tokens:
|
| 166 |
-
if token in model.wv:
|
| 167 |
-
vectors.append(model.wv[token])
|
| 168 |
-
if not vectors:
|
| 169 |
-
return np.zeros(model.vector_size)
|
| 170 |
-
return np.mean(vectors, axis=0)
|
| 171 |
-
|
| 172 |
-
# Dense embedding for each chunk
|
| 173 |
-
chunk_embeddings = [get_chunk_embedding(tokens, model) for tokens in tokenized_chunks]
|
| 174 |
-
|
| 175 |
-
"""Converting variable length chunks to fixed level embeddings"""
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
import faiss
|
| 179 |
-
|
| 180 |
-
# Convert embeddings to float32 numpy array
|
| 181 |
-
embedding_matrix = np.array(chunk_embeddings).astype("float32")
|
| 182 |
-
|
| 183 |
-
# Initialize FAISS index (L2 similarity)
|
| 184 |
-
index = faiss.IndexFlatL2(model.vector_size)
|
| 185 |
-
index.add(embedding_matrix)
|
| 186 |
-
|
| 187 |
-
"""Building FAISS index with the dense vectors generated from avaraging earlier.
|
| 188 |
-
|
| 189 |
-
FAISS is optimized for fast, approximate nearest-neighbor search — standard for semantic search pipelines.
|
| 190 |
-
|
| 191 |
-
Indexing takes precomputed embeddings (vectors generated from text) and organizes them into a searchable format like FAISS, enabling fast similarity-based retrieval.
|
| 192 |
-
"""
|
| 193 |
-
|
| 194 |
-
import faiss
|
| 195 |
-
import numpy as np
|
| 196 |
-
|
| 197 |
-
# Embed each chunk using average Word2Vec token embeddings
|
| 198 |
-
def embed_chunk(text, model):
|
| 199 |
-
tokens = text.lower().split()
|
| 200 |
vectors = [model.wv[token] for token in tokens if token in model.wv]
|
| 201 |
return np.mean(vectors, axis=0) if vectors else np.zeros(model.vector_size)
|
| 202 |
|
| 203 |
-
embeddings = np.array([
|
| 204 |
-
|
| 205 |
-
# Build and save FAISS index
|
| 206 |
index = faiss.IndexFlatL2(model.vector_size)
|
| 207 |
index.add(embeddings)
|
| 208 |
faiss.write_index(index, "reddit_faiss.index")
|
| 209 |
|
| 210 |
-
|
| 211 |
-
tokens = clean_text(query).split()
|
| 212 |
-
query_vec = get_chunk_embedding(tokens, model).astype("float32").reshape(1, -1)
|
| 213 |
-
|
| 214 |
-
distances, indices = index.search(query_vec, top_k)
|
| 215 |
-
return indices[0], distances[0]
|
| 216 |
-
|
| 217 |
-
original_chunks = [" ".join(tokens) for tokens in tokenized_chunks]
|
| 218 |
-
|
| 219 |
-
query = "quantum physics experiments"
|
| 220 |
-
top_ids, top_distances = search(query, model, index)
|
| 221 |
-
|
| 222 |
-
for i, idx in enumerate(top_ids):
|
| 223 |
-
print(f"Rank {i+1} | Distance: {top_distances[i]:.2f}")
|
| 224 |
-
print(original_chunks[idx][:300], "...\n")
|
| 225 |
-
|
| 226 |
-
"""# **Reddit Semantic Search App**"""
|
| 227 |
-
|
| 228 |
-
import gradio as gr
|
| 229 |
-
import numpy as np
|
| 230 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 231 |
-
from PIL import Image
|
| 232 |
-
|
| 233 |
-
from gensim.models import Word2Vec
|
| 234 |
-
import faiss
|
| 235 |
-
import numpy as np
|
| 236 |
-
import gradio as gr
|
| 237 |
-
|
| 238 |
-
# Load Word2Vec model and FAISS index
|
| 239 |
model = Word2Vec.load("reddit_word2vec.model")
|
| 240 |
index = faiss.read_index("reddit_faiss.index")
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
# Prepare embedding function
|
| 243 |
def embed_text(text):
|
| 244 |
-
tokens = text
|
| 245 |
-
|
| 246 |
-
if not vectors:
|
| 247 |
-
return np.zeros(model.vector_size)
|
| 248 |
-
return np.mean(vectors, axis=0)
|
| 249 |
-
|
| 250 |
-
# Build subreddit index
|
| 251 |
-
subreddit_map = {i: label for i, label in enumerate(subreddit_labels)}
|
| 252 |
-
unique_subreddits = sorted(set(subreddit_labels)) # for dropdown
|
| 253 |
|
| 254 |
-
# Semantic search function
|
| 255 |
def search_reddit(query, selected_subreddit, top_k=5):
|
| 256 |
-
query_vec = embed_text(query).
|
| 257 |
-
D, I = index.search(
|
| 258 |
|
| 259 |
results = []
|
| 260 |
for idx in I[0]:
|
| 261 |
-
if idx < len(chunked_comments) and subreddit_map
|
| 262 |
results.append(f"🔸 {chunked_comments[idx]}")
|
| 263 |
if len(results) >= top_k:
|
| 264 |
break
|
|
@@ -267,21 +75,20 @@ def search_reddit(query, selected_subreddit, top_k=5):
|
|
| 267 |
return "⚠️ No relevant results found."
|
| 268 |
return "\n\n".join(results)
|
| 269 |
|
| 270 |
-
#
|
| 271 |
with gr.Blocks(theme=gr.themes.Base(primary_hue="orange", secondary_hue="gray")) as demo:
|
| 272 |
gr.Image(
|
| 273 |
value="https://1000logos.net/wp-content/uploads/2017/05/Reddit-Logo.png",
|
| 274 |
show_label=False,
|
| 275 |
height=100
|
| 276 |
)
|
| 277 |
-
gr.Markdown("## Reddit Semantic Search (Powered by Word2Vec + FAISS)\n_Disclaimer:
|
| 278 |
-
|
| 279 |
with gr.Row():
|
| 280 |
query = gr.Textbox(label="Enter your Reddit-like query", placeholder="e.g. What's new in AI?")
|
| 281 |
-
|
| 282 |
output = gr.Textbox(label="Top Matching Chunks", lines=10)
|
| 283 |
search_btn = gr.Button("🔍 Search")
|
| 284 |
|
| 285 |
search_btn.click(fn=search_reddit, inputs=[query, subreddit_dropdown], outputs=output)
|
| 286 |
|
| 287 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
+
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
from itertools import chain
|
| 5 |
+
import numpy as np
|
| 6 |
+
from gensim.models import Word2Vec
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
import faiss
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from huggingface_hub import hf_hub_download
|
| 11 |
|
| 12 |
+
# --- CONFIGURATION ---
|
| 13 |
target_subreddits = ["askscience", "gaming", "technology", "todayilearned", "programming"]
|
| 14 |
+
chunk_size = 5
|
| 15 |
|
| 16 |
+
# --- LOAD REDDIT COMMENTS ---
|
| 17 |
def load_reddit_split(subreddit_name):
|
| 18 |
+
path = hf_hub_download(repo_id="HuggingFaceGECLM/REDDIT_comments", filename=f"{subreddit_name}.jsonl")
|
| 19 |
+
with open(path, "r") as f:
|
|
|
|
|
|
|
| 20 |
for line in f:
|
| 21 |
yield json.loads(line)
|
| 22 |
|
| 23 |
+
combined_dataset = list(chain(*(load_reddit_split(sub) for sub in target_subreddits)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# --- CLEAN + CHUNK ---
|
| 26 |
+
def clean_text(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
text = text.lower()
|
| 28 |
text = re.sub(r"http\S+|www\S+|https\S+", "", text)
|
| 29 |
text = re.sub(r"[^a-zA-Z\s]", "", text)
|
| 30 |
return re.sub(r"\s+", " ", text).strip()
|
| 31 |
|
| 32 |
+
cleaned_comments = [clean_text(comment["body"]) for comment in combined_dataset if "body" in comment]
|
| 33 |
+
chunked_comments = [" ".join(cleaned_comments[i:i+chunk_size]) for i in range(0, len(cleaned_comments), chunk_size)]
|
| 34 |
+
subreddit_labels = [ex["subreddit_name_prefixed"] for ex in combined_dataset[:len(chunked_comments)]]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# --- TOKENIZE ---
|
| 37 |
+
tokenized_chunks = [chunk.split() for chunk in chunked_comments]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# --- TRAIN WORD2VEC ---
|
| 40 |
model = Word2Vec(sentences=tokenized_chunks, vector_size=100, window=5, min_count=2, workers=4, sg=1)
|
| 41 |
model.save("reddit_word2vec.model")
|
| 42 |
|
| 43 |
+
# --- EMBEDDINGS + FAISS ---
|
| 44 |
+
def embed_tokens(tokens, model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
vectors = [model.wv[token] for token in tokens if token in model.wv]
|
| 46 |
return np.mean(vectors, axis=0) if vectors else np.zeros(model.vector_size)
|
| 47 |
|
| 48 |
+
embeddings = np.array([embed_tokens(chunk, model) for chunk in tokenized_chunks]).astype("float32")
|
|
|
|
|
|
|
| 49 |
index = faiss.IndexFlatL2(model.vector_size)
|
| 50 |
index.add(embeddings)
|
| 51 |
faiss.write_index(index, "reddit_faiss.index")
|
| 52 |
|
| 53 |
+
# --- SEARCH LOGIC ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
model = Word2Vec.load("reddit_word2vec.model")
|
| 55 |
index = faiss.read_index("reddit_faiss.index")
|
| 56 |
+
subreddit_map = {i: label for i, label in enumerate(subreddit_labels)}
|
| 57 |
+
unique_subreddits = sorted(set(subreddit_labels))
|
| 58 |
|
|
|
|
| 59 |
def embed_text(text):
|
| 60 |
+
tokens = clean_text(text).split()
|
| 61 |
+
return embed_tokens(tokens, model).astype("float32")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
def search_reddit(query, selected_subreddit, top_k=5):
|
| 64 |
+
query_vec = embed_text(query).reshape(1, -1)
|
| 65 |
+
D, I = index.search(query_vec, top_k)
|
| 66 |
|
| 67 |
results = []
|
| 68 |
for idx in I[0]:
|
| 69 |
+
if idx < len(chunked_comments) and subreddit_map.get(idx) == selected_subreddit:
|
| 70 |
results.append(f"🔸 {chunked_comments[idx]}")
|
| 71 |
if len(results) >= top_k:
|
| 72 |
break
|
|
|
|
| 75 |
return "⚠️ No relevant results found."
|
| 76 |
return "\n\n".join(results)
|
| 77 |
|
| 78 |
+
# --- GRADIO APP ---
|
| 79 |
with gr.Blocks(theme=gr.themes.Base(primary_hue="orange", secondary_hue="gray")) as demo:
|
| 80 |
gr.Image(
|
| 81 |
value="https://1000logos.net/wp-content/uploads/2017/05/Reddit-Logo.png",
|
| 82 |
show_label=False,
|
| 83 |
height=100
|
| 84 |
)
|
| 85 |
+
gr.Markdown("## 👾 Reddit Semantic Search (Powered by Word2Vec + FAISS)\n_Disclaimer: Experimental prototype, not owned/developed by Reddit Inc_")
|
|
|
|
| 86 |
with gr.Row():
|
| 87 |
query = gr.Textbox(label="Enter your Reddit-like query", placeholder="e.g. What's new in AI?")
|
| 88 |
+
subreddit_dropdown = gr.Dropdown(choices=unique_subreddits, label="Filter by Subreddit")
|
| 89 |
output = gr.Textbox(label="Top Matching Chunks", lines=10)
|
| 90 |
search_btn = gr.Button("🔍 Search")
|
| 91 |
|
| 92 |
search_btn.click(fn=search_reddit, inputs=[query, subreddit_dropdown], outputs=output)
|
| 93 |
|
| 94 |
+
demo.launch(share=True)
|