Update app.py
Browse files
app.py
CHANGED
@@ -7,21 +7,21 @@ nli_classifier = pipeline("text-classification", model="tasksource/ModernBERT-ba
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
with gr.Blocks() as demo:
|
26 |
gr.Markdown("# 🤖 ModernBERT Text Analysis")
|
27 |
|
@@ -39,8 +39,8 @@ with gr.Blocks() as demo:
|
|
39 |
)
|
40 |
|
41 |
labels_or_premise = gr.Textbox(
|
42 |
-
label="🏷️ Categories
|
43 |
-
placeholder="Enter comma-separated categories
|
44 |
lines=2
|
45 |
)
|
46 |
|
@@ -51,38 +51,48 @@ with gr.Blocks() as demo:
|
|
51 |
gr.Markdown(label="📈 Analysis", visible=False)
|
52 |
]
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
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 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
def
|
77 |
-
return
|
78 |
-
|
79 |
-
|
80 |
-
)
|
81 |
|
82 |
mode.change(
|
83 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
inputs=[mode],
|
85 |
-
outputs=[
|
86 |
)
|
87 |
|
88 |
submit_btn.click(
|
@@ -91,6 +101,5 @@ with gr.Blocks() as demo:
|
|
91 |
outputs=outputs
|
92 |
)
|
93 |
|
94 |
-
# Launch the app
|
95 |
if __name__ == "__main__":
|
96 |
demo.launch()
|
|
|
7 |
|
8 |
def process_input(text_input, labels_or_premise, mode):
|
9 |
if mode == "Zero-Shot Classification":
|
|
|
10 |
labels = [label.strip() for label in labels_or_premise.split(',')]
|
|
|
|
|
11 |
prediction = zero_shot_classifier(text_input, labels)
|
12 |
results = {label: score for label, score in zip(prediction['labels'], prediction['scores'])}
|
13 |
return results, ''
|
|
|
14 |
else: # NLI mode
|
|
|
15 |
prediction = nli_classifier([{"text": text_input, "text_pair": labels_or_premise}])
|
16 |
results = {pred['label']: pred['score'] for pred in prediction}
|
17 |
return results, ''
|
18 |
|
19 |
+
def update_interface(mode):
|
20 |
+
if mode == "Zero-Shot Classification":
|
21 |
+
return gr.update(label="🏷️ Categories", placeholder="Enter comma-separated categories...")
|
22 |
+
else:
|
23 |
+
return gr.update(label="Hypothesis", placeholder="Enter a hypothesis to compare with the premise...")
|
24 |
+
|
25 |
with gr.Blocks() as demo:
|
26 |
gr.Markdown("# 🤖 ModernBERT Text Analysis")
|
27 |
|
|
|
39 |
)
|
40 |
|
41 |
labels_or_premise = gr.Textbox(
|
42 |
+
label="🏷️ Categories",
|
43 |
+
placeholder="Enter comma-separated categories...",
|
44 |
lines=2
|
45 |
)
|
46 |
|
|
|
51 |
gr.Markdown(label="📈 Analysis", visible=False)
|
52 |
]
|
53 |
|
54 |
+
with gr.Column(variant="panel") as zero_shot_examples_panel:
|
55 |
+
gr.Examples(
|
56 |
+
examples=[
|
57 |
+
["I need to buy groceries", "shopping, urgent tasks, leisure, philosophy"],
|
58 |
+
["The sun is very bright today", "weather, astronomy, complaints, poetry"],
|
59 |
+
["I love playing video games", "entertainment, sports, education, business"],
|
60 |
+
["The car won't start", "transportation, art, cooking, literature"],
|
61 |
+
["She wrote a beautiful poem", "creativity, finance, exercise, technology"]
|
62 |
+
],
|
63 |
+
inputs=[text_input, labels_or_premise],
|
64 |
+
label="Zero-Shot Classification Examples"
|
65 |
+
)
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
with gr.Column(variant="panel") as nli_examples_panel:
|
68 |
+
gr.Examples(
|
69 |
+
examples=[
|
70 |
+
["A man is sleeping on a couch", "The man is awake"],
|
71 |
+
["The restaurant is full of people", "The place is empty"],
|
72 |
+
["The child is playing with toys", "The kid is having fun"],
|
73 |
+
["It's raining outside", "The weather is wet"],
|
74 |
+
["The dog is barking at the mailman", "There is a cat"]
|
75 |
+
],
|
76 |
+
inputs=[text_input, labels_or_premise],
|
77 |
+
label="Natural Language Inference Examples"
|
78 |
+
)
|
79 |
|
80 |
+
def update_visibility(mode):
|
81 |
+
return (
|
82 |
+
gr.update(visible=(mode == "Zero-Shot Classification")),
|
83 |
+
gr.update(visible=(mode == "Natural Language Inference"))
|
84 |
+
)
|
85 |
|
86 |
mode.change(
|
87 |
+
fn=update_interface,
|
88 |
+
inputs=[mode],
|
89 |
+
outputs=[labels_or_premise]
|
90 |
+
)
|
91 |
+
|
92 |
+
mode.change(
|
93 |
+
fn=update_visibility,
|
94 |
inputs=[mode],
|
95 |
+
outputs=[zero_shot_examples_panel, nli_examples_panel]
|
96 |
)
|
97 |
|
98 |
submit_btn.click(
|
|
|
101 |
outputs=outputs
|
102 |
)
|
103 |
|
|
|
104 |
if __name__ == "__main__":
|
105 |
demo.launch()
|