Ozgur Unlu commited on
Commit
ebeacbc
β€’
1 Parent(s): c3223a8

first initial 2

Browse files
Files changed (2) hide show
  1. app.py +63 -89
  2. requirements.txt +1 -3
app.py CHANGED
@@ -1,106 +1,77 @@
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,
@@ -109,10 +80,13 @@ iface = gr.Interface(
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
- )
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
  import torch
 
4
 
5
  # Load the zero-shot classification model
6
+ try:
7
+ model_name = "MoritzLaurer/DeBERTa-v3-large-mnli-fever-anli-ling-wanli"
8
+ classifier = pipeline("zero-shot-classification",
9
+ model=model_name,
10
+ device=0 if torch.cuda.is_available() else -1)
11
+ except Exception as e:
12
+ print(f"Error loading main model: {e}")
13
+ # Fallback to a lighter model if the first one fails
14
+ model_name = "facebook/bart-large-mnli"
15
+ classifier = pipeline("zero-shot-classification", model=model_name)
 
 
 
 
 
 
 
 
 
 
16
 
17
  def classify_product(ad_text):
18
  if not ad_text.strip():
19
  return "Please enter some ad text."
20
 
21
+ try:
22
+ # Category classification
23
+ category_result = classifier(
24
+ ad_text,
25
+ candidate_labels=[
26
+ "Software", "Electronics", "Clothing", "Food & Beverage",
27
+ "Healthcare", "Financial Services", "Entertainment",
28
+ "Home & Garden", "Automotive", "Education"
29
+ ],
30
+ hypothesis_template="This is an advertisement for a product in the category of",
31
+ multi_label=False
32
+ )
33
+
34
+ # Product type classification
35
+ product_result = classifier(
36
+ ad_text,
37
+ candidate_labels=[
38
+ "software application", "mobile app", "subscription service",
39
+ "physical product", "digital product", "professional service",
40
+ "consumer device", "platform", "tool"
41
+ ],
42
+ hypothesis_template="This is specifically a",
43
+ multi_label=False
44
+ )
45
+
46
+ # Format output string
47
+ output = f"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  πŸ“Š Analysis Results:
49
 
50
+ 🏷️ Category: {category_result['labels'][0]}
51
+ Confidence: {category_result['scores'][0]:.2%}
52
 
53
+ πŸ“¦ Product Type: {product_result['labels'][0]}
54
+ Confidence: {product_result['scores'][0]:.2%}
55
+ """
56
+
57
+ # Additional product details from text
58
+ if any(brand_keyword in ad_text.lower() for brand_keyword in ['by', 'from', 'introducing', 'new']):
59
+ product_name_result = classifier(
60
+ ad_text,
61
+ candidate_labels=["contains brand name", "does not contain brand name"],
62
+ hypothesis_template="This text",
63
+ multi_label=False
64
+ )
65
+ if product_name_result['labels'][0] == "contains brand name":
66
+ output += "\n🏒 Brand Mention: Likely contains a brand name"
67
+
68
+ return output
69
 
70
+ except Exception as e:
71
+ return f"An error occurred: {str(e)}\nPlease try with different text or contact support."
72
 
73
  # Create Gradio interface
74
+ demo = gr.Interface(
75
  fn=classify_product,
76
  inputs=gr.Textbox(
77
  lines=5,
 
80
  ),
81
  outputs=gr.Textbox(label="Analysis Results"),
82
  title="AI Powered Product Identifier from Ad Text",
83
+ description="Paste your marketing ad text to identify the product category and type. Maximum 100 words.",
84
  examples=[
85
  ["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."],
86
  ["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."],
87
  ],
88
  theme=gr.themes.Soft()
89
+ )
90
+
91
+ if __name__ == "__main__":
92
+ demo.launch()
requirements.txt CHANGED
@@ -1,5 +1,3 @@
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
 
1
  gradio==4.7.1
2
  transformers==4.34.0
3
+ torch==2.0.1