Spaces:
Sleeping
Sleeping
Ozgur Unlu
commited on
Commit
·
c3223a8
1
Parent(s):
a7e2260
initial start
Browse files- app.py +118 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
5 |
+
import torch
|
6 |
+
import spacy
|
7 |
+
|
8 |
+
# Load the zero-shot classification model
|
9 |
+
model_name = "MoritzLaurer/DeBERTa-v3-large-mnli-fever-anli-ling-wanli"
|
10 |
+
classifier = pipeline("zero-shot-classification", model=model_name)
|
11 |
+
|
12 |
+
# Load spaCy for brand name extraction
|
13 |
+
nlp = spacy.load("en_core_web_sm")
|
14 |
+
|
15 |
+
def extract_brand_names(text):
|
16 |
+
doc = nlp(text)
|
17 |
+
# Look for organization names and proper nouns that might be brands
|
18 |
+
potential_brands = []
|
19 |
+
for ent in doc.ents:
|
20 |
+
if ent.label_ in ["ORG", "PRODUCT"]:
|
21 |
+
potential_brands.append((ent.text, 0.9)) # High confidence for named entities
|
22 |
+
|
23 |
+
# Also check for proper nouns that might be brands
|
24 |
+
for token in doc:
|
25 |
+
if token.pos_ == "PROPN" and token.text not in [brand[0] for brand in potential_brands]:
|
26 |
+
potential_brands.append((token.text, 0.7)) # Lower confidence for proper nouns
|
27 |
+
|
28 |
+
return potential_brands
|
29 |
+
|
30 |
+
def classify_product(ad_text):
|
31 |
+
if not ad_text.strip():
|
32 |
+
return "Please enter some ad text."
|
33 |
+
|
34 |
+
# Category classification
|
35 |
+
category_hypothesis = "This is an advertisement for a product in the category of"
|
36 |
+
candidate_categories = [
|
37 |
+
"Software", "Electronics", "Clothing", "Food & Beverage",
|
38 |
+
"Healthcare", "Financial Services", "Entertainment",
|
39 |
+
"Home & Garden", "Automotive", "Education"
|
40 |
+
]
|
41 |
+
|
42 |
+
category_result = classifier(
|
43 |
+
ad_text,
|
44 |
+
candidate_labels=candidate_categories,
|
45 |
+
hypothesis_template=category_hypothesis,
|
46 |
+
multi_label=False
|
47 |
+
)
|
48 |
+
|
49 |
+
# Product type classification
|
50 |
+
product_hypothesis = "This is specifically a"
|
51 |
+
# We'll let the model infer specific product types based on the text
|
52 |
+
product_result = classifier(
|
53 |
+
ad_text,
|
54 |
+
candidate_labels=[
|
55 |
+
"software application", "mobile app", "subscription service",
|
56 |
+
"physical product", "digital product", "professional service",
|
57 |
+
"consumer device", "platform", "tool"
|
58 |
+
],
|
59 |
+
hypothesis_template=product_hypothesis,
|
60 |
+
multi_label=False
|
61 |
+
)
|
62 |
+
|
63 |
+
# Brand extraction
|
64 |
+
brands = extract_brand_names(ad_text)
|
65 |
+
|
66 |
+
# Format results
|
67 |
+
results = {
|
68 |
+
"Category": {
|
69 |
+
"classification": category_result["labels"][0],
|
70 |
+
"confidence": f"{category_result['scores'][0]:.2%}"
|
71 |
+
},
|
72 |
+
"Product Type": {
|
73 |
+
"classification": product_result["labels"][0],
|
74 |
+
"confidence": f"{product_result['scores'][0]:.2%}"
|
75 |
+
},
|
76 |
+
"Detected Brands": [
|
77 |
+
{"brand": brand, "confidence": f"{conf:.2%}"}
|
78 |
+
for brand, conf in brands
|
79 |
+
] if brands else "No specific brand detected"
|
80 |
+
}
|
81 |
+
|
82 |
+
# Format output string
|
83 |
+
output = f"""
|
84 |
+
📊 Analysis Results:
|
85 |
+
|
86 |
+
🏷️ Category: {results['Category']['classification']}
|
87 |
+
Confidence: {results['Category']['confidence']}
|
88 |
+
|
89 |
+
📦 Product Type: {results['Product Type']['classification']}
|
90 |
+
Confidence: {results['Product Type']['confidence']}
|
91 |
+
|
92 |
+
🏢 Brand Detection:"""
|
93 |
+
|
94 |
+
if isinstance(results["Detected Brands"], list):
|
95 |
+
for brand_info in results["Detected Brands"]:
|
96 |
+
output += f"\n • {brand_info['brand']} (Confidence: {brand_info['confidence']})"
|
97 |
+
else:
|
98 |
+
output += f"\n {results['Detected Brands']}"
|
99 |
+
|
100 |
+
return output
|
101 |
+
|
102 |
+
# Create Gradio interface
|
103 |
+
iface = gr.Interface(
|
104 |
+
fn=classify_product,
|
105 |
+
inputs=gr.Textbox(
|
106 |
+
lines=5,
|
107 |
+
placeholder="Paste your ad text here (max 100 words)...",
|
108 |
+
label="Advertisement Text"
|
109 |
+
),
|
110 |
+
outputs=gr.Textbox(label="Analysis Results"),
|
111 |
+
title="AI Powered Product Identifier from Ad Text",
|
112 |
+
description="Paste your marketing ad text to identify the product category, type, and brand mentions.",
|
113 |
+
examples=[
|
114 |
+
["Experience seamless productivity with our new CloudWork Pro subscription. This AI-powered workspace solution helps remote teams collaborate better with smart document sharing, real-time editing, and integrated chat features. Starting at $29/month."],
|
115 |
+
["Introducing the new iPhone 15 Pro with revolutionary A17 Pro chip. Capture stunning photos with our advanced 48MP camera system. Available in titanium finish with all-day battery life. Pre-order now at Apple stores nationwide."],
|
116 |
+
],
|
117 |
+
theme=gr.themes.Soft()
|
118 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.7.1
|
2 |
+
transformers==4.34.0
|
3 |
+
torch==2.0.1
|
4 |
+
spacy==3.7.2
|
5 |
+
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.0/en_core_web_sm-3.7.0.tar.gz
|