Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,86 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
# Initialize the
|
5 |
-
|
|
|
6 |
|
7 |
-
def
|
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 |
-
placeholder="Enter comma-separated categories (e.g., happy, sad, excited, confused)",
|
33 |
-
lines=2,
|
34 |
-
elem_classes=["example-text"]
|
35 |
)
|
36 |
-
|
37 |
-
outputs=
|
38 |
-
gr.Label(label="📊 Classification Results"),
|
39 |
-
gr.Markdown(label="📈 Detailed Analysis", elem_classes=["markdown-text"])
|
40 |
-
],
|
41 |
-
title="🤖 Zero-Shot Text Classification with ModernBERT",
|
42 |
-
description="""
|
43 |
-
Classify any text into arbirary categories or perform natural language inference with ModernBERT
|
44 |
-
|
45 |
-
**How to use:**
|
46 |
-
1. Enter your text in the first box
|
47 |
-
2. Add comma-separated category labels in the second box
|
48 |
-
3. Click submit to see how your text matches each category
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
["This new phone has an amazing camera and great battery life", "technology, photography, consumer, review"],
|
56 |
-
["Mix flour, sugar, and eggs until well combined", "cooking, baking, instructions, food"],
|
57 |
-
["Scientists discovered a new species of butterfly in the Amazon", "science, nature, discovery, environment"],
|
58 |
-
["The team scored in the final minute to win the championship", "sports, victory, competition, excitement"],
|
59 |
-
["The painting uses vibrant colors to express deep emotions", "art, emotion, creativity, analysis"]
|
60 |
-
],
|
61 |
-
cache_examples=False,
|
62 |
-
css="""
|
63 |
-
footer {display:none !important}
|
64 |
-
.output-markdown{display:none !important}
|
65 |
-
.gradio-container {
|
66 |
-
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
|
67 |
-
max-width: 1200px !important;
|
68 |
-
}
|
69 |
-
.gr-button-primary {
|
70 |
-
background: linear-gradient(90deg, #11142D, #253885) !important;
|
71 |
-
border: none !important;
|
72 |
-
color: white !important;
|
73 |
-
border-radius: 12px !important;
|
74 |
-
transition: all 0.3s ease !important;
|
75 |
-
}
|
76 |
-
.gr-button-primary:hover {
|
77 |
-
transform: translateY(-2px) !important;
|
78 |
-
box-shadow: 0 4px 12px rgba(17, 20, 45, 0.3) !important;
|
79 |
-
background: linear-gradient(90deg, #253885, #4285F4) !important;
|
80 |
-
}
|
81 |
-
.gr-input, .gr-textarea {
|
82 |
-
border-radius: 8px !important;
|
83 |
-
border: 2px solid #E2E8F0 !important;
|
84 |
-
padding: 12px !important;
|
85 |
-
font-size: 16px !important;
|
86 |
-
}
|
87 |
-
.gr-input:focus, .gr-textarea:focus {
|
88 |
-
border-color: #253885 !important;
|
89 |
-
box-shadow: 0 0 0 3px rgba(37, 56, 133, 0.2) !important;
|
90 |
-
}
|
91 |
-
.gr-panel {
|
92 |
-
border-radius: 16px !important;
|
93 |
-
box-shadow: 0 4px 15px -1px rgba(0, 0, 0, 0.1) !important;
|
94 |
-
background: white !important;
|
95 |
-
}
|
96 |
-
.gr-box {
|
97 |
-
border-radius: 12px !important;
|
98 |
-
background: white !important;
|
99 |
-
}
|
100 |
-
.markdown-text {
|
101 |
-
font-size: 16px !important;
|
102 |
-
line-height: 1.6 !important;
|
103 |
-
}
|
104 |
-
.example-text {
|
105 |
-
font-family: 'Inter', sans-serif !important;
|
106 |
-
color: #11142D !important;
|
107 |
-
}
|
108 |
-
"""
|
109 |
-
)
|
110 |
|
111 |
# Launch the app
|
112 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Initialize the classifiers
|
5 |
+
zero_shot_classifier = pipeline("zero-shot-classification", model="tasksource/ModernBERT-base-nli")
|
6 |
+
nli_classifier = pipeline("text-classification", model="tasksource/ModernBERT-base-nli")
|
7 |
|
8 |
+
def process_input(text_input, labels_or_premise, mode):
|
9 |
+
if mode == "Zero-Shot Classification":
|
10 |
+
# Clean and process the labels
|
11 |
+
labels = [label.strip() for label in labels_or_premise.split(',')]
|
12 |
+
|
13 |
+
# Get predictions
|
14 |
+
prediction = zero_shot_classifier(text_input, labels)
|
15 |
+
results = {label: score for label, score in zip(prediction['labels'], prediction['scores'])}
|
16 |
+
return results, ''
|
17 |
|
18 |
+
else: # NLI mode
|
19 |
+
# Process as premise-hypothesis pair
|
20 |
+
prediction = nli_classifier([{"text": text_input, "text_pair": labels_or_premise}])
|
21 |
+
results = {pred['label']: pred['score'] for pred in prediction}
|
22 |
+
return results, ''
|
23 |
+
|
24 |
+
# Create the interface
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
gr.Markdown("# 🤖 ModernBERT Text Analysis")
|
27 |
|
28 |
+
mode = gr.Radio(
|
29 |
+
["Zero-Shot Classification", "Natural Language Inference"],
|
30 |
+
label="Select Mode",
|
31 |
+
value="Zero-Shot Classification"
|
32 |
+
)
|
33 |
|
34 |
+
with gr.Column():
|
35 |
+
text_input = gr.Textbox(
|
36 |
+
label="✍️ Input Text",
|
37 |
+
placeholder="Enter your text...",
|
38 |
+
lines=3
|
39 |
+
)
|
40 |
+
|
41 |
+
labels_or_premise = gr.Textbox(
|
42 |
+
label="🏷️ Categories / Premise",
|
43 |
+
placeholder="Enter comma-separated categories or premise text...",
|
44 |
+
lines=2
|
45 |
+
)
|
46 |
+
|
47 |
+
submit_btn = gr.Button("Submit")
|
48 |
+
|
49 |
+
outputs = [
|
50 |
+
gr.Label(label="📊 Results"),
|
51 |
+
gr.Markdown(label="📈 Analysis", visible=False)
|
52 |
+
]
|
53 |
|
54 |
+
# Different examples for each mode
|
55 |
+
zero_shot_examples = [
|
56 |
+
["I need to buy groceries", "shopping, urgent tasks, leisure, philosophy"],
|
57 |
+
["The sun is very bright today", "weather, astronomy, complaints, poetry"],
|
58 |
+
["I love playing video games", "entertainment, sports, education, business"],
|
59 |
+
["The car won't start", "transportation, art, cooking, literature"],
|
60 |
+
["She wrote a beautiful poem", "creativity, finance, exercise, technology"]
|
61 |
+
]
|
62 |
|
63 |
+
nli_examples = [
|
64 |
+
["A man is sleeping on a couch", "The man is awake"],
|
65 |
+
["The restaurant is full of people", "The place is empty"],
|
66 |
+
["The child is playing with toys", "The kid is having fun"],
|
67 |
+
["It's raining outside", "The weather is wet"],
|
68 |
+
["The dog is barking at the mailman", "There is a cat"]
|
69 |
+
]
|
70 |
+
|
71 |
+
def update_examples(mode_value):
|
72 |
+
return gr.Examples(
|
73 |
+
zero_shot_examples if mode_value == "Zero-Shot Classification" else nli_examples,
|
74 |
+
inputs=[text_input, labels_or_premise]
|
|
|
|
|
|
|
75 |
)
|
76 |
+
|
77 |
+
mode.change(fn=update_examples, inputs=[mode], outputs=gr.Examples())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
submit_btn.click(
|
80 |
+
fn=process_input,
|
81 |
+
inputs=[text_input, labels_or_premise, mode],
|
82 |
+
outputs=outputs
|
83 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# Launch the app
|
86 |
if __name__ == "__main__":
|