Spaces:
Runtime error
Runtime error
File size: 2,453 Bytes
75fdda9 171a063 8c1ee79 57b32e0 75fdda9 57b32e0 75fdda9 57b32e0 8c1ee79 57b32e0 8c1ee79 75fdda9 57b32e0 75fdda9 8c1ee79 75fdda9 57b32e0 171a063 57b32e0 8c1ee79 57b32e0 171a063 57b32e0 171a063 57b32e0 8c1ee79 171a063 8c1ee79 57b32e0 8c1ee79 57b32e0 8c1ee79 57b32e0 75fdda9 57b32e0 8c1ee79 75fdda9 171a063 75fdda9 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import gradio as gr
import torch
import re
from transformers import pipeline
# Load fine-tuned NER model from Hugging Face Hub
model_name = "luxury-fashion-ner"
ner_pipeline = pipeline("ner", model=model_name, tokenizer=model_name)
# Regex for extracting price
price_pattern = re.compile(r'(\bunder\b|\babove\b|\bbelow\b|\bbetween\b)?\s?(\d{1,5})\s?(AED|USD|EUR)?', re.IGNORECASE)
# Keywords for gender extraction
gender_keywords = ["men", "male", "women", "female", "unisex"]
def extract_attributes(query):
"""
Extract structured fashion attributes dynamically using the fine-tuned NER model.
"""
structured_output = {"Brand": "Unknown", "Category": "Unknown", "Gender": "Unknown", "Price": "Unknown"}
# Run NER model on query
entities = ner_pipeline(query)
for entity in entities:
entity_text = entity["word"].replace("##", "") # Fix tokenization artifacts
entity_label = entity["entity"]
if "ORG" in entity_label: # Organization = Brand
structured_output["Brand"] = entity_text
elif "MISC" in entity_label: # Miscellaneous = Category
structured_output["Category"] = entity_text
elif "LOC" in entity_label: # Locations (sometimes used for brands)
structured_output["Brand"] = entity_text
# Extract gender
for gender in gender_keywords:
if gender in query.lower():
structured_output["Gender"] = gender.capitalize()
break
# Extract price
price_match = price_pattern.search(query)
if price_match:
condition, amount, currency = price_match.groups()
structured_output["Price"] = f"{condition.capitalize() if condition else ''} {amount} {currency if currency else 'AED'}".strip()
return structured_output
# Define Gradio UI
def parse_query(user_query):
"""
Parses fashion-related queries into structured attributes.
"""
parsed_output = extract_attributes(user_query)
return parsed_output # JSON output
# Create Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("# 🛍️ Luxury Fashion Query Parser using Fine-Tuned NER Model")
query_input = gr.Textbox(label="Enter your search query", placeholder="e.g., Gucci men’s perfume under 200AED")
output_box = gr.JSON(label="Parsed Output")
parse_button = gr.Button("Parse Query")
parse_button.click(parse_query, inputs=[query_input], outputs=[output_box])
demo.launch() |