File size: 1,284 Bytes
0411d57
88c3dd8
0411d57
88c3dd8
 
 
 
0411d57
 
 
 
 
 
88c3dd8
0411d57
 
88c3dd8
0411d57
 
 
 
 
 
 
88c3dd8
0411d57
 
 
 
88c3dd8
 
 
 
 
59f66b5
 
88c3dd8
59f66b5
 
0411d57
59f66b5
88c3dd8
 
59f66b5
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
# app.py
from flask import Flask, request, render_template
from transformers import pipeline
import re

app = Flask(__name__)

# Load lightweight fashion model
ner_pipeline = pipeline(
    "ner",
    model="Keng/bert-product-ner",
    aggregation_strategy="simple"
)

def extract_details(query):
    result = {"Brand": None, "Category": None, "Price": None}
    
    # 1. NER Extraction
    entities = ner_pipeline(query)
    for entity in entities:
        if entity["entity_group"] == "BRAND":
            result["Brand"] = entity["word"].title()
        elif entity["entity_group"] == "PRODUCT":
            result["Category"] = entity["word"].title()
    
    # 2. Price Regex Fallback
    price_match = re.search(r"(under|below|less than)\s*(\d+)\s*AED", query, re.IGNORECASE)
    if price_match:
        result["Price"] = f"{price_match.group(1).title()} {price_match.group(2)} AED"
    
    return result

@app.route("/", methods=["GET", "POST"])
def index():
    result = None
    query = ""
    if request.method == "POST":
        query = request.form.get("query", "")
        if query.strip():
            result = extract_details(query)
    return render_template("index.html", result=result, query=query)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)