Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from flair.data import Sentence
|
| 4 |
+
from flair.models import SequenceTagger
|
| 5 |
+
import requests
|
| 6 |
+
import re
|
| 7 |
+
from bs4 import BeautifulSoup
|
| 8 |
+
|
| 9 |
+
# Load FashionNLP (Flair-based NER model)
|
| 10 |
+
tagger = SequenceTagger.load("flair/ner-english-large")
|
| 11 |
+
|
| 12 |
+
# Regex for extracting price
|
| 13 |
+
price_pattern = re.compile(r'(\bunder\b|\babove\b|\bbelow\b|\bbetween\b)?\s?(\d{1,5})\s?(AED|USD|EUR)?', re.IGNORECASE)
|
| 14 |
+
|
| 15 |
+
# Keywords for gender extraction
|
| 16 |
+
gender_keywords = ["men", "male", "women", "female", "unisex"]
|
| 17 |
+
|
| 18 |
+
def extract_fashion_entities(text):
|
| 19 |
+
"""
|
| 20 |
+
Extracts fashion-related entities (Brand, Category, Material, Price, Gender) from text.
|
| 21 |
+
"""
|
| 22 |
+
sentence = Sentence(text)
|
| 23 |
+
tagger.predict(sentence)
|
| 24 |
+
|
| 25 |
+
extracted_entities = {"Brand": "Unknown", "Category": "Unknown", "Material": "Unknown", "Price": "Unknown", "Gender": "Unknown"}
|
| 26 |
+
|
| 27 |
+
for entity in sentence.get_spans('ner'):
|
| 28 |
+
entity_text = entity.text
|
| 29 |
+
entity_label = entity.tag
|
| 30 |
+
|
| 31 |
+
if entity_label in ["ORG", "BRAND", "HOUSE"]:
|
| 32 |
+
extracted_entities["Brand"] = entity_text
|
| 33 |
+
elif entity_label in ["PRODUCT", "CATEGORY"]:
|
| 34 |
+
extracted_entities["Category"] = entity_text
|
| 35 |
+
elif entity_label in ["MATERIAL"]:
|
| 36 |
+
extracted_entities["Material"] = entity_text
|
| 37 |
+
elif entity_label in ["PRICE"]:
|
| 38 |
+
extracted_entities["Price"] = entity_text
|
| 39 |
+
elif entity_label in ["GENDER"]:
|
| 40 |
+
extracted_entities["Gender"] = entity_text
|
| 41 |
+
|
| 42 |
+
# Extract gender
|
| 43 |
+
for gender in gender_keywords:
|
| 44 |
+
if gender in text.lower():
|
| 45 |
+
extracted_entities["Gender"] = gender.capitalize()
|
| 46 |
+
break
|
| 47 |
+
|
| 48 |
+
# Extract price if not found by NER
|
| 49 |
+
price_match = price_pattern.search(text)
|
| 50 |
+
if price_match and extracted_entities["Price"] == "Unknown":
|
| 51 |
+
condition, amount, currency = price_match.groups()
|
| 52 |
+
extracted_entities["Price"] = f"{condition.capitalize() if condition else ''} {amount} {currency if currency else 'AED'}".strip()
|
| 53 |
+
|
| 54 |
+
return extracted_entities
|
| 55 |
+
|
| 56 |
+
def scrape_fashion_trends(url):
|
| 57 |
+
"""
|
| 58 |
+
Scrapes fashion trend articles from a given URL and extracts key entities.
|
| 59 |
+
"""
|
| 60 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 61 |
+
response = requests.get(url, headers=headers)
|
| 62 |
+
|
| 63 |
+
if response.status_code != 200:
|
| 64 |
+
return {"Error": "Unable to fetch data"}
|
| 65 |
+
|
| 66 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 67 |
+
|
| 68 |
+
# Extract article text
|
| 69 |
+
paragraphs = soup.find_all("p")
|
| 70 |
+
text = " ".join([p.text for p in paragraphs])
|
| 71 |
+
|
| 72 |
+
# Run entity extraction
|
| 73 |
+
extracted_trends = extract_fashion_entities(text)
|
| 74 |
+
|
| 75 |
+
return extracted_trends
|
| 76 |
+
|
| 77 |
+
# Define Gradio UI
|
| 78 |
+
def parse_fashion_query(user_query):
|
| 79 |
+
"""
|
| 80 |
+
Parses a fashion search query and extracts structured attributes.
|
| 81 |
+
"""
|
| 82 |
+
return extract_fashion_entities(user_query)
|
| 83 |
+
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("# ποΈ Luxury Fashion Query Parser using FashionNLP")
|
| 86 |
+
|
| 87 |
+
with gr.Tab("Search Query Parser"):
|
| 88 |
+
query_input = gr.Textbox(label="Enter your search query", placeholder="e.g., Gucci menβs perfume under 200AED")
|
| 89 |
+
output_box = gr.JSON(label="Parsed Output")
|
| 90 |
+
parse_button = gr.Button("Parse Query")
|
| 91 |
+
parse_button.click(parse_fashion_query, inputs=[query_input], outputs=[output_box])
|
| 92 |
+
|
| 93 |
+
with gr.Tab("Fashion Trends Analyzer"):
|
| 94 |
+
url_input = gr.Textbox(label="Enter Fashion News URL", placeholder="e.g., https://www.vogue.com/fashion")
|
| 95 |
+
trends_output = gr.JSON(label="Extracted Trends")
|
| 96 |
+
scrape_button = gr.Button("Analyze Trends")
|
| 97 |
+
scrape_button.click(scrape_fashion_trends, inputs=[url_input], outputs=[trends_output])
|
| 98 |
+
|
| 99 |
+
demo.launch()
|