Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,17 @@
|
|
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."
|
@@ -20,31 +23,47 @@ def semantic_search(query, top_k=3):
|
|
20 |
]
|
21 |
return "\n".join(results)
|
22 |
|
|
|
23 |
def upload_and_index(file):
|
24 |
global global_docs, nn_model, doc_embeddings
|
25 |
try:
|
26 |
-
content
|
27 |
-
|
28 |
-
content = content.decode("utf-8")
|
29 |
lines = content.splitlines()
|
30 |
global_docs = [line.strip() for line in lines if line.strip()]
|
|
|
|
|
31 |
doc_embeddings = model.encode(global_docs)
|
|
|
|
|
32 |
nn_model = NearestNeighbors(n_neighbors=3, metric='euclidean')
|
33 |
nn_model.fit(doc_embeddings)
|
|
|
34 |
return "Documents indexed successfully!"
|
35 |
except Exception as e:
|
36 |
return f"Error: {str(e)}"
|
37 |
|
38 |
-
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown("## 🔍 Semantic Search in Academic Papers (No FAISS)")
|
|
|
|
|
41 |
file_input = gr.File(label="Upload .txt file", file_types=[".txt"])
|
|
|
|
|
42 |
upload_button = gr.Button("Upload & Index")
|
43 |
upload_output = gr.Textbox(label="Status")
|
|
|
|
|
44 |
query_input = gr.Textbox(label="Enter your query")
|
|
|
|
|
45 |
search_button = gr.Button("Search")
|
46 |
search_output = gr.Textbox(label="Results")
|
|
|
|
|
47 |
upload_button.click(upload_and_index, inputs=file_input, outputs=upload_output)
|
48 |
search_button.click(semantic_search, inputs=query_input, outputs=search_output)
|
49 |
|
|
|
50 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
from sklearn.neighbors import NearestNeighbors
|
5 |
|
6 |
+
# Load the sentence transformer model
|
7 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
+
|
9 |
+
# Global variables for storing documents and embeddings
|
10 |
global_docs = []
|
11 |
nn_model = None
|
12 |
doc_embeddings = None
|
13 |
|
14 |
+
# Function for semantic search
|
15 |
def semantic_search(query, top_k=3):
|
16 |
if not nn_model or not global_docs:
|
17 |
return "Please upload and index a file first."
|
|
|
23 |
]
|
24 |
return "\n".join(results)
|
25 |
|
26 |
+
# Function to upload and index documents
|
27 |
def upload_and_index(file):
|
28 |
global global_docs, nn_model, doc_embeddings
|
29 |
try:
|
30 |
+
# Read the file content and decode to text
|
31 |
+
content = file.read().decode("utf-8")
|
|
|
32 |
lines = content.splitlines()
|
33 |
global_docs = [line.strip() for line in lines if line.strip()]
|
34 |
+
|
35 |
+
# Create document embeddings
|
36 |
doc_embeddings = model.encode(global_docs)
|
37 |
+
|
38 |
+
# Initialize the nearest neighbors model
|
39 |
nn_model = NearestNeighbors(n_neighbors=3, metric='euclidean')
|
40 |
nn_model.fit(doc_embeddings)
|
41 |
+
|
42 |
return "Documents indexed successfully!"
|
43 |
except Exception as e:
|
44 |
return f"Error: {str(e)}"
|
45 |
|
46 |
+
# Gradio interface
|
47 |
with gr.Blocks() as demo:
|
48 |
gr.Markdown("## 🔍 Semantic Search in Academic Papers (No FAISS)")
|
49 |
+
|
50 |
+
# File upload input
|
51 |
file_input = gr.File(label="Upload .txt file", file_types=[".txt"])
|
52 |
+
|
53 |
+
# Upload and index button
|
54 |
upload_button = gr.Button("Upload & Index")
|
55 |
upload_output = gr.Textbox(label="Status")
|
56 |
+
|
57 |
+
# Query input
|
58 |
query_input = gr.Textbox(label="Enter your query")
|
59 |
+
|
60 |
+
# Search button
|
61 |
search_button = gr.Button("Search")
|
62 |
search_output = gr.Textbox(label="Results")
|
63 |
+
|
64 |
+
# Attach actions to buttons
|
65 |
upload_button.click(upload_and_index, inputs=file_input, outputs=upload_output)
|
66 |
search_button.click(semantic_search, inputs=query_input, outputs=search_output)
|
67 |
|
68 |
+
# Launch the Gradio interface
|
69 |
demo.launch()
|