File size: 7,347 Bytes
39c1ac4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe86b7f
 
39c1ac4
 
c1810f7
 
39c1ac4
 
 
 
 
 
 
 
fe86b7f
39c1ac4
c1810f7
 
 
39c1ac4
fe86b7f
39c1ac4
 
 
fe86b7f
39c1ac4
 
 
fe86b7f
39c1ac4
 
 
 
fe86b7f
39c1ac4
 
 
 
 
fe86b7f
39c1ac4
 
 
 
 
 
 
 
 
 
 
 
 
fe86b7f
 
 
39c1ac4
 
 
 
c1810f7
39c1ac4
 
 
 
 
 
 
 
 
 
 
fe86b7f
 
39c1ac4
 
fe86b7f
 
 
39c1ac4
 
 
 
 
fe86b7f
 
 
39c1ac4
fe86b7f
39c1ac4
c1810f7
fe86b7f
39c1ac4
fe86b7f
39c1ac4
 
 
 
 
fe86b7f
39c1ac4
fe86b7f
39c1ac4
 
 
 
 
 
 
 
 
 
 
c1810f7
 
39c1ac4
fe86b7f
 
 
 
 
 
 
39c1ac4
c1810f7
39c1ac4
fe86b7f
 
 
 
 
c1810f7
39c1ac4
c1810f7
 
 
 
39c1ac4
c1810f7
 
 
39c1ac4
c1810f7
 
fe86b7f
39c1ac4
c1810f7
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import streamlit as st
from transformers import pipeline

############ SETTING UP THE PAGE LAYOUT AND TITLE ############

# `st.set_page_config` is used to display the default layout width, the title of the app, and the emoticon in the browser tab.

st.set_page_config(layout="centered", page_title="X_G85 Fake News", page_icon="πŸ“‘")

############ CREATE THE LOGO AND HEADING ############

# We create a set of columns to display the logo and the heading next to each other.
c1, c2 = st.columns([0.32, 2])

# The snowflake logo will be displayed in the first column, on the left.
with c1:
    st.caption("")
    st.title("πŸ“‘")

# The heading will be on the right.
with c2:
    st.caption("")
    st.title("X_G85 Fake News")

# We need to set up session state via st.session_state so that app interactions don't reset the app.
if "valid_inputs_received" not in st.session_state:
    st.session_state["valid_inputs_received"] = False

############ SIDEBAR CONTENT ############

st.sidebar.subheader("Model Options")
st.sidebar.write("")


# Model selection
SELECTED_MODEL = st.sidebar.selectbox("Choose a model", ("Bert", "Roberta", "Lstm"))

if SELECTED_MODEL:
    st.session_state.valid_inputs_received = False

MODEL_INFO = {
    "Bert": """
    #### [BERT base model (uncased)](https://huggingface.co/google-bert/bert-base-uncased)
    Pretrained model on English language using a masked language modeling (MLM) objective. It was introduced in this paper and first released in this repository. This model is uncased: it does not make a difference between english and English.
    """,
    "Roberta": """
    #### [jy46604790/Fake-News-Bert-Detect](https://huggingface.co/jy46604790/Fake-News-Bert-Detect)
    This model is trained by over 40,000 news from different medias based on the 'roberta-base'. It can give result by simply entering the text of the news less than 500 words(the excess will be truncated automatically).
    """,
    "Lstm": """
    #### [X_G85 Fake News LSTM MODEL](https://huggingface.co/x-g85)
    It is trained on the provided datasets\n
    Notebook: [Fake News using Lstm](https://www.kaggle.com/code/adamalrahman/fake-news-using-lstm)
    """,
    None: "NO MODEL SELECTED",
}


model_info_container = st.sidebar.container(border=True)
model_info_container.markdown("### Model Information\n")
model_info_container.markdown(MODEL_INFO[SELECTED_MODEL])


copyright_container = st.sidebar.container(border=True)
copyright_container.markdown("Copyright ©️ 2024 [X_G85](https://huggingface.co/x-g85)")


############ TABBED NAVIGATION ############


MainTab, InfoTab = st.tabs(["Main", "Info"])

with InfoTab:
    st.subheader("X_G85 Fake News")
    st.markdown(
        "It is fake news detection based on the following models trained on datasets"
    )

    st.subheader("Datasets")
    st.markdown(
        """
        We have used the following datasets to create our own datasets and train models.
        - [Kaggle: Fake news detection dataset english](https://www.kaggle.com/datasets/sadikaljarif/fake-news-detection-dataset-english)
        - [Kaggle: Liar Preprocessed](https://www.kaggle.com/datasets/khandalaryan/liar-preprocessed-dataset)
        - [Kaggle: Stocknews](https://www.kaggle.com/datasets/aaron7sun/stocknews)
        """
    )

    st.subheader("Credits")
    st.markdown(
        """
        - Bert: [google-bert/bert-base-uncased](https://huggingface.co/google-bert/bert-base-uncased) 
        - Roberta: [jy46604790/Fake-News-Bert-Detect](https://huggingface.co/jy46604790/Fake-News-Bert-Detect)
        """
    )
    st.write("")
    copyright_container = st.container(border=True)
    copyright_container.markdown(
        "Copyright ©️ 2024 [X_G85](https://huggingface.co/x-g85)"
    )


def MODEL_RESULT(model: str, news: str) -> str | None:
    if model == "Roberta":
        MODEL_jy46604790 = "jy46604790/Fake-News-Bert-Detect"
        classifier = pipeline(
            "text-classification", model=MODEL_jy46604790, tokenizer=MODEL_jy46604790
        )
        result = classifier(news)

        if result[0]["label"] == "LABEL_1":
            return "REAL NEWS"
        else:
            return "FAKE NEWS"

    # TODO(Adam-Al-Rahman): Complete the statement
    if model == "Bert":
        pass

    if model == "Lstm":
        from utils import modelx

        return modelx(arch=model, model_path="models/lstm/x_g85_lstm.keras", text=news)


with MainTab:
    # Then, we create a intro text for the app, which we wrap in a st.markdown() widget.

    st.write("")
    st.markdown("Classify News based on the selected ml model.")
    st.write("")
    container = st.container(border=True)
    container.write(f"Selected model: {SELECTED_MODEL}")

    with st.form(key="form"):
        pre_defined_news = "Indonesian police have recaptured a U.S. citizen who escaped a week ago from an overcrowded prison on the holiday island of Bali, the jail's second breakout of foreign inmates this year.  Cristian Beasley from California was rearrested on Sunday, Badung Police Chief Yudith Satria Hananta said, without providing further details.  Beasley was a suspect in crimes related to narcotics but had not been sentenced when he escaped from Kerobokan prison in Bali last week. The 32-year-old is believed to have cut through bars in the ceiling of his cell before scaling a perimeter wall of the prison in an area being refurbished. The Kerobokan prison, about 10 km (six miles) from the main tourist beaches in the Kuta area, often holds foreigners facing drug-related charges. Representatives of Beasley could not immediately be reached for comment. In June, an Australian, a Bulgarian, an Indian, and a Malaysian tunneled to freedom about 12 meters (13 yards) under Kerobokan prison s walls. The Indian and the Bulgarian were caught soon after in neighboring East Timor, but Australian Shaun Edward Davidson and Malaysian Tee Kok King remain at large. Davidson has taunted authorities by saying he was enjoying life in various parts of the world, in purported posts on Facebook.  Kerobokan has housed several well-known foreign drug convicts, including Australian Schappelle Corby, whose 12-1/2-year sentence for marijuana smuggling got huge media attention."

        news = st.text_area(
            "Enter news to classify",
            pre_defined_news,
            height=200,
            help="Please provide the news that you need to verify for its truthfulness.",
            key="news",
        )

        submit_button = st.form_submit_button(label="Submit")

        if (
            not news
            and not submit_button
            and not st.session_state.valid_inputs_received
        ):
            st.stop()

        elif submit_button and not news:
            st.warning("πŸ“‘ There is no news to classify")
            st.session_state.valid_inputs_received = False
            st.stop()

        elif submit_button or st.session_state.valid_inputs_received:
            if submit_button:
                st.session_state.valid_inputs_received = True

            # Default Model: Bert
            MODEL = SELECTED_MODEL if SELECTED_MODEL else "Bert"
            result = MODEL_RESULT(model=MODEL, news=news)

            if result:
                st.success(f"Result: {result}")
            else:
                st.error(f"{MODEL} model error")