File size: 1,852 Bytes
3c1bc1b
 
 
 
 
ca9da5c
 
 
3c1bc1b
 
570b959
 
b7206e6
3c1bc1b
 
 
 
 
 
 
 
 
 
 
2aa9837
b59be81
2aa9837
97de52d
3c1bc1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from transformers import DistilBertTokenizer
from transformers import DistilBertForSequenceClassification
from transformers import pipeline
import gradio as gr
import logging

logger = logging.getLogger(__name__)

MODEL_PATH = "RedmarkerAI/hrw_multi_generous_v1"
auth_token = os.environ.get("TOKEN_MODEL")
dataset_token = os.environ.get("TOKEN_DATASET")
hf_writer = gr.HuggingFaceDatasetSaver(dataset_token, "RedmarkerAI/hrw_test_multiclass_flagged_data")
model = DistilBertForSequenceClassification.from_pretrained(MODEL_PATH, use_auth_token=auth_token)
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")

clf = pipeline("text-classification", model=model.to("cpu"), tokenizer=tokenizer)


def clf_result(text_input: str) -> str:

    if "best" not in text_input.lower():
        res = "Please enter a sentence with the word `best`"
        return res
    model_res = clf(text_input)
    print(f"{model_res = }")
    model_res = model_res[0]
    label_res = model_res["label"]
    score = model_res["score"]
    res = f"Result: {label_res}\n\nScore: {score}"
    return res


demo = gr.Interface(
    fn=clf_result,
    title="Test High Risk Words Multiclass model",
    examples=["All the best lenders and rates for car loans in one AI powered marketplace", "Caregiver burnout can happen to your best employees."],
    description="DistilBert for text classification model fine tuned on 70% of annotated RM production data combined with industry-specific webscrape data",
    inputs=gr.Textbox(placeholder="Enter sentence containing the word `best` here and press Submit", label="Sentence to check"),
    outputs="textbox",
    allow_flagging="manual",
    flagging_options=["wrong result :(", "correct result :)", "inconsistent result", "debatable input", "other"],
    flagging_callback=hf_writer,
)

demo.launch()