Spaces:
Runtime error
Runtime error
File size: 1,981 Bytes
1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 a28f198 1adf9a7 |
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 |
import gradio as gr
import numpy as np
from sentence_transformers import SentenceTransformer
from sklearn.neighbors import NearestNeighbors
# Load model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Global storage
global_docs = []
nn_model = None
doc_embeddings = None
# Load documents
def load_documents(file_obj):
docs = [line.strip() for line in file_obj if line.strip()]
return docs
# Build index using Nearest Neighbors
def build_index(docs):
embeddings = model.encode(docs)
nn = NearestNeighbors(n_neighbors=3, metric='euclidean')
nn.fit(embeddings)
return nn, embeddings
# Search
def semantic_search(query, top_k=3):
if not nn_model or not global_docs:
return "Please upload and index a file first."
query_vec = model.encode([query])
distances, indices = nn_model.kneighbors(query_vec, n_neighbors=top_k)
results = [
f"Rank {i+1}:\nDocument: {global_docs[idx]}\nDistance: {distances[0][i]:.4f}\n"
for i, idx in enumerate(indices[0])
]
return "\n".join(results)
# Upload and index
def upload_and_index(file):
global global_docs, nn_model, doc_embeddings
contents = file.read().decode("utf-8").splitlines()
global_docs = [line.strip() for line in contents if line.strip()]
nn_model, doc_embeddings = build_index(global_docs)
return "Documents indexed successfully!"
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## π Semantic Search in Academic Papers (No FAISS)")
file_input = gr.File(label="Upload .txt file", file_types=[".txt"])
upload_button = gr.Button("Upload & Index")
upload_output = gr.Textbox(label="Status")
query_input = gr.Textbox(label="Enter your query")
search_button = gr.Button("Search")
search_output = gr.Textbox(label="Results")
upload_button.click(upload_and_index, inputs=file_input, outputs=upload_output)
search_button.click(semantic_search, inputs=query_input, outputs=search_output)
demo.launch()
|