File size: 3,272 Bytes
d38e058
 
 
 
3356a7c
 
 
d38e058
 
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
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:
        try:
            _classifier = pipeline(
                "text-classification",
                model="YosefA/adfluence-intent-model",
                return_all_scores=True
            )
        except Exception as e:
            print(f"Error loading model: {e}")
            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()