Pradeepthi30 commited on
Commit
758071d
·
verified ·
1 Parent(s): 9a812d4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +43 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import numpy as np
4
+ from sentence_transformers import SentenceTransformer
5
+ from sklearn.neighbors import NearestNeighbors
6
+
7
+ model = SentenceTransformer('all-MiniLM-L6-v2')
8
+ global_docs = []
9
+ nn_model = None
10
+ doc_embeddings = None
11
+
12
+ def semantic_search(query, top_k=3):
13
+ if not nn_model or not global_docs:
14
+ return "Please upload and index a file first."
15
+ query_vec = model.encode([query])
16
+ distances, indices = nn_model.kneighbors(query_vec, n_neighbors=top_k)
17
+ results = [
18
+ f"Rank {i+1}:\nDocument: {global_docs[idx]}\nDistance: {distances[0][i]:.4f}\n"
19
+ for i, idx in enumerate(indices[0])
20
+ ]
21
+ return "\n".join(results)
22
+
23
+ def upload_and_index(file):
24
+ global global_docs, nn_model, doc_embeddings
25
+ contents = file.read().decode("utf-8").splitlines()
26
+ global_docs = [line.strip() for line in contents if line.strip()]
27
+ doc_embeddings = model.encode(global_docs)
28
+ nn_model = NearestNeighbors(n_neighbors=3, metric='euclidean')
29
+ nn_model.fit(doc_embeddings)
30
+ return "Documents indexed successfully!"
31
+
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("## 🔍 Semantic Search in Academic Papers (No FAISS)")
34
+ file_input = gr.File(label="Upload .txt file", file_types=[".txt"])
35
+ upload_button = gr.Button("Upload & Index")
36
+ upload_output = gr.Textbox(label="Status")
37
+ query_input = gr.Textbox(label="Enter your query")
38
+ search_button = gr.Button("Search")
39
+ search_output = gr.Textbox(label="Results")
40
+ upload_button.click(upload_and_index, inputs=file_input, outputs=upload_output)
41
+ search_button.click(semantic_search, inputs=query_input, outputs=search_output)
42
+
43
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ sentence-transformers
3
+ scikit-learn
4
+ numpy