File size: 1,859 Bytes
1cb41a9
597533d
39ef911
1cb41a9
39ef911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import transformers
from transformers import AutoModelForSequenceClassification, AutoTokenizer

def classify_sentence(sent:str):
    toksentence = tokenizer(sent,truncation=True,return_tensors="pt")
    model.eval()
    with torch.no_grad():
        toksentence.to(device)
        output = model(**toksentence)
    
    return F.softmax(output.logits,dim=1).argmax(dim=1)

    
def classify_text(text:str):
    sentences = sent_tokenize(text)
    annotations = np.array(list(map(classify_sentence,sentences)),dtype=object)  
    result = list(zip(sentences,[mapping[val] for val in annotations]))
    return (annotations,result)
    
def classify_text_wrapper(text:str):
    preds,result = classify_text(text)
    n = len(preds)
    non_biased = np.where(preds==0)[0].shape[0]
    biased = np.where(preds==1)[0].shape[0]
    
    return (result,{'bias ratio':biased/n})

examples=[["[Newsoms's] obsession with masks has created an almost hostile environment in our neighborhoods and streets.\n“He won because the Election was Rigged,” Trump wrote, not referring to Biden by name, adding a list of complaints about vote counting"]]

model = AutoModelForSequenceClassification.from_pretrained("tkurtulus/autotrain-rottentomato-2981285985")
tokenizer = AutoTokenizer.from_pretrained("tkurtulus/autotrain-rottentomato-2981285985");
model.eval();

label = gr.outputs.Label(num_top_classes=None,label='')
text_h = gr.outputs.HighlightedText(color_map={'Unbiased':'#9ad1A1','Biased':'#db8a8a'},label='Classification')
inputs = gr.inputs.Textbox(placeholder=None, default="", label=None)

app = gr.Interface(fn=classify_text_wrapper,title='Bias classifier',theme='default',
                    inputs="textbox",layout='unaligned', outputs=[text_h,label], capture_session=True
                    ,examples=examples)

app.launch(inbrowser=True)