File size: 2,575 Bytes
685ba0e
 
d6a25c5
30ad188
d6a25c5
685ba0e
d6a25c5
 
685ba0e
 
 
 
 
 
 
d6a25c5
685ba0e
 
 
 
 
 
 
 
 
 
 
 
 
9eebfee
30ad188
 
685ba0e
30ad188
685ba0e
30ad188
685ba0e
30ad188
685ba0e
30ad188
685ba0e
30ad188
685ba0e
30ad188
685ba0e
30ad188
685ba0e
 
 
 
 
 
 
 
 
 
 
30ad188
 
685ba0e
 
 
30ad188
 
685ba0e
 
 
 
30ad188
 
d6a25c5
 
 
685ba0e
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
from typing import Dict, List, Tuple, Union

import streamlit as st
from annotated_text import annotated_text

from analyzer import NewsAnalyzer


def run() -> None:
    analyzer = NewsAnalyzer(
        category_model_name="elozano/news-category",
        fake_model_name="elozano/news-fake",
        clickbait_model_name="elozano/news-clickbait",
        ner_model_name="dslim/bert-base-NER",
    )
    st.title("📰 News Analyzer")
    headline = st.text_input("Headline:")
    content = st.text_input("Content:")
    if headline == "":
        st.error("Please, provide a headline.")
    else:
        if content == "":
            st.warning(
                "Please, provide both headline and content to achieve better results."
            )
        button = st.button("Analyze")
        if button:
            predictions = analyzer(headline=headline, content=content)
            col1, _, col2 = st.columns([2, 1, 5])

            with col1:
                st.subheader("Analysis:")
                category_prediction = predictions["category"]
                st.markdown(
                    f"{category_prediction['emoji']} **Category**: {category_prediction['label']}"
                )
                clickbait_prediction = predictions["clickbait"]
                st.markdown(
                    f"{clickbait_prediction['emoji']} **Clickbait**: {'Yes' if clickbait_prediction['label'] == 'Clickbait' else 'No'}"
                )
                fake_prediction = predictions["fake"]
                st.markdown(
                    f"{fake_prediction['emoji']} **Fake**: {'Yes' if fake_prediction['label'] == 'Fake' else 'No'}"
                )

            with col2:
                st.subheader("Headline:")
                annotated_text(
                    *parse_entities(headline, predictions["ner"]["headline"])
                )
                st.subheader("Content:")
                if content:
                    annotated_text(
                        *parse_entities(content, predictions["ner"]["content"])
                    )
                else:
                    st.error("Content not provided.")


def parse_entities(
    text: str, entities: Dict[str, Union[str, int]]
) -> List[Union[str, Tuple[str, str]]]:
    start = 0
    parsed_text = []
    for entity in entities:
        parsed_text.append(text[start : entity["start"]])
        parsed_text.append((entity["word"], entity["entity_group"]))
        start = entity["end"]
    parsed_text.append(text[start:])
    return parsed_text


if __name__ == "__main__":
    run()