Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,11 +35,14 @@ category_data = df[category_column].dropna().unique().tolist()
|
|
35 |
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
36 |
model = SentenceTransformer(model_name)
|
37 |
|
38 |
-
# Function to find synonyms dynamically
|
39 |
def find_synonym(word, top_n=1):
|
40 |
query_embedding = model.encode(word, convert_to_tensor=True)
|
41 |
results = util.semantic_search(query_embedding, model.encode(designer_data + category_data, convert_to_tensor=True), top_k=top_n)
|
42 |
-
|
|
|
|
|
|
|
43 |
|
44 |
# Function to correct spellings
|
45 |
def correct_spelling(word):
|
@@ -50,14 +53,15 @@ def correct_spelling(word):
|
|
50 |
return best_match
|
51 |
return word
|
52 |
|
53 |
-
# Autocomplete function
|
54 |
def autocomplete(query):
|
55 |
if not query.strip():
|
56 |
return "None", "None", [], []
|
57 |
|
58 |
original_query = query.strip()
|
59 |
corrected_query = correct_spelling(original_query)
|
60 |
-
|
|
|
61 |
|
62 |
# Perform fuzzy matching for designers and categories separately
|
63 |
designer_matches = process.extract(synonym_query, designer_data, scorer=fuzz.partial_ratio, limit=5)
|
@@ -89,4 +93,4 @@ with gr.Blocks() as demo:
|
|
89 |
outputs=[correction_output, synonym_output, designer_output, category_output]
|
90 |
)
|
91 |
|
92 |
-
demo.launch()
|
|
|
35 |
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
36 |
model = SentenceTransformer(model_name)
|
37 |
|
38 |
+
# Function to find synonyms dynamically with fallback
|
39 |
def find_synonym(word, top_n=1):
|
40 |
query_embedding = model.encode(word, convert_to_tensor=True)
|
41 |
results = util.semantic_search(query_embedding, model.encode(designer_data + category_data, convert_to_tensor=True), top_k=top_n)
|
42 |
+
# Check if results exist
|
43 |
+
if results and len(results[0]) > 0:
|
44 |
+
return [designer_data + category_data[result['corpus_id']] for result in results[0] if result['score'] > 0.6]
|
45 |
+
return [] # Return an empty list if no results
|
46 |
|
47 |
# Function to correct spellings
|
48 |
def correct_spelling(word):
|
|
|
53 |
return best_match
|
54 |
return word
|
55 |
|
56 |
+
# Autocomplete function with safe handling of synonyms
|
57 |
def autocomplete(query):
|
58 |
if not query.strip():
|
59 |
return "None", "None", [], []
|
60 |
|
61 |
original_query = query.strip()
|
62 |
corrected_query = correct_spelling(original_query)
|
63 |
+
synonym_results = find_synonym(corrected_query, top_n=1)
|
64 |
+
synonym_query = synonym_results[0] if synonym_results else corrected_query
|
65 |
|
66 |
# Perform fuzzy matching for designers and categories separately
|
67 |
designer_matches = process.extract(synonym_query, designer_data, scorer=fuzz.partial_ratio, limit=5)
|
|
|
93 |
outputs=[correction_output, synonym_output, designer_output, category_output]
|
94 |
)
|
95 |
|
96 |
+
demo.launch(share=True)
|