File size: 2,797 Bytes
d8f4336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import gradio as gr
from bert.model import BERT_predict
from lstm.model import BiLSTM_predict
from deberta.model import deBERTa_predict

examples=[
            "How do I train my dogs to kill raccoons?",
            "Why Indian education totally bullshit?",
            "What is it really like to be a nurse practitioner",
            "What is your prefer race? white, black or asian."
        ]

def alert(prob):
    raise gr.Error(f"This may not suitable for posting insincere probability {prob:.2f}")

def clear_post():
    return ""
    
def add_post(posts, new_post, mode):
    if mode == "BiLSTM":
        prob = BiLSTM_predict(new_post)
        insincere = 1 if prob > 0.35 else 0
        if insincere:
            alert(prob)
            return posts, ""
        else:
            return posts + [{"post": new_post, "model": "BiLSTM", "prob": prob}], ""
    elif mode == "BERT":
        insincere, prob = BERT_predict(new_post)
        if insincere:
            alert(prob)
            return posts, ""
        else:
            return posts + [{"post": new_post, "model": "BERT", "prob": prob}], ""
    elif mode == "DeBERTaV3":
        insincere, prob = deBERTa_predict(new_post)
        if insincere:
            alert(prob)
            return posts, ""
        else:
            return posts + [{"post": new_post, "model": "DeBERTaV3", "prob": prob}], ""


with gr.Blocks(theme=gr.themes.Soft(), title="Quara Question post") as demo:

    posts = gr.State([])

    new_post = gr.Textbox(label="Add post", autofocus=True)
    mode = gr.Radio(["BiLSTM", "BERT", "DeBERTaV3"], value="BiLSTM", label="Model")

    with gr.Row():
        submit = gr.Button("submit", variant='primary')
        clear = gr.Button("clear")

    submit.click(add_post, inputs=[posts, new_post, mode], outputs=[posts, new_post])
    clear.click(clear_post, inputs=None, outputs=new_post)
    @gr.render(inputs=posts)
    def render_posts(post_list):
        output = [post for post in post_list]
        gr.Markdown(f"### Question post ({len(output)})")
        for index, post in enumerate(output):
            with gr.Row():
                gr.Textbox(
                    f"{post['post']} | {post['prob']:.8f}", 
                    label=f"Post{index + 1} ({post['model']})", 
                    show_label=True
                )
                delete_btn = gr.Button("Delete", scale=0, variant="stop")
                
                def delete(post=post):
                    post_list.remove(post)
                    return post_list
                delete_btn.click(delete, None, [posts])
    
    with gr.Row():
        examples = gr.Examples(
        examples=examples,
        inputs=[new_post],
    )

demo.launch()