File size: 1,516 Bytes
643b330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
from transformers import pipeline

pipe = pipeline("zero-shot-classification",model='MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7')

with gr.Blocks() as demo:
    txt = gr.Textbox('Input Text', label='Text to classify', interactive=True)
    with gr.Row():
        labels = gr.DataFrame(headers=['Labels'], row_count=(2, 'dynamic'), col_count=(1, 'fixed'),
                              datatype='str', interactive=True, scale=4)
        submit = gr.Button('Submit', scale=1)
    with gr.Group():
        with gr.Row():
            checkbox = gr.Checkbox(label='Multi-Label Classification', interactive=True, info='Showing the score for more than one label')
            dropdown = gr.Dropdown(label='Number of Labels to predict', multiselect=False, value=1, choices=list(range(1,6)),
                        interactive=False)
    result = gr.Label(label='Classification Result', visible=False)

    def activate_dropdown(ob):
        if not ob:
            return gr.Dropdown(interactive=ob, value=1)
        return gr.Dropdown(interactive=ob)

    def submit_btn(text, df, label_no):
        output = pipe(text, list(df['Labels']), multi_label=True)
        return gr.Label(visible=True, num_top_classes=int(label_no),
                        value={i: j for i, j in zip(output['labels'], output['scores'])})

    checkbox.change(activate_dropdown, inputs=[checkbox], outputs=[dropdown])
    submit.click(submit_btn, inputs=[txt, labels, dropdown], outputs=[result])
demo.launch()