sileod commited on
Commit
4860931
·
verified ·
1 Parent(s): bf8c75c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -32,9 +32,8 @@ def process_input(text_input, labels_or_premise, mode):
32
  results = {label: score for label, score in zip(prediction['labels'], prediction['scores'])}
33
  return results, ''
34
  else: # NLI mode
35
- pred= nli_classifier([{"text": text_input, "text_pair": labels_or_premise}],return_all_scores=True)[0]
36
- results= {pred['label']:pred['score'] for pred in pred}
37
-
38
  return results, ''
39
 
40
  def update_interface(mode):
@@ -43,20 +42,25 @@ def update_interface(mode):
43
  gr.update(
44
  label="🏷️ Categories",
45
  placeholder="Enter comma-separated categories...",
46
- value=zero_shot_examples[0][1]
47
  ),
48
- gr.update(value=zero_shot_examples[0][0])
49
  )
50
  else:
51
  return (
52
  gr.update(
53
  label="🔎 Hypothesis",
54
  placeholder="Enter a hypothesis to compare with the premise...",
55
- value=nli_examples[0][1]
56
  ),
57
- gr.update(value=nli_examples[0][0])
58
  )
59
 
 
 
 
 
 
 
 
60
  with gr.Blocks() as demo:
61
  gr.Markdown("""
62
  # tasksource/ModernBERT-nli demonstration
@@ -78,14 +82,14 @@ with gr.Blocks() as demo:
78
  label="✍️ Input Text",
79
  placeholder="Enter your text...",
80
  lines=3,
81
- value=zero_shot_examples[0][0] # Initial value
82
  )
83
 
84
  labels_or_premise = gr.Textbox(
85
  label="🏷️ Categories",
86
  placeholder="Enter comma-separated categories...",
87
  lines=2,
88
- value=zero_shot_examples[0][1] # Initial value
89
  )
90
 
91
  submit_btn = gr.Button("Submit")
@@ -96,18 +100,18 @@ with gr.Blocks() as demo:
96
  ]
97
 
98
  with gr.Column(variant="panel") as zero_shot_examples_panel:
99
- gr.Examples(
100
  examples=zero_shot_examples,
101
  inputs=[text_input, labels_or_premise],
102
  label="Zero-Shot Classification Examples",
103
  )
104
 
105
  with gr.Column(variant="panel") as nli_examples_panel:
106
- gr.Examples(
107
  examples=nli_examples,
108
  inputs=[text_input, labels_or_premise],
109
  label="Natural Language Inference Examples",
110
- )
111
 
112
  def update_visibility(mode):
113
  return (
@@ -115,6 +119,7 @@ with gr.Blocks() as demo:
115
  gr.update(visible=(mode == "Natural Language Inference"))
116
  )
117
 
 
118
  mode.change(
119
  fn=update_interface,
120
  inputs=[mode],
@@ -127,6 +132,19 @@ with gr.Blocks() as demo:
127
  outputs=[zero_shot_examples_panel, nli_examples_panel]
128
  )
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  submit_btn.click(
131
  fn=process_input,
132
  inputs=[text_input, labels_or_premise, mode],
 
32
  results = {label: score for label, score in zip(prediction['labels'], prediction['scores'])}
33
  return results, ''
34
  else: # NLI mode
35
+ pred = nli_classifier([{"text": text_input, "text_pair": labels_or_premise}], return_all_scores=True)[0]
36
+ results = {pred['label']: pred['score'] for pred in pred}
 
37
  return results, ''
38
 
39
  def update_interface(mode):
 
42
  gr.update(
43
  label="🏷️ Categories",
44
  placeholder="Enter comma-separated categories...",
 
45
  ),
46
+ gr.update()
47
  )
48
  else:
49
  return (
50
  gr.update(
51
  label="🔎 Hypothesis",
52
  placeholder="Enter a hypothesis to compare with the premise...",
 
53
  ),
54
+ gr.update()
55
  )
56
 
57
+ def handle_example_selection(evt: gr.SelectData, mode):
58
+ # Return the appropriate label based on the current mode
59
+ if mode == "Zero-Shot Classification":
60
+ return gr.update(label="🏷️ Categories")
61
+ else:
62
+ return gr.update(label="🔎 Hypothesis")
63
+
64
  with gr.Blocks() as demo:
65
  gr.Markdown("""
66
  # tasksource/ModernBERT-nli demonstration
 
82
  label="✍️ Input Text",
83
  placeholder="Enter your text...",
84
  lines=3,
85
+ value=zero_shot_examples[0][0]
86
  )
87
 
88
  labels_or_premise = gr.Textbox(
89
  label="🏷️ Categories",
90
  placeholder="Enter comma-separated categories...",
91
  lines=2,
92
+ value=zero_shot_examples[0][1]
93
  )
94
 
95
  submit_btn = gr.Button("Submit")
 
100
  ]
101
 
102
  with gr.Column(variant="panel") as zero_shot_examples_panel:
103
+ examples_zero = gr.Examples(
104
  examples=zero_shot_examples,
105
  inputs=[text_input, labels_or_premise],
106
  label="Zero-Shot Classification Examples",
107
  )
108
 
109
  with gr.Column(variant="panel") as nli_examples_panel:
110
+ examples_nli = gr.Examples(
111
  examples=nli_examples,
112
  inputs=[text_input, labels_or_premise],
113
  label="Natural Language Inference Examples",
114
+ )
115
 
116
  def update_visibility(mode):
117
  return (
 
119
  gr.update(visible=(mode == "Natural Language Inference"))
120
  )
121
 
122
+ # Update interface on mode change
123
  mode.change(
124
  fn=update_interface,
125
  inputs=[mode],
 
132
  outputs=[zero_shot_examples_panel, nli_examples_panel]
133
  )
134
 
135
+ # Handle example selection events
136
+ examples_zero.select(
137
+ fn=handle_example_selection,
138
+ inputs=[mode],
139
+ outputs=[labels_or_premise]
140
+ )
141
+
142
+ examples_nli.select(
143
+ fn=handle_example_selection,
144
+ inputs=[mode],
145
+ outputs=[labels_or_premise]
146
+ )
147
+
148
  submit_btn.click(
149
  fn=process_input,
150
  inputs=[text_input, labels_or_premise, mode],