Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
Semantic Quote Search Engine
|
| 3 |
-
Deploy this to Hugging Face Spaces!
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
@@ -10,13 +9,13 @@ from datasets import load_dataset
|
|
| 10 |
import pandas as pd
|
| 11 |
import os
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
print("
|
| 15 |
|
| 16 |
# Load embedding model
|
| 17 |
-
print("
|
| 18 |
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 19 |
-
print("
|
| 20 |
|
| 21 |
# Initialize ChromaDB
|
| 22 |
chroma_path = "./chroma_db"
|
|
@@ -26,9 +25,9 @@ client = chromadb.PersistentClient(path=chroma_path)
|
|
| 26 |
# Check if collection exists, otherwise create it
|
| 27 |
try:
|
| 28 |
collection = client.get_collection("quotes_collection")
|
| 29 |
-
print(f"
|
| 30 |
except:
|
| 31 |
-
print("
|
| 32 |
|
| 33 |
# Load dataset
|
| 34 |
dataset = load_dataset("Abirate/english_quotes", split="train")
|
|
@@ -74,10 +73,10 @@ except:
|
|
| 74 |
metadatas=metadata[i:end_idx]
|
| 75 |
)
|
| 76 |
|
| 77 |
-
print(f"
|
| 78 |
|
| 79 |
|
| 80 |
-
#
|
| 81 |
def semantic_search(query, n_results=5):
|
| 82 |
"""
|
| 83 |
Perform semantic search over the quotes collection.
|
|
@@ -116,11 +115,11 @@ def semantic_search(query, n_results=5):
|
|
| 116 |
def search_quotes(query, num_results):
|
| 117 |
"""Wrapper for Gradio interface"""
|
| 118 |
if not query.strip():
|
| 119 |
-
return "
|
| 120 |
return semantic_search(query, n_results=int(num_results))
|
| 121 |
|
| 122 |
|
| 123 |
-
#
|
| 124 |
demo = gr.Interface(
|
| 125 |
fn=search_quotes,
|
| 126 |
inputs=[
|
|
@@ -134,11 +133,11 @@ demo = gr.Interface(
|
|
| 134 |
maximum=10,
|
| 135 |
value=5,
|
| 136 |
step=1,
|
| 137 |
-
label="
|
| 138 |
)
|
| 139 |
],
|
| 140 |
-
outputs=gr.Markdown(label="
|
| 141 |
-
title="
|
| 142 |
description="""
|
| 143 |
## Search through famous quotes using AI-powered semantic similarity!
|
| 144 |
|
|
|
|
| 1 |
"""
|
| 2 |
+
Semantic Quote Search Engine, 510, Jaideep, week 13
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 9 |
import pandas as pd
|
| 10 |
import os
|
| 11 |
|
| 12 |
+
# INITIALIZATIO
|
| 13 |
+
print(" Starting Semantic Search Engine...")
|
| 14 |
|
| 15 |
# Load embedding model
|
| 16 |
+
print(" Loading embedding model...")
|
| 17 |
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 18 |
+
print("Model loaded!")
|
| 19 |
|
| 20 |
# Initialize ChromaDB
|
| 21 |
chroma_path = "./chroma_db"
|
|
|
|
| 25 |
# Check if collection exists, otherwise create it
|
| 26 |
try:
|
| 27 |
collection = client.get_collection("quotes_collection")
|
| 28 |
+
print(f"Loaded existing collection with {collection.count()} documents")
|
| 29 |
except:
|
| 30 |
+
print("Creating new collection from dataset...")
|
| 31 |
|
| 32 |
# Load dataset
|
| 33 |
dataset = load_dataset("Abirate/english_quotes", split="train")
|
|
|
|
| 73 |
metadatas=metadata[i:end_idx]
|
| 74 |
)
|
| 75 |
|
| 76 |
+
print(f" Collection created with {collection.count()} documents!")
|
| 77 |
|
| 78 |
|
| 79 |
+
# SEARCH FUNCTION
|
| 80 |
def semantic_search(query, n_results=5):
|
| 81 |
"""
|
| 82 |
Perform semantic search over the quotes collection.
|
|
|
|
| 115 |
def search_quotes(query, num_results):
|
| 116 |
"""Wrapper for Gradio interface"""
|
| 117 |
if not query.strip():
|
| 118 |
+
return "⚠ Please enter a search query!"
|
| 119 |
return semantic_search(query, n_results=int(num_results))
|
| 120 |
|
| 121 |
|
| 122 |
+
# GRADIO INTERFACE
|
| 123 |
demo = gr.Interface(
|
| 124 |
fn=search_quotes,
|
| 125 |
inputs=[
|
|
|
|
| 133 |
maximum=10,
|
| 134 |
value=5,
|
| 135 |
step=1,
|
| 136 |
+
label=" Number of Results"
|
| 137 |
)
|
| 138 |
],
|
| 139 |
+
outputs=gr.Markdown(label="Search Results"),
|
| 140 |
+
title=" Semantic Quote Search Engine",
|
| 141 |
description="""
|
| 142 |
## Search through famous quotes using AI-powered semantic similarity!
|
| 143 |
|