Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,127 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
| 4 |
classifier = pipeline("zero-shot-classification", model="tasksource/ModernBERT-base-nli")
|
| 5 |
|
| 6 |
def zeroShotClassification(text_input, candidate_labels):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
footer {display:none !important}
|
| 18 |
.output-markdown{display:none !important}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
.gr-button-primary {
|
| 20 |
-
|
| 21 |
-
height: 43px;
|
| 22 |
-
width: 130px;
|
| 23 |
-
left: 0px;
|
| 24 |
-
top: 0px;
|
| 25 |
-
padding: 0px;
|
| 26 |
-
cursor: pointer !important;
|
| 27 |
-
background: none rgb(17, 20, 45) !important;
|
| 28 |
border: none !important;
|
| 29 |
-
|
| 30 |
-
font-family: Poppins !important;
|
| 31 |
-
font-size: 14px !important;
|
| 32 |
-
font-weight: 500 !important;
|
| 33 |
-
color: rgb(255, 255, 255) !important;
|
| 34 |
-
line-height: 1 !important;
|
| 35 |
border-radius: 12px !important;
|
| 36 |
-
transition:
|
| 37 |
-
box-shadow: none !important;
|
| 38 |
}
|
| 39 |
-
.gr-button-primary:hover{
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
left: 0px;
|
| 44 |
-
top: 0px;
|
| 45 |
-
padding: 0px;
|
| 46 |
-
cursor: pointer !important;
|
| 47 |
-
background: none rgb(66, 133, 244) !important;
|
| 48 |
-
border: none !important;
|
| 49 |
-
text-align: center !important;
|
| 50 |
-
font-family: Poppins !important;
|
| 51 |
-
font-size: 14px !important;
|
| 52 |
-
font-weight: 500 !important;
|
| 53 |
-
color: rgb(255, 255, 255) !important;
|
| 54 |
-
line-height: 1 !important;
|
| 55 |
-
border-radius: 12px !important;
|
| 56 |
-
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
| 57 |
-
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
| 58 |
}
|
| 59 |
-
.
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
-
.
|
| 64 |
-
|
|
|
|
| 65 |
}
|
| 66 |
-
.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
}
|
| 71 |
-
.
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
-
.
|
| 77 |
-
|
| 78 |
-
color:
|
| 79 |
}
|
| 80 |
"""
|
| 81 |
|
| 82 |
-
|
| 83 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Initialize the classifier
|
| 5 |
classifier = pipeline("zero-shot-classification", model="tasksource/ModernBERT-base-nli")
|
| 6 |
|
| 7 |
def zeroShotClassification(text_input, candidate_labels):
|
| 8 |
+
# Clean and process the labels
|
| 9 |
+
labels = [label.strip() for label in candidate_labels.split(',')]
|
| 10 |
+
|
| 11 |
+
# Get predictions
|
| 12 |
+
prediction = classifier(text_input, labels)
|
| 13 |
+
|
| 14 |
+
# Format results as percentage with 2 decimal places
|
| 15 |
+
results = {label: f"{score*100:.2f}%"
|
| 16 |
+
for label, score in zip(prediction['labels'], prediction['scores'])}
|
| 17 |
+
|
| 18 |
+
# Create markdown output for detailed view
|
| 19 |
+
markdown_output = "### Results Breakdown:\n\n"
|
| 20 |
+
for label, score in sorted(results.items(), key=lambda x: float(x[1].rstrip('%')), reverse=True):
|
| 21 |
+
# Create confidence bar using Unicode blocks
|
| 22 |
+
score_num = float(score.rstrip('%'))
|
| 23 |
+
blocks = "█" * int(score_num/5) + "░" * (20 - int(score_num/5))
|
| 24 |
+
markdown_output += f"**{label}**: {blocks} {score}\n\n"
|
| 25 |
+
|
| 26 |
+
return results, markdown_output
|
| 27 |
|
| 28 |
+
# More diverse examples
|
| 29 |
+
examples = [
|
| 30 |
+
["One day I will see the world", "travel, adventure, dreams, future"],
|
| 31 |
+
["The movie had amazing special effects but a weak plot", "entertainment, technology, criticism, story"],
|
| 32 |
+
["This new phone has an amazing camera and great battery life", "technology, photography, consumer, review"],
|
| 33 |
+
["Mix flour, sugar, and eggs until well combined", "cooking, baking, instructions, food"],
|
| 34 |
+
["Scientists discovered a new species of butterfly in the Amazon", "science, nature, discovery, environment"],
|
| 35 |
+
["The team scored in the final minute to win the championship", "sports, victory, competition, excitement"],
|
| 36 |
+
["The painting uses vibrant colors to express deep emotions", "art, emotion, creativity, analysis"],
|
| 37 |
+
]
|
| 38 |
|
| 39 |
+
# Custom CSS with modern design
|
| 40 |
+
custom_css = """
|
| 41 |
footer {display:none !important}
|
| 42 |
.output-markdown{display:none !important}
|
| 43 |
+
.gradio-container {
|
| 44 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
|
| 45 |
+
max-width: 1200px !important;
|
| 46 |
+
}
|
| 47 |
.gr-button-primary {
|
| 48 |
+
background: linear-gradient(90deg, #11142D, #253885) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
border: none !important;
|
| 50 |
+
color: white !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
border-radius: 12px !important;
|
| 52 |
+
transition: all 0.3s ease !important;
|
|
|
|
| 53 |
}
|
| 54 |
+
.gr-button-primary:hover {
|
| 55 |
+
transform: translateY(-2px) !important;
|
| 56 |
+
box-shadow: 0 4px 12px rgba(17, 20, 45, 0.3) !important;
|
| 57 |
+
background: linear-gradient(90deg, #253885, #4285F4) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
+
.gr-input, .gr-textarea {
|
| 60 |
+
border-radius: 8px !important;
|
| 61 |
+
border: 2px solid #E2E8F0 !important;
|
| 62 |
+
padding: 12px !important;
|
| 63 |
+
font-size: 16px !important;
|
| 64 |
}
|
| 65 |
+
.gr-input:focus, .gr-textarea:focus {
|
| 66 |
+
border-color: #253885 !important;
|
| 67 |
+
box-shadow: 0 0 0 3px rgba(37, 56, 133, 0.2) !important;
|
| 68 |
}
|
| 69 |
+
.gr-panel {
|
| 70 |
+
border-radius: 16px !important;
|
| 71 |
+
box-shadow: 0 4px 15px -1px rgba(0, 0, 0, 0.1) !important;
|
| 72 |
+
background: white !important;
|
| 73 |
}
|
| 74 |
+
.gr-box {
|
| 75 |
+
border-radius: 12px !important;
|
| 76 |
+
background: white !important;
|
| 77 |
+
}
|
| 78 |
+
.markdown-text {
|
| 79 |
+
font-size: 16px !important;
|
| 80 |
+
line-height: 1.6 !important;
|
| 81 |
}
|
| 82 |
+
.example-text {
|
| 83 |
+
font-family: 'Inter', sans-serif !important;
|
| 84 |
+
color: #11142D !important;
|
| 85 |
}
|
| 86 |
"""
|
| 87 |
|
| 88 |
+
# Create the interface
|
| 89 |
+
demo = gr.Interface(
|
| 90 |
+
fn=zeroShotClassification,
|
| 91 |
+
inputs=[
|
| 92 |
+
gr.Textbox(
|
| 93 |
+
label="✍️ Input Text",
|
| 94 |
+
placeholder="Enter the text you want to classify...",
|
| 95 |
+
lines=3,
|
| 96 |
+
elem_classes=["example-text"]
|
| 97 |
+
),
|
| 98 |
+
gr.Textbox(
|
| 99 |
+
label="🏷️ Category Labels",
|
| 100 |
+
placeholder="Enter comma-separated categories (e.g., happy, sad, excited, confused)",
|
| 101 |
+
lines=2,
|
| 102 |
+
elem_classes=["example-text"]
|
| 103 |
+
)
|
| 104 |
+
],
|
| 105 |
+
outputs=[
|
| 106 |
+
gr.Label(label="📊 Classification Results"),
|
| 107 |
+
gr.Markdown(label="📈 Detailed Analysis", elem_classes=["markdown-text"])
|
| 108 |
+
],
|
| 109 |
+
title="🤖 Zero-Shot Text Classification with ModernBERT",
|
| 110 |
+
description="""
|
| 111 |
+
Classify any text into categories of your choice with ModernBERT
|
| 112 |
+
|
| 113 |
+
**How to use:**
|
| 114 |
+
1. Enter your text in the first box
|
| 115 |
+
2. Add comma-separated category labels in the second box
|
| 116 |
+
3. Click submit to see how your text matches each category
|
| 117 |
+
|
| 118 |
+
Try the examples below or create your own classifications!
|
| 119 |
+
""",
|
| 120 |
+
examples=examples,
|
| 121 |
+
css=custom_css,
|
| 122 |
+
theme=gr.themes.Soft()
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Launch the app
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
demo.launch()
|