Spaces:
Runtime error
Runtime error
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() | |