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()