DINGOLANI commited on
Commit
754152d
·
verified ·
1 Parent(s): 52dcfcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -1,11 +1,15 @@
1
  import gradio as gr
2
  import torch
3
  import re
4
- from transformers import pipeline
5
 
6
- # Load fine-tuned NER model from Hugging Face Hub
7
- model_name = "luxury-fashion-ner"
8
- ner_pipeline = pipeline("ner", model=model_name, tokenizer=model_name)
 
 
 
 
9
 
10
  # Regex for extracting price
11
  price_pattern = re.compile(r'(\bunder\b|\babove\b|\bbelow\b|\bbetween\b)?\s?(\d{1,5})\s?(AED|USD|EUR)?', re.IGNORECASE)
@@ -15,7 +19,7 @@ gender_keywords = ["men", "male", "women", "female", "unisex"]
15
 
16
  def extract_attributes(query):
17
  """
18
- Extract structured fashion attributes dynamically using the fine-tuned NER model.
19
  """
20
  structured_output = {"Brand": "Unknown", "Category": "Unknown", "Gender": "Unknown", "Price": "Unknown"}
21
 
@@ -26,12 +30,12 @@ def extract_attributes(query):
26
  entity_text = entity["word"].replace("##", "") # Fix tokenization artifacts
27
  entity_label = entity["entity"]
28
 
29
- if "ORG" in entity_label: # Organization = Brand
30
  structured_output["Brand"] = entity_text
31
- elif "MISC" in entity_label: # Miscellaneous = Category
32
  structured_output["Category"] = entity_text
33
- elif "LOC" in entity_label: # Locations (sometimes used for brands)
34
- structured_output["Brand"] = entity_text
35
 
36
  # Extract gender
37
  for gender in gender_keywords:
@@ -39,9 +43,9 @@ def extract_attributes(query):
39
  structured_output["Gender"] = gender.capitalize()
40
  break
41
 
42
- # Extract price
43
  price_match = price_pattern.search(query)
44
- if price_match:
45
  condition, amount, currency = price_match.groups()
46
  structured_output["Price"] = f"{condition.capitalize() if condition else ''} {amount} {currency if currency else 'AED'}".strip()
47
 
@@ -57,7 +61,7 @@ def parse_query(user_query):
57
 
58
  # Create Gradio Interface
59
  with gr.Blocks() as demo:
60
- gr.Markdown("# 🛍️ Luxury Fashion Query Parser using Fine-Tuned NER Model")
61
 
62
  query_input = gr.Textbox(label="Enter your search query", placeholder="e.g., Gucci men’s perfume under 200AED")
63
  output_box = gr.JSON(label="Parsed Output")
 
1
  import gradio as gr
2
  import torch
3
  import re
4
+ from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
5
 
6
+ # Load NER-Luxury model from Hugging Face
7
+ model_name = "AkimfromParis/NER-Luxury"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForTokenClassification.from_pretrained(model_name)
10
+
11
+ # Load pipeline for Named Entity Recognition (NER)
12
+ ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
13
 
14
  # Regex for extracting price
15
  price_pattern = re.compile(r'(\bunder\b|\babove\b|\bbelow\b|\bbetween\b)?\s?(\d{1,5})\s?(AED|USD|EUR)?', re.IGNORECASE)
 
19
 
20
  def extract_attributes(query):
21
  """
22
+ Extract structured fashion attributes dynamically using the fine-tuned NER-Luxury model.
23
  """
24
  structured_output = {"Brand": "Unknown", "Category": "Unknown", "Gender": "Unknown", "Price": "Unknown"}
25
 
 
30
  entity_text = entity["word"].replace("##", "") # Fix tokenization artifacts
31
  entity_label = entity["entity"]
32
 
33
+ if "HOUSE" in entity_label or "BRAND" in entity_label: # Luxury brands
34
  structured_output["Brand"] = entity_text
35
+ elif "CATEGORY" in entity_label: # Fashion categories
36
  structured_output["Category"] = entity_text
37
+ elif "MONETARYVALUE" in entity_label: # Price values
38
+ structured_output["Price"] = entity_text
39
 
40
  # Extract gender
41
  for gender in gender_keywords:
 
43
  structured_output["Gender"] = gender.capitalize()
44
  break
45
 
46
+ # Extract price if not found by NER
47
  price_match = price_pattern.search(query)
48
+ if price_match and structured_output["Price"] == "Unknown":
49
  condition, amount, currency = price_match.groups()
50
  structured_output["Price"] = f"{condition.capitalize() if condition else ''} {amount} {currency if currency else 'AED'}".strip()
51
 
 
61
 
62
  # Create Gradio Interface
63
  with gr.Blocks() as demo:
64
+ gr.Markdown("# 🛍️ Luxury Fashion Query Parser using NER-Luxury")
65
 
66
  query_input = gr.Textbox(label="Enter your search query", placeholder="e.g., Gucci men’s perfume under 200AED")
67
  output_box = gr.JSON(label="Parsed Output")