DINGOLANI commited on
Commit
0411d57
·
verified ·
1 Parent(s): f925ef2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -63
app.py CHANGED
@@ -1,76 +1,35 @@
 
1
  from flask import Flask, request, render_template
2
- from huggingface_hub import InferenceClient
3
  import re
4
 
5
  app = Flask(__name__)
6
 
7
- # Initialize DeepSeek-R1 client
8
- client = InferenceClient(model="deepseek-ai/deepseek-llm-67b-chat")
 
 
 
 
9
 
10
- def parse_llm_response(response):
11
- """Improved parsing that handles model's raw responses"""
12
- result = {
13
- "Brand": None,
14
- "Category": None,
15
- "Gender": None,
16
- "Price": None
17
- }
18
 
19
- # Enhanced pattern matching for flexible JSON extraction
20
- patterns = {
21
- "brand": r'"brand":\s*"([^"]*)"',
22
- "category": r'"category":\s*"([^"]*)"',
23
- "gender": r'"gender":\s*"([^"]*)"',
24
- "price_range": r'"price_range":\s*"([^"]*)"'
25
- }
26
 
27
- for key, pattern in patterns.items():
28
- match = re.search(pattern, response, re.IGNORECASE)
29
- if match:
30
- value = match.group(1).strip()
31
- if value.lower() in ["null", "n/a", ""]:
32
- continue
33
- if key == "brand":
34
- result["Brand"] = value.title()
35
- elif key == "category":
36
- result["Category"] = value.title()
37
- elif key == "gender":
38
- result["Gender"] = value.title()
39
- elif key == "price_range":
40
- result["Price"] = value.upper()
41
 
42
  return result
43
 
44
- def analyze_query(query):
45
- """Enhanced prompt for luxury brand understanding"""
46
- prompt = f"""Analyze this fashion query and extract structured data. Follow these rules:
47
-
48
- 1. Brand: Identify the luxury fashion brand mentioned (e.g., Gucci, Prada, Balenciaga)
49
- 2. Category: Product type (perfume, bag, shoes, etc.)
50
- 3. Gender: men, women, or unisex
51
- 4. Price: Exact price range from query
52
-
53
- Return JSON format:
54
-
55
- {{
56
- "brand": "<brand name>",
57
- "category": "<product category>",
58
- "gender": "<target gender>",
59
- "price_range": "<price info>"
60
- }}
61
-
62
- Query: "{query}"
63
- """
64
-
65
- response = client.text_generation(
66
- prompt=prompt,
67
- max_new_tokens=200,
68
- temperature=0.01, # More deterministic output
69
- stop_sequences=["\n\n"] # Prevent extra text
70
- )
71
-
72
- return parse_llm_response(response)
73
-
74
  @app.route("/", methods=["GET", "POST"])
75
  def index():
76
  result = None
@@ -78,7 +37,7 @@ def index():
78
  if request.method == "POST":
79
  query = request.form.get("query", "")
80
  if query.strip():
81
- result = analyze_query(query)
82
  return render_template("index.html", result=result, query=query)
83
 
84
  if __name__ == "__main__":
 
1
+ # app.py
2
  from flask import Flask, request, render_template
3
+ from transformers import pipeline
4
  import re
5
 
6
  app = Flask(__name__)
7
 
8
+ # Load lightweight fashion model
9
+ ner_pipeline = pipeline(
10
+ "ner",
11
+ model="Keng/bert-product-ner",
12
+ aggregation_strategy="simple"
13
+ )
14
 
15
+ def extract_details(query):
16
+ result = {"Brand": None, "Category": None, "Price": None}
 
 
 
 
 
 
17
 
18
+ # 1. NER Extraction
19
+ entities = ner_pipeline(query)
20
+ for entity in entities:
21
+ if entity["entity_group"] == "BRAND":
22
+ result["Brand"] = entity["word"].title()
23
+ elif entity["entity_group"] == "PRODUCT":
24
+ result["Category"] = entity["word"].title()
25
 
26
+ # 2. Price Regex Fallback
27
+ price_match = re.search(r"(under|below|less than)\s*(\d+)\s*AED", query, re.IGNORECASE)
28
+ if price_match:
29
+ result["Price"] = f"{price_match.group(1).title()} {price_match.group(2)} AED"
 
 
 
 
 
 
 
 
 
 
30
 
31
  return result
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  @app.route("/", methods=["GET", "POST"])
34
  def index():
35
  result = None
 
37
  if request.method == "POST":
38
  query = request.form.get("query", "")
39
  if query.strip():
40
+ result = extract_details(query)
41
  return render_template("index.html", result=result, query=query)
42
 
43
  if __name__ == "__main__":