Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# Load the SentenceTransformer model
|
| 6 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Encode the query
|
| 16 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 17 |
|
| 18 |
results = []
|
| 19 |
-
for i,
|
| 20 |
-
# Encode the
|
| 21 |
-
|
| 22 |
|
| 23 |
# Calculate cosine similarity
|
| 24 |
-
similarities = util.pytorch_cos_sim(query_embedding,
|
| 25 |
|
| 26 |
# Calculate average similarity for the list
|
| 27 |
avg_similarity = similarities.mean().item()
|
| 28 |
|
| 29 |
-
results.append((i, avg_similarity))
|
| 30 |
|
| 31 |
# Sort results by similarity score (descending)
|
| 32 |
results.sort(key=lambda x: x[1], reverse=True)
|
| 33 |
|
| 34 |
# Format the output
|
| 35 |
output = ""
|
| 36 |
-
for i, score in results:
|
| 37 |
output += f"List {i}: Similarity score {score:.4f}\n"
|
| 38 |
-
|
| 39 |
-
output += f" Element {j}: {item}\n"
|
| 40 |
-
output += "\n"
|
| 41 |
|
| 42 |
return output
|
| 43 |
|
|
@@ -46,11 +59,15 @@ iface = gr.Interface(
|
|
| 46 |
fn=compare_embeddings,
|
| 47 |
inputs=[
|
| 48 |
gr.Textbox(label="Query"),
|
| 49 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
],
|
| 51 |
outputs=gr.Textbox(label="Results"),
|
| 52 |
-
title="
|
| 53 |
-
description="Compare a query with multiple lists of
|
| 54 |
)
|
| 55 |
|
| 56 |
# Launch the app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
# Load the SentenceTransformer model
|
| 6 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 7 |
|
| 8 |
+
def extract_lists(text):
|
| 9 |
+
# Use regex to find all lists in the format ['item1', 'item2', ...]
|
| 10 |
+
pattern = r'\[([^\]]+)\]'
|
| 11 |
+
matches = re.findall(pattern, text)
|
| 12 |
+
|
| 13 |
+
# Process each match into a list of keywords
|
| 14 |
+
lists = []
|
| 15 |
+
for match in matches:
|
| 16 |
+
# Split by comma, strip whitespace and quotes, and filter out empty strings
|
| 17 |
+
keywords = [item.strip().strip("'\"") for item in match.split(',') if item.strip()]
|
| 18 |
+
if keywords: # Only add non-empty lists
|
| 19 |
+
lists.append(keywords)
|
| 20 |
+
|
| 21 |
+
return lists
|
| 22 |
+
|
| 23 |
+
def compare_embeddings(query, lists_text):
|
| 24 |
+
# Extract lists from the input text
|
| 25 |
+
keyword_lists = extract_lists(lists_text)
|
| 26 |
+
|
| 27 |
+
if not keyword_lists:
|
| 28 |
+
return "No valid lists found in the input. Please check the format."
|
| 29 |
|
| 30 |
# Encode the query
|
| 31 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 32 |
|
| 33 |
results = []
|
| 34 |
+
for i, keywords in enumerate(keyword_lists):
|
| 35 |
+
# Encode the keywords
|
| 36 |
+
keyword_embeddings = model.encode(keywords, convert_to_tensor=True)
|
| 37 |
|
| 38 |
# Calculate cosine similarity
|
| 39 |
+
similarities = util.pytorch_cos_sim(query_embedding, keyword_embeddings)[0]
|
| 40 |
|
| 41 |
# Calculate average similarity for the list
|
| 42 |
avg_similarity = similarities.mean().item()
|
| 43 |
|
| 44 |
+
results.append((i, avg_similarity, keywords))
|
| 45 |
|
| 46 |
# Sort results by similarity score (descending)
|
| 47 |
results.sort(key=lambda x: x[1], reverse=True)
|
| 48 |
|
| 49 |
# Format the output
|
| 50 |
output = ""
|
| 51 |
+
for i, score, keywords in results:
|
| 52 |
output += f"List {i}: Similarity score {score:.4f}\n"
|
| 53 |
+
output += f" Keywords: {', '.join(keywords)}\n\n"
|
|
|
|
|
|
|
| 54 |
|
| 55 |
return output
|
| 56 |
|
|
|
|
| 59 |
fn=compare_embeddings,
|
| 60 |
inputs=[
|
| 61 |
gr.Textbox(label="Query"),
|
| 62 |
+
gr.Textbox(
|
| 63 |
+
label="Lists of keywords",
|
| 64 |
+
placeholder="Enter lists in the format: ['keyword1', 'keyword2', ...] ['keyword3', 'keyword4', ...]",
|
| 65 |
+
lines=5
|
| 66 |
+
),
|
| 67 |
],
|
| 68 |
outputs=gr.Textbox(label="Results"),
|
| 69 |
+
title="Keyword Lists Comparison App",
|
| 70 |
+
description="Compare a query with multiple lists of keywords and find the most relevant lists. Enter each list in square brackets, separated by commas."
|
| 71 |
)
|
| 72 |
|
| 73 |
# Launch the app
|