File size: 4,513 Bytes
04e7b78
9604b3c
 
d3061d0
9604b3c
 
 
d3061d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9604b3c
d3061d0
 
 
 
 
 
 
 
 
 
9604b3c
d3061d0
 
9604b3c
 
d3061d0
 
 
 
9604b3c
d3061d0
9604b3c
d3061d0
9604b3c
d3061d0
9604b3c
d3061d0
 
 
 
9604b3c
d3061d0
 
 
 
 
9604b3c
d3061d0
 
 
9604b3c
d3061d0
 
 
 
9604b3c
d3061d0
 
 
 
 
 
 
9604b3c
d3061d0
 
 
9604b3c
 
 
d3061d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Initialize the classifier
classifier = pipeline("zero-shot-classification", model="tasksource/ModernBERT-base-nli")

def zeroShotClassification(text_input, candidate_labels):
    # Clean and process the labels
    labels = [label.strip() for label in candidate_labels.split(',')]
    
    # Get predictions
    prediction = classifier(text_input, labels)
    
    # Format results as percentage with 2 decimal places
    results = {label: f"{score*100:.2f}%" 
              for label, score in zip(prediction['labels'], prediction['scores'])}
    
    # Create markdown output for detailed view
    markdown_output = "### Results Breakdown:\n\n"
    for label, score in sorted(results.items(), key=lambda x: float(x[1].rstrip('%')), reverse=True):
        # Create confidence bar using Unicode blocks
        score_num = float(score.rstrip('%'))
        blocks = "β–ˆ" * int(score_num/5) + "β–‘" * (20 - int(score_num/5))
        markdown_output += f"**{label}**: {blocks} {score}\n\n"
    
    return results, markdown_output

# More diverse examples
examples = [
    ["One day I will see the world", "travel, adventure, dreams, future"],
    ["The movie had amazing special effects but a weak plot", "entertainment, technology, criticism, story"],
    ["This new phone has an amazing camera and great battery life", "technology, photography, consumer, review"],
    ["Mix flour, sugar, and eggs until well combined", "cooking, baking, instructions, food"],
    ["Scientists discovered a new species of butterfly in the Amazon", "science, nature, discovery, environment"],
    ["The team scored in the final minute to win the championship", "sports, victory, competition, excitement"],
    ["The painting uses vibrant colors to express deep emotions", "art, emotion, creativity, analysis"],
]

# Custom CSS with modern design
custom_css = """
footer {display:none !important}
.output-markdown{display:none !important}
.gradio-container {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
    max-width: 1200px !important;
}
.gr-button-primary {
    background: linear-gradient(90deg, #11142D, #253885) !important;
    border: none !important;
    color: white !important;
    border-radius: 12px !important;
    transition: all 0.3s ease !important;
}
.gr-button-primary:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 4px 12px rgba(17, 20, 45, 0.3) !important;
    background: linear-gradient(90deg, #253885, #4285F4) !important;
}
.gr-input, .gr-textarea {
    border-radius: 8px !important;
    border: 2px solid #E2E8F0 !important;
    padding: 12px !important;
    font-size: 16px !important;
}
.gr-input:focus, .gr-textarea:focus {
    border-color: #253885 !important;
    box-shadow: 0 0 0 3px rgba(37, 56, 133, 0.2) !important;
}
.gr-panel {
    border-radius: 16px !important;
    box-shadow: 0 4px 15px -1px rgba(0, 0, 0, 0.1) !important;
    background: white !important;
}
.gr-box {
    border-radius: 12px !important;
    background: white !important;
}
.markdown-text {
    font-size: 16px !important;
    line-height: 1.6 !important;
}
.example-text {
    font-family: 'Inter', sans-serif !important;
    color: #11142D !important;
}
"""

# Create the interface
demo = gr.Interface(
    fn=zeroShotClassification,
    inputs=[
        gr.Textbox(
            label="✍️ Input Text",
            placeholder="Enter the text you want to classify...",
            lines=3,
            elem_classes=["example-text"]
        ),
        gr.Textbox(
            label="🏷️ Category Labels",
            placeholder="Enter comma-separated categories (e.g., happy, sad, excited, confused)",
            lines=2,
            elem_classes=["example-text"]
        )
    ],
    outputs=[
        gr.Label(label="πŸ“Š Classification Results"),
        gr.Markdown(label="πŸ“ˆ Detailed Analysis", elem_classes=["markdown-text"])
    ],
    title="πŸ€– Zero-Shot Text Classification with ModernBERT",
    description="""
    Classify any text into categories of your choice with ModernBERT 
    
    **How to use:**
    1. Enter your text in the first box
    2. Add comma-separated category labels in the second box
    3. Click submit to see how your text matches each category
    
    Try the examples below or create your own classifications!
    """,
    examples=examples,
    css=custom_css,
    theme=gr.themes.Soft()
)

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