Spaces:
Sleeping
Sleeping
File size: 1,858 Bytes
debeeab 90a841f f053894 90a841f debeeab 90a841f fd80bbd b718343 90a841f b718343 fd80bbd b718343 90a841f f053894 90a841f debeeab fd80bbd debeeab d52c941 90a841f 0ede6b5 8a7677b 90a841f debeeab fd80bbd debeeab 90a841f fd80bbd 411666d 8a7677b fd80bbd 90a841f debeeab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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() |