Spaces:
Sleeping
Sleeping
import gradio as gr | |
from sentence_transformers import SentenceTransformer | |
import pandas as pd | |
from rapidfuzz import fuzz, process | |
# Load the model | |
model_name = "sentence-transformers/all-MiniLM-L6-v2" | |
model = SentenceTransformer(model_name) | |
# Load CSV data | |
data_file = "Luxury_Products_Apparel_Data.csv" # Ensure this file is uploaded | |
try: | |
df = pd.read_csv(data_file, nrows=1000) # Limit rows for testing | |
except FileNotFoundError: | |
df = pd.DataFrame({ | |
"ProductName": ["Gucci Shoes", "Nike Sneakers", "Louis Vuitton Handbag"], | |
"Category": ["Shoes", "Bags"], | |
"SubCategory": ["Sneakers", "Totes"] | |
}) # Fallback sample data | |
# Extract relevant fields | |
product_names = df["ProductName"].dropna().tolist() | |
categories = df["Category"].dropna().unique().tolist() | |
subcategories = df["SubCategory"].dropna().unique().tolist() | |
# Merge into one dataset for autocomplete | |
autocomplete_data = product_names + categories + subcategories | |
# Clean data by removing unnecessary characters | |
autocomplete_data = [str(item).strip('"') for item in autocomplete_data] | |
# Autocomplete function | |
def autocomplete(query): | |
if not query.strip(): | |
return [] # Avoid empty queries | |
# Fuzzy matching with typo tolerance | |
matches = process.extract(query, autocomplete_data, scorer=fuzz.partial_ratio, limit=5) | |
# Return list of suggestions (Gradio will display them in separate lines) | |
return [match[0] for match in matches] | |
# Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("### Improved Autocomplete for Luxury Products") | |
query = gr.Textbox(label="Start typing for autocomplete") | |
autocomplete_output = gr.Textbox(label="Autocomplete Suggestions", lines=5, interactive=False) | |
# Trigger autocomplete on change | |
query.change(fn=autocomplete, inputs=query, outputs=autocomplete_output) | |
demo.launch() |