Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from sentence_transformers import SentenceTransformer
|
| 3 |
import pandas as pd
|
| 4 |
-
from rapidfuzz import fuzz, process
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
| 8 |
model = SentenceTransformer(model_name)
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
-
data_file = "
|
| 12 |
try:
|
| 13 |
df = pd.read_csv(data_file, nrows=1000) # Limit rows for testing
|
| 14 |
except FileNotFoundError:
|
| 15 |
-
df = pd.DataFrame({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Autocomplete function
|
| 20 |
def autocomplete(query):
|
| 21 |
if not query.strip():
|
| 22 |
-
return [] #
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
return "\n".join([match[0] for match in matches]) # Return matches as a multi-line string
|
| 27 |
|
| 28 |
# Gradio interface
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
-
gr.Markdown("###
|
| 31 |
|
| 32 |
-
# Create a Textbox without live=True
|
| 33 |
query = gr.Textbox(label="Start typing for autocomplete")
|
| 34 |
autocomplete_output = gr.Textbox(label="Autocomplete Suggestions", lines=5)
|
| 35 |
|
| 36 |
-
# Trigger autocomplete on
|
| 37 |
-
query.
|
| 38 |
|
| 39 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
import pandas as pd
|
| 4 |
+
from rapidfuzz import fuzz, process
|
| 5 |
|
| 6 |
+
# Load the model
|
| 7 |
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
| 8 |
model = SentenceTransformer(model_name)
|
| 9 |
|
| 10 |
+
# Load CSV data
|
| 11 |
+
data_file = "Luxury_Products_Apparel_Data.csv" # Ensure this file is uploaded
|
| 12 |
try:
|
| 13 |
df = pd.read_csv(data_file, nrows=1000) # Limit rows for testing
|
| 14 |
except FileNotFoundError:
|
| 15 |
+
df = pd.DataFrame({
|
| 16 |
+
"ProductName": ["Gucci Shoes", "Nike Sneakers", "Louis Vuitton Handbag"],
|
| 17 |
+
"Category": ["Shoes", "Bags"],
|
| 18 |
+
"SubCategory": ["Sneakers", "Totes"]
|
| 19 |
+
}) # Fallback sample data
|
| 20 |
|
| 21 |
+
# Extract relevant fields
|
| 22 |
+
product_names = df["ProductName"].dropna().tolist()
|
| 23 |
+
categories = df["Category"].dropna().unique().tolist()
|
| 24 |
+
subcategories = df["SubCategory"].dropna().unique().tolist()
|
| 25 |
+
|
| 26 |
+
# Merge into one dataset for autocomplete
|
| 27 |
+
autocomplete_data = product_names + categories + subcategories
|
| 28 |
+
|
| 29 |
+
# Clean data by removing unnecessary characters
|
| 30 |
+
autocomplete_data = [str(item).strip('"') for item in autocomplete_data]
|
| 31 |
|
| 32 |
# Autocomplete function
|
| 33 |
def autocomplete(query):
|
| 34 |
if not query.strip():
|
| 35 |
+
return [] # Avoid empty queries
|
| 36 |
+
|
| 37 |
+
# Fuzzy matching with typo tolerance
|
| 38 |
+
matches = process.extract(query, autocomplete_data, scorer=fuzz.partial_ratio, limit=5)
|
| 39 |
|
| 40 |
+
# Return only the best-matched product names, categories, or subcategories
|
| 41 |
+
return [match[0] for match in matches]
|
|
|
|
| 42 |
|
| 43 |
# Gradio interface
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("### Improved Autocomplete for Luxury Products")
|
| 46 |
|
|
|
|
| 47 |
query = gr.Textbox(label="Start typing for autocomplete")
|
| 48 |
autocomplete_output = gr.Textbox(label="Autocomplete Suggestions", lines=5)
|
| 49 |
|
| 50 |
+
# Trigger autocomplete on change
|
| 51 |
+
query.change(fn=autocomplete, inputs=query, outputs=autocomplete_output)
|
| 52 |
|
| 53 |
demo.launch()
|