File size: 4,450 Bytes
d38e058
 
 
 
3356a7c
 
 
d38e058
 
3356a7c
 
a10ba15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3356a7c
d38e058
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import gradio as gr
from transformers import pipeline
import torch

# Global variable to cache the model
_classifier = None

def load_model():
    """Load the intent classification model"""
    global _classifier
    if _classifier is None:
        model_name = "YosefA/adfluence-intent-model"
        
        # Try multiple approaches to load the model
        loading_strategies = [
            {
                "name": "Standard loading with trust_remote_code",
                "kwargs": {"trust_remote_code": True, "return_all_scores": True}
            },
            {
                "name": "Loading with revision='main'",
                "kwargs": {"revision": "main", "return_all_scores": True}
            },
            {
                "name": "Loading with use_fast=False",
                "kwargs": {"use_fast": False, "return_all_scores": True}
            },
            {
                "name": "Loading with legacy tokenizer",
                "kwargs": {"use_fast": False, "trust_remote_code": True, "return_all_scores": True}
            }
        ]
        
        for strategy in loading_strategies:
            try:
                print(f"Trying: {strategy['name']}")
                _classifier = pipeline(
                    "text-classification",
                    model=model_name,
                    **strategy['kwargs']
                )
                print(f"βœ… Model loaded successfully using: {strategy['name']}")
                return _classifier
                
            except Exception as e:
                print(f"❌ Failed with {strategy['name']}: {e}")
                continue
        
        print("❌ All loading strategies failed")
        return None
    return _classifier

def classify_intent(comment):
    """
    Classify the intent of a comment
    
    Args:
        comment (str): The input comment text
        
    Returns:
        dict: Classification results with labels and scores
    """
    if not comment.strip():
        return "Please enter a comment to classify."
    
    classifier = load_model()
    if classifier is None:
        return "Error: Could not load the model. Please try again later."
    
    try:
        # Get predictions
        results = classifier(comment)
        
        # Format results for display
        formatted_results = []
        for result in results:
            for item in result:
                label = item['label']
                score = item['score']
                formatted_results.append(f"{label}: {score:.4f} ({score*100:.2f}%)")
        
        return "\n".join(formatted_results)
        
    except Exception as e:
        return f"Error during classification: {str(e)}"

# Create the Gradio interface
with gr.Blocks(title="Ad Comments Intent Classifier") as demo:
    gr.Markdown("""
    # 🎯 Ad Comments Intent Classifier
    
    This app classifies the intent of comments related to advertisements using the **YosefA/adfluence-intent-model**.
    
    Simply enter a comment below and get the classification results with confidence scores.
    """)
    
    with gr.Row():
        with gr.Column():
            comment_input = gr.Textbox(
                label="Comment Text",
                placeholder="Enter your comment here...",
                lines=3,
                max_lines=10
            )
            
            classify_btn = gr.Button("πŸ” Classify Intent", variant="primary")
            
        with gr.Column():
            output = gr.Textbox(
                label="Classification Results",
                lines=5,
                max_lines=10,
                interactive=False
            )
    
    # Example inputs
    gr.Examples(
        examples=[
            ["This product looks amazing! Where can I buy it?"],
            ["This is clearly a scam, don't trust it."],
            ["I love this brand, they make quality products."],
            ["The price seems too high for what you get."],
            ["Has anyone tried this? I'm curious about reviews."]
        ],
        inputs=comment_input,
        label="πŸ“ Example Comments"
    )
    
    # Set up the event handlers
    classify_btn.click(
        fn=classify_intent,
        inputs=comment_input,
        outputs=output
    )
    
    comment_input.submit(
        fn=classify_intent,
        inputs=comment_input,
        outputs=output
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()