QueryAnalyzer / app.py
DINGOLANI's picture
Update app.py
0411d57 verified
# 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)