File size: 2,312 Bytes
72eedb2
 
 
 
 
 
 
 
ef01f5b
72eedb2
 
ef01f5b
72eedb2
 
 
 
 
 
 
ef01f5b
 
72eedb2
7ac517b
ef01f5b
72eedb2
 
 
 
ef01f5b
 
72eedb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef01f5b
 
 
 
 
 
 
 
 
 
 
 
 
 
72eedb2
 
ef01f5b
72eedb2
ef01f5b
 
 
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
59
60
61
62
63
64
65
66
67
import streamlit as st
import numpy as np
import re
import time

from transformers import AutoModelForSequenceClassification, AutoTokenizer, Trainer
from Scraper import Scrap

st.set_page_config(layout="wide")

model_checkpoint = "Rifky/FND"
label = {0: "valid", 1: "fake"}

@st.cache(show_spinner=False, allow_output_mutation=True)
def load_model():
    model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=2)
    tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, fast=True)
    return Trainer(model=model), tokenizer

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

input_column, reference_column = st.columns(2)
input_column.write('# Fake News Detection AI')

with st.spinner("Loading Model..."):
    model, tokenizer = load_model()

user_input = input_column.text_input("Article url")
submit = input_column.button("submit")


if submit:
    last_time = time.time()
    with st.spinner("Reading Article..."):
        if user_input:
            if user_input[:4] == 'http':
                text = Scrap(user_input)
            else:
                text = user_input

    if text:
        text = re.sub(r'\n', ' ', text)

        with st.spinner("Computing..."):
            text = text.split()
            text_len = len(text)

            sequences = []
            for i in range(text_len // 512):
                sequences.append(" ".join(text[i * 512: (i + 1) * 512]))
            sequences.append(" ".join(text[text_len - (text_len % 512) : text_len]))
            sequences = [tokenizer(i, max_length=512, truncation=True, padding="max_length") for i in sequences]

            predictions = model.predict(sequences)[0]
            result = [
                np.sum([sigmoid(i[0]) for i in predictions]) / len(predictions), 
                np.sum([sigmoid(i[1]) for i in predictions]) / len(predictions)
            ]
                
            print (f'\nresult: {result}')
            input_column.markdown(f"<small>Compute Finished in {int(time.time() - last_time)} seconds</small>", unsafe_allow_html=True)
            prediction = np.argmax(result, axis=-1)
            input_column.success(f"This news is {label[prediction]}.")
            st.text(f"{int(result[prediction]*100)}% confidence")
            input_column.progress(result[prediction])