File size: 2,125 Bytes
5bf33cf
 
c8951e4
 
 
5bf33cf
 
61efe65
 
5bf33cf
12647f7
35d25ca
12647f7
5bf33cf
 
12647f7
35d25ca
d11819e
12647f7
 
 
 
 
 
 
 
 
5bf33cf
12647f7
 
 
 
 
5bf33cf
 
12647f7
 
35d25ca
12647f7
 
5bf33cf
 
12647f7
5bf33cf
 
 
2e97396
 
5bf33cf
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn as nn
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
from typing import List, Dict, Any

device = 'cuda' if torch.cuda.is_available() else 'cpu'

class Predictor():
    def __init__(self, path="", tokenizer_path='bert-base-uncased'):
        self.model = AutoModelForSequenceClassification.from_pretrained(path, trust_remote_code=True).to(device)
        self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)

    def preprocess(self, inputs: List[str]):
        MAX_LENGHT = 15
        tokens_unseen = self.tokenizer.batch_encode_plus(
            inputs,
            max_length = MAX_LENGHT,
            pad_to_max_length=True,
            truncation=True
        )
        
        unseen_seq = torch.tensor(tokens_unseen['input_ids'])
        unseen_mask = torch.tensor(tokens_unseen['attention_mask'])
        
        return unseen_seq, unseen_mask

    def postprocess(self, preds):
        preds = np.argmax(preds, axis = 1)
        prediction_label = "This is fake news" if preds[0] == 1 else "This is fake news"
        # print(f"Đây là {prediction_label} new.")
        return prediction_label

    def predict(self, inputs: str):
        unseen_seq, unseen_mask = self.preprocess([inputs])
        with torch.no_grad():
            preds = self.model(unseen_seq, unseen_mask)
            preds = preds.detach().cpu().numpy()
        return self.postprocess(preds)

# Instantiate a predictor
predictor = Predictor('leroyrr/fake-news-detection-bert')

# Create title and description for our task
title = "Fake News Detection Demo"
description = "Detect fake news"
article = "Created from nguyenquocviet/fake-news-detection-bert"
# Create the Gradio interface
iface = gr.Interface(fn=predictor.predict,
                     inputs="textbox",
                     outputs="textbox",
                     title=title,
                     description=description,
                     article=article)

# Launch the interface
iface.launch()