news-analyzer / app.py
elozano's picture
NER model added
30ad188
raw
history blame
No virus
2.36 kB
import streamlit as st
from annotated_text import annotated_text
from news_pipeline import NewsPipeline
CATEGORY_EMOJIS = {
"Automobile": "πŸš—",
"Entertainment": "🍿",
"Politics": "βš–οΈ",
"Science": "πŸ§ͺ",
"Sports": "πŸ€",
"Technology": "πŸ’»",
"World": "🌍",
}
FAKE_EMOJIS = {"Fake": "πŸ‘»", "Real": "πŸ‘"}
CLICKBAIT_EMOJIS = {"Clickbait": "🎣", "Normal": "βœ…"}
def app():
news_pipe = NewsPipeline()
st.title("πŸ“° News Analyzer")
headline = st.text_input("Article headline:")
content = st.text_area("Article content:")
button = st.button("Analyze")
if button:
if headline == "" and content == "":
st.error("Please, introduce an article headline and content.")
else:
if headline == "" or content == "":
st.warning(
"Please, provide both headline and content to achieve better results."
)
else:
st.success("Article successfully analyzed!")
with st.spinner("Analyzing article..."):
prediction = news_pipe(headline, content)
col1, _, col2 = st.columns([2, 1, 6])
with col1:
st.subheader("Analysis:")
st.markdown(
f"{CATEGORY_EMOJIS[prediction['category']]} **Category**: {prediction['category']}"
)
st.markdown(
f"{FAKE_EMOJIS[prediction['fake']]} **Fake**: {'Yes' if prediction['fake'] == 'Fake' else 'No'}"
)
st.markdown(
f"{CLICKBAIT_EMOJIS[prediction['clickbait']]} **Clickbait**: {'Yes' if prediction['clickbait'] == 'Clickbait' else 'No'}"
)
with col2:
st.subheader("Headline")
annotated_text(*parse_text(headline, prediction["ner"]["headline"]))
st.subheader("Content")
annotated_text(*parse_text(content, prediction["ner"]["content"]))
def parse_text(text, prediction):
start = 0
parsed_text = []
for p in prediction:
parsed_text.append(text[start : p["start"]])
parsed_text.append((p["word"], p["entity_group"]))
start = p["end"]
parsed_text.append(text[start:])
return parsed_text
if __name__ == "__main__":
app()