Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
|
| 7 |
+
# Load the model
|
| 8 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
+
|
| 10 |
+
def load_csv(file):
|
| 11 |
+
df = pd.read_csv(file.name)
|
| 12 |
+
return df
|
| 13 |
+
|
| 14 |
+
def search_similar_queries(query, df, top_k=5):
|
| 15 |
+
# Encode the query
|
| 16 |
+
query_embedding = model.encode([query])
|
| 17 |
+
|
| 18 |
+
# Encode all queries in the DataFrame
|
| 19 |
+
all_embeddings = model.encode(df['query'].tolist())
|
| 20 |
+
|
| 21 |
+
# Calculate cosine similarity
|
| 22 |
+
similarities = cosine_similarity(query_embedding, all_embeddings)[0]
|
| 23 |
+
|
| 24 |
+
# Get top-k similar queries
|
| 25 |
+
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 26 |
+
|
| 27 |
+
results = []
|
| 28 |
+
for idx in top_indices:
|
| 29 |
+
result = {
|
| 30 |
+
'query': df.iloc[idx]['query'],
|
| 31 |
+
'similarity': similarities[idx],
|
| 32 |
+
'uber_intent': df.iloc[idx]['uber_intent'],
|
| 33 |
+
'common_intent': df.iloc[idx]['common_intent'],
|
| 34 |
+
'sub_common_intent': df.iloc[idx]['sub_common_intent'],
|
| 35 |
+
'fsc': df.iloc[idx]['fsc'],
|
| 36 |
+
'language': df.iloc[idx]['language'],
|
| 37 |
+
'Name': df.iloc[idx]['Name']
|
| 38 |
+
}
|
| 39 |
+
results.append(result)
|
| 40 |
+
|
| 41 |
+
return results
|
| 42 |
+
|
| 43 |
+
def gradio_interface(csv_file, query, top_k):
|
| 44 |
+
df = load_csv(csv_file)
|
| 45 |
+
results = search_similar_queries(query, df, top_k)
|
| 46 |
+
|
| 47 |
+
output = ""
|
| 48 |
+
for i, result in enumerate(results, 1):
|
| 49 |
+
output += f"Result {i}:\n"
|
| 50 |
+
output += f"Query: {result['query']}\n"
|
| 51 |
+
output += f"Similarity: {result['similarity']:.4f}\n"
|
| 52 |
+
output += f"Uber Intent: {result['uber_intent']}\n"
|
| 53 |
+
output += f"Common Intent: {result['common_intent']}\n"
|
| 54 |
+
output += f"Sub-Common Intent: {result['sub_common_intent']}\n"
|
| 55 |
+
output += f"FSC: {result['fsc']}\n"
|
| 56 |
+
output += f"Language: {result['language']}\n"
|
| 57 |
+
output += f"Name: {result['Name']}\n\n"
|
| 58 |
+
|
| 59 |
+
return output
|
| 60 |
+
|
| 61 |
+
# Create Gradio interface
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=gradio_interface,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.File(label="Upload CSV file"),
|
| 66 |
+
gr.Textbox(label="Enter your query"),
|
| 67 |
+
gr.Slider(minimum=1, maximum=10, step=1, label="Top-K results", value=5)
|
| 68 |
+
],
|
| 69 |
+
outputs=gr.Textbox(label="Results"),
|
| 70 |
+
title="Query Similarity Search",
|
| 71 |
+
description="Upload a CSV file, enter a query, and find similar queries with associated metadata."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Launch the interface
|
| 75 |
+
iface.launch()
|