Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,25 +17,35 @@ embeddings = model.encode(data, convert_to_tensor=True)
|
|
17 |
|
18 |
# Autocomplete function
|
19 |
def autocomplete(query):
|
|
|
|
|
20 |
matches = [text for text in data if query.lower() in text.lower()]
|
21 |
return matches[:5] # Return top 5 matches
|
22 |
|
23 |
# Semantic search function
|
24 |
def semantic_search(query):
|
|
|
|
|
25 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
26 |
results = util.semantic_search(query_embedding, embeddings, top_k=5)
|
27 |
return [data[result['corpus_id']] for result in results[0]]
|
28 |
|
29 |
# Define Gradio interface
|
30 |
with gr.Blocks() as demo:
|
31 |
-
gr.Markdown("### Autocomplete & Semantic Search")
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
demo.launch()
|
|
|
17 |
|
18 |
# Autocomplete function
|
19 |
def autocomplete(query):
|
20 |
+
if not query.strip():
|
21 |
+
return [] # Return empty if query is blank
|
22 |
matches = [text for text in data if query.lower() in text.lower()]
|
23 |
return matches[:5] # Return top 5 matches
|
24 |
|
25 |
# Semantic search function
|
26 |
def semantic_search(query):
|
27 |
+
if not query.strip():
|
28 |
+
return [] # Return empty if query is blank
|
29 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
30 |
results = util.semantic_search(query_embedding, embeddings, top_k=5)
|
31 |
return [data[result['corpus_id']] for result in results[0]]
|
32 |
|
33 |
# Define Gradio interface
|
34 |
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("### Real-Time Autocomplete & Semantic Search")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
query = gr.Textbox(label="Start typing for autocomplete", live=True)
|
39 |
+
autocomplete_output = gr.Textbox(label="Autocomplete Suggestions")
|
40 |
+
|
41 |
+
with gr.Row():
|
42 |
+
semantic_query = gr.Textbox(label="Enter your query for semantic search")
|
43 |
+
semantic_search_output = gr.Textbox(label="Semantic Search Results")
|
44 |
+
|
45 |
+
# Real-time autocomplete
|
46 |
+
query.change(fn=autocomplete, inputs=query, outputs=autocomplete_output)
|
47 |
+
|
48 |
+
# Semantic search triggered on submit
|
49 |
+
semantic_query.submit(fn=semantic_search, inputs=semantic_query, outputs=semantic_search_output)
|
50 |
|
51 |
demo.launch()
|