Spaces:
Runtime error
Runtime error
Commit ·
3c1bc1b
1
Parent(s): dac49ea
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from transformers import DistilBertTokenizer
|
| 3 |
+
from transformers import DistilBertForSequenceClassification
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
MODEL_PATH = "RedmarkerAI/hrw_multi_generous_v1"
|
| 8 |
+
auth_token = os.environ.get("TOKEN_FROM_SECRET")
|
| 9 |
+
dataset_token = os.environ.get("DATASET_TOKEN")
|
| 10 |
+
hf_writer = gr.HuggingFaceDatasetSaver(dataset_token, "hrw_test_multiclass_flagged_data")
|
| 11 |
+
model = DistilBertForSequenceClassification.from_pretrained(MODEL_PATH, use_auth_token=auth_token)
|
| 12 |
+
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
|
| 13 |
+
|
| 14 |
+
clf = pipeline("text-classification", model=model.to("cpu"), tokenizer=tokenizer)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def clf_result(text_input: str) -> str:
|
| 18 |
+
|
| 19 |
+
if "best" not in text_input.lower():
|
| 20 |
+
res = "Please enter a sentence with the word `best`"
|
| 21 |
+
return res
|
| 22 |
+
model_res = clf(text_input)[0]
|
| 23 |
+
# label_map = {"LABEL_0": "NOT RISKY", "LABEL_1": "RISKY"}
|
| 24 |
+
label_map = {
|
| 25 |
+
"0": "Not Risky",
|
| 26 |
+
"1": "Possibly Risky",
|
| 27 |
+
"2": "Risky"
|
| 28 |
+
}
|
| 29 |
+
label_res = label_map.get(model_res["label"])
|
| 30 |
+
score = model_res["score"]
|
| 31 |
+
res = f"Result: {label_res}\n\nScore: {score}"
|
| 32 |
+
return res
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=clf_result,
|
| 37 |
+
title="Test High Risk Words Multiclass model",
|
| 38 |
+
examples=["All the best lenders and rates for car loans in one AI powered marketplace", "Caregiver burnout can happen to your best employees."],
|
| 39 |
+
description="DistilBert for text classification model fine tuned on 70% of annotated RM production data combined with industry-specific webscrape data",
|
| 40 |
+
inputs=gr.Textbox(placeholder="Enter sentence containing the word `best` here and press Submit", label="Sentence to check"),
|
| 41 |
+
outputs="textbox",
|
| 42 |
+
allow_flagging="manual",
|
| 43 |
+
flagging_options=["wrong result :(", "correct result :)", "inconsistent result", "debatable input", "other"],
|
| 44 |
+
flagging_callback=hf_writer,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|