Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,23 @@ nli_classifier = pipeline("text-classification", model="tasksource/ModernBERT-ba
|
|
| 8 |
if False:
|
| 9 |
gr.load("models/answerdotai/ModernBERT-base").launch()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def process_input(text_input, labels_or_premise, mode):
|
| 12 |
if mode == "Zero-Shot Classification":
|
| 13 |
labels = [label.strip() for label in labels_or_premise.split(',')]
|
|
@@ -16,7 +33,6 @@ def process_input(text_input, labels_or_premise, mode):
|
|
| 16 |
return results, ''
|
| 17 |
else: # NLI mode
|
| 18 |
prediction = nli_classifier([{"text": text_input, "text_pair": labels_or_premise}])[0]
|
| 19 |
-
# Force showing all three labels
|
| 20 |
results = {
|
| 21 |
"entailment": prediction.get("score", 0) if prediction.get("label") == "entailment" else 0,
|
| 22 |
"contradiction": prediction.get("score", 0) if prediction.get("label") == "contradiction" else 0,
|
|
@@ -26,13 +42,33 @@ def process_input(text_input, labels_or_premise, mode):
|
|
| 26 |
|
| 27 |
def update_interface(mode):
|
| 28 |
if mode == "Zero-Shot Classification":
|
| 29 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
-
gr.Markdown("
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
mode = gr.Radio(
|
| 37 |
["Zero-Shot Classification", "Natural Language Inference"],
|
| 38 |
label="Select Mode",
|
|
@@ -43,13 +79,15 @@ with gr.Blocks() as demo:
|
|
| 43 |
text_input = gr.Textbox(
|
| 44 |
label="✍️ Input Text",
|
| 45 |
placeholder="Enter your text...",
|
| 46 |
-
lines=3
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
labels_or_premise = gr.Textbox(
|
| 50 |
label="🏷️ Categories",
|
| 51 |
placeholder="Enter comma-separated categories...",
|
| 52 |
-
lines=2
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
submit_btn = gr.Button("Submit")
|
|
@@ -61,26 +99,14 @@ with gr.Blocks() as demo:
|
|
| 61 |
|
| 62 |
with gr.Column(variant="panel") as zero_shot_examples_panel:
|
| 63 |
gr.Examples(
|
| 64 |
-
examples=
|
| 65 |
-
["I need to buy groceries", "shopping, urgent tasks, leisure, philosophy"],
|
| 66 |
-
["The sun is very bright today", "weather, astronomy, complaints, poetry"],
|
| 67 |
-
["I love playing video games", "entertainment, sports, education, business"],
|
| 68 |
-
["The car won't start", "transportation, art, cooking, literature"],
|
| 69 |
-
["She wrote a beautiful poem", "creativity, finance, exercise, technology"]
|
| 70 |
-
],
|
| 71 |
inputs=[text_input, labels_or_premise],
|
| 72 |
label="Zero-Shot Classification Examples"
|
| 73 |
)
|
| 74 |
|
| 75 |
with gr.Column(variant="panel") as nli_examples_panel:
|
| 76 |
gr.Examples(
|
| 77 |
-
examples=
|
| 78 |
-
["A man is sleeping on a couch", "The man is awake"],
|
| 79 |
-
["The restaurant is full of people", "The place is empty"],
|
| 80 |
-
["The child is playing with toys", "The kid is having fun"],
|
| 81 |
-
["It's raining outside", "The weather is wet"],
|
| 82 |
-
["The dog is barking at the mailman", "There is a cat"]
|
| 83 |
-
],
|
| 84 |
inputs=[text_input, labels_or_premise],
|
| 85 |
label="Natural Language Inference Examples"
|
| 86 |
)
|
|
@@ -94,7 +120,7 @@ with gr.Blocks() as demo:
|
|
| 94 |
mode.change(
|
| 95 |
fn=update_interface,
|
| 96 |
inputs=[mode],
|
| 97 |
-
outputs=[labels_or_premise]
|
| 98 |
)
|
| 99 |
|
| 100 |
mode.change(
|
|
|
|
| 8 |
if False:
|
| 9 |
gr.load("models/answerdotai/ModernBERT-base").launch()
|
| 10 |
|
| 11 |
+
# Define examples
|
| 12 |
+
zero_shot_examples = [
|
| 13 |
+
["I absolutely love this product, it's amazing!", "positive, negative, neutral, angry"],
|
| 14 |
+
["I need to buy groceries", "shopping, urgent tasks, leisure, philosophy"],
|
| 15 |
+
["The sun is very bright today", "weather, astronomy, complaints, poetry"],
|
| 16 |
+
["I love playing video games", "entertainment, sports, education, business"],
|
| 17 |
+
["The car won't start", "transportation, art, cooking, literature"]
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
nli_examples = [
|
| 21 |
+
["A man is sleeping on a couch", "The man is awake"],
|
| 22 |
+
["The restaurant is full of people", "The place is empty"],
|
| 23 |
+
["The child is playing with toys", "The kid is having fun"],
|
| 24 |
+
["It's raining outside", "The weather is wet"],
|
| 25 |
+
["The dog is barking at the mailman", "There is a cat"]
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
def process_input(text_input, labels_or_premise, mode):
|
| 29 |
if mode == "Zero-Shot Classification":
|
| 30 |
labels = [label.strip() for label in labels_or_premise.split(',')]
|
|
|
|
| 33 |
return results, ''
|
| 34 |
else: # NLI mode
|
| 35 |
prediction = nli_classifier([{"text": text_input, "text_pair": labels_or_premise}])[0]
|
|
|
|
| 36 |
results = {
|
| 37 |
"entailment": prediction.get("score", 0) if prediction.get("label") == "entailment" else 0,
|
| 38 |
"contradiction": prediction.get("score", 0) if prediction.get("label") == "contradiction" else 0,
|
|
|
|
| 42 |
|
| 43 |
def update_interface(mode):
|
| 44 |
if mode == "Zero-Shot Classification":
|
| 45 |
+
return (
|
| 46 |
+
gr.update(
|
| 47 |
+
label="🏷️ Categories",
|
| 48 |
+
placeholder="Enter comma-separated categories...",
|
| 49 |
+
value=zero_shot_examples[0][1]
|
| 50 |
+
),
|
| 51 |
+
gr.update(value=zero_shot_examples[0][0])
|
| 52 |
+
)
|
| 53 |
else:
|
| 54 |
+
return (
|
| 55 |
+
gr.update(
|
| 56 |
+
label="🔎 Hypothesis",
|
| 57 |
+
placeholder="Enter a hypothesis to compare with the premise...",
|
| 58 |
+
value=nli_examples[0][1]
|
| 59 |
+
),
|
| 60 |
+
gr.update(value=nli_examples[0][0])
|
| 61 |
+
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("""
|
| 65 |
+
# 🤖 ModernBERT Text Analysis, fine-tuned from Answer.ai
|
| 66 |
|
| 67 |
+
Using [tasksource/ModernBERT-base-nli](https://huggingface.co/tasksource/ModernBERT-base-nli),
|
| 68 |
+
fine-tuned from [answerdotai/ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base)
|
| 69 |
+
on large scale tasksource classification tasks. The tuned model achieves high accuracy on reasoning and long-context NLI, outperforming Llama 3 8B on ConTRoL and FOLIO.
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
mode = gr.Radio(
|
| 73 |
["Zero-Shot Classification", "Natural Language Inference"],
|
| 74 |
label="Select Mode",
|
|
|
|
| 79 |
text_input = gr.Textbox(
|
| 80 |
label="✍️ Input Text",
|
| 81 |
placeholder="Enter your text...",
|
| 82 |
+
lines=3,
|
| 83 |
+
value=zero_shot_examples[0][0] # Initial value
|
| 84 |
)
|
| 85 |
|
| 86 |
labels_or_premise = gr.Textbox(
|
| 87 |
label="🏷️ Categories",
|
| 88 |
placeholder="Enter comma-separated categories...",
|
| 89 |
+
lines=2,
|
| 90 |
+
value=zero_shot_examples[0][1] # Initial value
|
| 91 |
)
|
| 92 |
|
| 93 |
submit_btn = gr.Button("Submit")
|
|
|
|
| 99 |
|
| 100 |
with gr.Column(variant="panel") as zero_shot_examples_panel:
|
| 101 |
gr.Examples(
|
| 102 |
+
examples=zero_shot_examples,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
inputs=[text_input, labels_or_premise],
|
| 104 |
label="Zero-Shot Classification Examples"
|
| 105 |
)
|
| 106 |
|
| 107 |
with gr.Column(variant="panel") as nli_examples_panel:
|
| 108 |
gr.Examples(
|
| 109 |
+
examples=nli_examples,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
inputs=[text_input, labels_or_premise],
|
| 111 |
label="Natural Language Inference Examples"
|
| 112 |
)
|
|
|
|
| 120 |
mode.change(
|
| 121 |
fn=update_interface,
|
| 122 |
inputs=[mode],
|
| 123 |
+
outputs=[labels_or_premise, text_input]
|
| 124 |
)
|
| 125 |
|
| 126 |
mode.change(
|