Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,35 @@
|
|
|
|
|
| 1 |
from flask import Flask, request, render_template
|
| 2 |
-
from
|
| 3 |
import re
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
""
|
| 12 |
-
result = {
|
| 13 |
-
"Brand": None,
|
| 14 |
-
"Category": None,
|
| 15 |
-
"Gender": None,
|
| 16 |
-
"Price": None
|
| 17 |
-
}
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
|
| 24 |
-
"
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 =
|
| 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__":
|