Spaces:
Runtime error
Runtime error
File size: 1,600 Bytes
87188d3 78f2fc1 e25de72 b20fcd1 78f2fc1 545f3e4 b20fcd1 75cb8fa 87188d3 75cb8fa 87188d3 78f2fc1 b20fcd1 78f2fc1 b20fcd1 525165a b20fcd1 e25de72 |
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 |
from datasets import load_dataset
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import gradio as gr
import chromadb
# Load a small subset (10,000 rows)
dataset = load_dataset("wiki40b", "en", split="train[:1000]")
# Extract only text
docs = [d["text"] for d in dataset]
print("Loaded dataset with", len(docs), "documents.")
# Load embedding model
embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
# Convert texts to embeddings
embeddings = embed_model.encode(docs, show_progress_bar=True)
# Initialize ChromaDB client
chroma_client = chromadb.PersistentClient(path="./chroma_db") # Stores data persistently
collection = chroma_client.get_or_create_collection(name="wikipedia_docs")
# Store embeddings in ChromaDB
for i, (doc, embedding) in enumerate(zip(docs, embeddings)):
collection.add(
ids=[str(i)], # Unique ID for each doc
embeddings=[embedding.tolist()], # Convert numpy array to list
documents=[doc]
)
print("Stored embeddings in ChromaDB!")
# Search function using ChromaDB
def search_wikipedia(query, top_k=3):
query_embedding = embed_model.encode([query]).tolist()
results = collection.query(
query_embeddings=query_embedding,
n_results=top_k
)
return "\n\n".join(results["documents"][0]) # Return top results
# Gradio Interface
iface = gr.Interface(
fn=search_wikipedia,
inputs="text",
outputs="text",
title="Wikipedia Search RAG",
description="Enter a query and retrieve relevant Wikipedia passages."
)
iface.launch() |