Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	
		Ozgur Unlu
		
	commited on
		
		
					Commit 
							
							·
						
						ebeacbc
	
1
								Parent(s):
							
							c3223a8
								
first initial 2
Browse files- app.py +63 -89
- 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 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
                 | 
| 17 | 
            -
                 | 
| 18 | 
            -
                 | 
| 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 | 
            -
                 | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
                     | 
| 46 | 
            -
                     | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
                        " | 
| 56 | 
            -
                         | 
| 57 | 
            -
             | 
| 58 | 
            -
                     | 
| 59 | 
            -
                     | 
| 60 | 
            -
                     | 
| 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: { | 
| 87 | 
            -
               Confidence: { | 
| 88 |  | 
| 89 | 
            -
            📦 Product Type: { | 
| 90 | 
            -
               Confidence: { | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 99 |  | 
| 100 | 
            -
                 | 
|  | |
| 101 |  | 
| 102 | 
             
            # Create Gradio interface
         | 
| 103 | 
            -
             | 
| 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 | 
| 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
         | 
|  | |
|  |