Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
from sentence_transformers import SentenceTransformer
|
| 5 |
import chromadb
|
| 6 |
from chromadb.config import Settings
|
| 7 |
from chromadb.utils import embedding_functions
|
|
@@ -11,7 +9,7 @@ CSV_PATH = "ime-cdata - Sheet1.csv" # Replace with your actual CSV file path
|
|
| 11 |
COLLECTION_NAME = "query_embeddings"
|
| 12 |
EMBEDDING_MODEL = 'all-MiniLM-L6-v2'
|
| 13 |
|
| 14 |
-
# Initialize embedding
|
| 15 |
embedding_function = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=EMBEDDING_MODEL)
|
| 16 |
|
| 17 |
# Initialize Chroma client
|
|
@@ -27,7 +25,7 @@ def load_and_embed_data():
|
|
| 27 |
# Check if collection is empty
|
| 28 |
if collection.count() == 0:
|
| 29 |
print("Embedding and storing data. This may take a while...")
|
| 30 |
-
# Embed and store
|
| 31 |
collection.add(
|
| 32 |
documents=df['query'].tolist(),
|
| 33 |
metadatas=df.to_dict('records'),
|
|
@@ -40,44 +38,56 @@ def load_and_embed_data():
|
|
| 40 |
return collection
|
| 41 |
|
| 42 |
def search_similar_queries(query, collection, top_k=5):
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def gradio_interface(query, top_k):
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
output
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Load and embed data
|
| 83 |
collection = load_and_embed_data()
|
|
@@ -90,7 +100,7 @@ iface = gr.Interface(
|
|
| 90 |
gr.Slider(minimum=1, maximum=10, step=1, label="Top-K results", value=5)
|
| 91 |
],
|
| 92 |
outputs=gr.Textbox(label="Results"),
|
| 93 |
-
title="
|
| 94 |
description="Enter a query to find similar queries with associated metadata."
|
| 95 |
)
|
| 96 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
| 3 |
import chromadb
|
| 4 |
from chromadb.config import Settings
|
| 5 |
from chromadb.utils import embedding_functions
|
|
|
|
| 9 |
COLLECTION_NAME = "query_embeddings"
|
| 10 |
EMBEDDING_MODEL = 'all-MiniLM-L6-v2'
|
| 11 |
|
| 12 |
+
# Initialize embedding function
|
| 13 |
embedding_function = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=EMBEDDING_MODEL)
|
| 14 |
|
| 15 |
# Initialize Chroma client
|
|
|
|
| 25 |
# Check if collection is empty
|
| 26 |
if collection.count() == 0:
|
| 27 |
print("Embedding and storing data. This may take a while...")
|
| 28 |
+
# Embed and store only the query column
|
| 29 |
collection.add(
|
| 30 |
documents=df['query'].tolist(),
|
| 31 |
metadatas=df.to_dict('records'),
|
|
|
|
| 38 |
return collection
|
| 39 |
|
| 40 |
def search_similar_queries(query, collection, top_k=5):
|
| 41 |
+
try:
|
| 42 |
+
results = collection.query(
|
| 43 |
+
query_texts=[query],
|
| 44 |
+
n_results=top_k,
|
| 45 |
+
include=["metadatas", "distances"]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
formatted_results = []
|
| 49 |
+
for i in range(len(results['ids'][0])):
|
| 50 |
+
metadata = results['metadatas'][0][i]
|
| 51 |
+
result = {
|
| 52 |
+
'query': metadata.get('query', 'N/A'),
|
| 53 |
+
'similarity': 1 - results['distances'][0][i], # Convert distance to similarity
|
| 54 |
+
'uber_intent': metadata.get('uber_intent', 'N/A'),
|
| 55 |
+
'common_intent': metadata.get('common_intent', 'N/A'),
|
| 56 |
+
'sub_common_intent': metadata.get('sub_common_intent', 'N/A'),
|
| 57 |
+
'fsc': metadata.get('fsc', 'N/A'),
|
| 58 |
+
'language': metadata.get('language', 'N/A'),
|
| 59 |
+
'Name': metadata.get('Name', 'N/A')
|
| 60 |
+
}
|
| 61 |
+
formatted_results.append(result)
|
| 62 |
+
|
| 63 |
+
return formatted_results
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"Error in search_similar_queries: {str(e)}")
|
| 66 |
+
return []
|
| 67 |
|
| 68 |
def gradio_interface(query, top_k):
|
| 69 |
+
try:
|
| 70 |
+
results = search_similar_queries(query, collection, top_k)
|
| 71 |
+
|
| 72 |
+
if not results:
|
| 73 |
+
return "No results found or an error occurred."
|
| 74 |
+
|
| 75 |
+
output = ""
|
| 76 |
+
for i, result in enumerate(results, 1):
|
| 77 |
+
output += f"Result {i}:\n"
|
| 78 |
+
output += f"Query: {result['query']}\n"
|
| 79 |
+
output += f"Similarity: {result['similarity']:.4f}\n"
|
| 80 |
+
output += f"Uber Intent: {result['uber_intent']}\n"
|
| 81 |
+
output += f"Common Intent: {result['common_intent']}\n"
|
| 82 |
+
output += f"Sub-Common Intent: {result['sub_common_intent']}\n"
|
| 83 |
+
output += f"FSC: {result['fsc']}\n"
|
| 84 |
+
output += f"Language: {result['language']}\n"
|
| 85 |
+
output += f"Name: {result['Name']}\n\n"
|
| 86 |
+
|
| 87 |
+
return output
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"Error in gradio_interface: {str(e)}")
|
| 90 |
+
return f"An error occurred: {str(e)}"
|
| 91 |
|
| 92 |
# Load and embed data
|
| 93 |
collection = load_and_embed_data()
|
|
|
|
| 100 |
gr.Slider(minimum=1, maximum=10, step=1, label="Top-K results", value=5)
|
| 101 |
],
|
| 102 |
outputs=gr.Textbox(label="Results"),
|
| 103 |
+
title="Query Similarity Search",
|
| 104 |
description="Enter a query to find similar queries with associated metadata."
|
| 105 |
)
|
| 106 |
|