sileod's picture
Update app.py
a51d9c2 verified
raw
history blame
8.24 kB
import gradio as gr
from transformers import pipeline
import nltk
def sent_tokenize(text):
# Regular expression to split sentences
sentence_endings = re.compile(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|!)(\s|$)')
sentences = sentence_endings.split(text)
return [s.strip() for s in sentences if s.strip()]
# Initialize the classifiers
zero_shot_classifier = pipeline("zero-shot-classification", model="tasksource/ModernBERT-base-nli")
nli_classifier = pipeline("text-classification", model="tasksource/ModernBERT-base-nli")
# Define examples (including new long context example)
zero_shot_examples = [
["I absolutely love this product, it's amazing!", "positive, negative, neutral"],
["I need to buy groceries", "shopping, urgent tasks, leisure, philosophy"],
["The sun is very bright today", "weather, astronomy, complaints, poetry"],
["I love playing video games", "entertainment, sports, education, business"],
["The car won't start", "transportation, art, cooking, literature"]
]
nli_examples = [
["A man is sleeping on a couch", "The man is awake"],
["The restaurant's waiting area is bustling, but several tables remain vacant", "The establishment is at maximum capacity"],
["The child is methodically arranging blocks while frowning in concentration", "The kid is experiencing joy"],
["Dark clouds are gathering and the pavement shows scattered wet spots", "It's been raining heavily all day"],
["A German Shepherd is exhibiting defensive behavior towards someone approaching the property", "The animal making noise is feline"]
]
long_context_examples = [
["""The small cafe on the corner has been bustling with activity all morning. The aroma of freshly baked pastries wafts through the air, drawing in passersby. The baristas work efficiently behind the counter, crafting intricate latte art. Several customers are seated at wooden tables, engaged in quiet conversations or working on laptops. Through the large windows, sunshine streams in, creating a warm and inviting atmosphere.""",
"The cafe is experiencing a slow, quiet morning"]
]
def process_input(text_input, labels_or_premise, mode):
if mode == "Zero-Shot Classification":
labels = [label.strip() for label in labels_or_premise.split(',')]
prediction = zero_shot_classifier(text_input, labels)
results = {label: score for label, score in zip(prediction['labels'], prediction['scores'])}
return results, ''
elif mode == "Natural Language Inference":
pred = nli_classifier([{"text": text_input, "text_pair": labels_or_premise}], return_all_scores=True)[0]
results = {pred['label']: pred['score'] for pred in pred}
return results, ''
else: # Long Context NLI
# Global prediction
global_pred = nli_classifier([{"text": text_input, "text_pair": labels_or_premise}], return_all_scores=True)[0]
global_results = {pred['label']: pred['score'] for pred in global_pred}
# Sentence-level analysis
sentences = sent_tokenize(text_input)
sentence_results = []
for sentence in sentences:
sent_pred = nli_classifier([{"text": sentence, "text_pair": labels_or_premise}], return_all_scores=True)[0]
sent_scores = {pred['label']: pred['score'] for pred in sent_pred}
max_label = max(sent_scores.items(), key=lambda x: x[1])[0]
sentence_results.append({
'sentence': sentence,
'prediction': max_label,
'scores': sent_scores
})
# Create markdown analysis
analysis_md = "## Global Prediction\n"
max_global_label = max(global_results.items(), key=lambda x: x[1])[0]
analysis_md += f"Overall prediction: **{max_global_label}**\n\n"
analysis_md += "## Sentence-Level Analysis\n"
for i, result in enumerate(sentence_results, 1):
analysis_md += f"\n### Sentence {i}\n"
analysis_md += f"*{result['sentence']}*\n"
analysis_md += f"Prediction: **{result['prediction']}**\n"
scores_str = ", ".join([f"{label}: {score:.2f}" for label, score in result['scores'].items()])
analysis_md += f"Scores: {scores_str}\n"
return global_results, analysis_md
def update_interface(mode):
if mode == "Zero-Shot Classification":
return (
gr.update(
label="🏷️ Categories",
placeholder="Enter comma-separated categories...",
value=zero_shot_examples[0][1]
),
gr.update(value=zero_shot_examples[0][0])
)
elif mode == "Natural Language Inference":
return (
gr.update(
label="πŸ”Ž Hypothesis",
placeholder="Enter a hypothesis to compare with the premise...",
value=nli_examples[0][1]
),
gr.update(value=nli_examples[0][0])
)
else: # Long Context NLI
return (
gr.update(
label="πŸ”Ž Global Hypothesis",
placeholder="Enter a hypothesis to test against the full context...",
value=long_context_examples[0][1]
),
gr.update(value=long_context_examples[0][0])
)
with gr.Blocks() as demo:
gr.Markdown("""
# tasksource/ModernBERT-nli demonstration
This space uses [tasksource/ModernBERT-base-nli](https://huggingface.co/tasksource/ModernBERT-base-nli),
fine-tuned from [answerdotai/ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base)
on tasksource classification tasks.
This NLI model achieves high accuracy on logical reasoning and long-context NLI, outperforming Llama 3 8B on ConTRoL and FOLIO.
""")
mode = gr.Radio(
["Zero-Shot Classification", "Natural Language Inference", "Long Context NLI"],
label="Select Mode",
value="Zero-Shot Classification"
)
with gr.Column():
text_input = gr.Textbox(
label="✍️ Input Text",
placeholder="Enter your text...",
lines=3,
value=zero_shot_examples[0][0]
)
labels_or_premise = gr.Textbox(
label="🏷️ Categories",
placeholder="Enter comma-separated categories...",
lines=2,
value=zero_shot_examples[0][1]
)
submit_btn = gr.Button("Submit")
outputs = [
gr.Label(label="πŸ“Š Results"),
gr.Markdown(label="πŸ“ˆ Sentence Analysis", visible=True)
]
with gr.Column(variant="panel") as zero_shot_examples_panel:
gr.Examples(
examples=zero_shot_examples,
inputs=[text_input, labels_or_premise],
label="Zero-Shot Classification Examples",
)
with gr.Column(variant="panel") as nli_examples_panel:
gr.Examples(
examples=nli_examples,
inputs=[text_input, labels_or_premise],
label="Natural Language Inference Examples",
)
with gr.Column(variant="panel") as long_context_examples_panel:
gr.Examples(
examples=long_context_examples,
inputs=[text_input, labels_or_premise],
label="Long Context NLI Examples",
)
def update_visibility(mode):
return (
gr.update(visible=(mode == "Zero-Shot Classification")),
gr.update(visible=(mode == "Natural Language Inference")),
gr.update(visible=(mode == "Long Context NLI"))
)
mode.change(
fn=update_interface,
inputs=[mode],
outputs=[labels_or_premise, text_input]
)
mode.change(
fn=update_visibility,
inputs=[mode],
outputs=[zero_shot_examples_panel, nli_examples_panel, long_context_examples_panel]
)
submit_btn.click(
fn=process_input,
inputs=[text_input, labels_or_premise, mode],
outputs=outputs
)
if __name__ == "__main__":
demo.launch()