belyakoff commited on
Commit
f7503c7
·
verified ·
1 Parent(s): 72a0178

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -52
app.py CHANGED
@@ -1,59 +1,44 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- pipe = pipeline("zero-shot-classification",model='MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7')
5
 
6
- with gr.Blocks() as demo:
7
- txt = gr.Textbox('Введите текст', label='Текст для классификации', interactive=True)
8
- with gr.Row():
9
- labels = gr.DataFrame(
10
- headers=['Labels'],
11
- row_count=(2, 'static'),
12
- col_count=(1, 'fixed'),
13
- datatype='str',
14
- interactive=True,
15
- scale=4,
16
- )
17
- submit = gr.Button('Обработать', scale=1)
18
- with gr.Group():
19
- with gr.Row():
20
- checkbox = gr.Checkbox(
21
- label='Множественная положительная классификация',
22
- interactive=True,
23
- info='',
24
- )
25
- dropdown = gr.Dropdown(
26
- label='Number of Labels to predict',
27
- multiselect=False,
28
- value=1,
29
- choices=list(range(1,6),),
30
- interactive=False,
31
- )
32
- result = gr.Label(
33
- label='Результат классификации',
34
- visible=False,
35
- )
36
 
37
- def activate_dropdown(ob):
38
- if not ob:
39
- return gr.Dropdown(
40
- interactive=ob,
41
- value=1,
42
- )
43
- return gr.Dropdown(interactive=ob)
44
 
45
- def submit_btn(text, df, label_no):
46
- output = pipe(
47
- text,
48
- list(df['Labels']),
49
- multi_label=True,
50
- )
51
- return gr.Label(
52
- visible=True,
53
- num_top_classes=int(label_no),
54
- value={i: j for i, j in zip(output['labels'], output['scores'])}
55
- )
56
 
57
- checkbox.change(activate_dropdown, inputs=[checkbox], outputs=[dropdown])
58
- submit.click(submit_btn, inputs=[txt, labels, dropdown], outputs=[result])
59
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ classifier = pipeline("zero-shot-classification", model="MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7")
5
 
6
+ def classify(text, *labels):
7
+ labels = [label for label in labels if label]
8
+ if not text or not labels:
9
+ return []
10
+ result = classifier(text, candidate_labels=labels)
11
+ return list(zip(result['labels'], result['scores']))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ def dynamic_ui(labels):
14
+ inputs = [gr.Textbox(label=f"Class {i+1}", value=label) for i, label in enumerate(labels)]
15
+ return inputs
 
 
 
 
16
 
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("## Zero-Shot Text Classification")
 
 
 
 
 
 
 
 
 
19
 
20
+ text_input = gr.Textbox(label="Text for classification")
21
+
22
+ labels = [""]
23
+ classes_container = gr.Column(dynamic_ui(labels))
24
+
25
+ def add_class():
26
+ labels.append("")
27
+ return dynamic_ui(labels)
28
+
29
+ def remove_class():
30
+ if labels:
31
+ labels.pop()
32
+ return dynamic_ui(labels)
33
+
34
+ add_button = gr.Button("Add Class")
35
+ remove_button = gr.Button("Remove Class")
36
+ add_button.click(add_class, [], classes_container)
37
+ remove_button.click(remove_class, [], classes_container)
38
+
39
+ output = gr.Dataframe(headers=["Class", "Probability"], label="Classification Results")
40
+ button = gr.Button("Classify")
41
+
42
+ button.click(classify, inputs=[text_input] + classes_container.children, outputs=output)
43
+
44
+ demo.launch()