|
import streamlit as st
|
|
import datetime
|
|
import pandas as pd
|
|
from gnews import GNews
|
|
from transformers import pipeline
|
|
import plotly.graph_objects as go
|
|
|
|
|
|
pipe = pipeline("text-classification", model="pramudyalyza/bert-indonesian-finetuned-news")
|
|
|
|
|
|
def process_keyword(keyword):
|
|
one_week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
|
|
|
|
news = GNews(language='id', country='ID', max_results=100)
|
|
|
|
search_results = news.get_news(keyword)
|
|
|
|
filtered_headlines = []
|
|
for article in search_results:
|
|
published_date = datetime.datetime.strptime(article['published date'], '%a, %d %b %Y %H:%M:%S %Z')
|
|
if published_date > one_week_ago:
|
|
filtered_headlines.append(article['title'])
|
|
|
|
df = pd.DataFrame(filtered_headlines, columns=['title'])
|
|
df_clean = df.drop_duplicates()
|
|
|
|
df_clean['sentiment'] = df_clean['title'].apply(lambda x: pipe(x)[0]['label'])
|
|
|
|
positive_count = (df_clean['sentiment'] == 'Positive').sum()
|
|
negative_count = (df_clean['sentiment'] == 'Negative').sum()
|
|
total_count = len(df_clean)
|
|
|
|
return positive_count, negative_count, total_count, df_clean
|
|
|
|
|
|
st.title("News Sentiment Analysis Dashboard")
|
|
|
|
keyword_input = st.text_input("Enter a keyword to search for news", placeholder="Type a keyword...")
|
|
|
|
if st.button("Analyze"):
|
|
if keyword_input:
|
|
with st.spinner('Scraping and analyzing the data...'):
|
|
positive_count, negative_count, total_count, df_clean = process_keyword(keyword_input)
|
|
|
|
|
|
fig_positive = go.Figure(go.Indicator(
|
|
mode="gauge+number",
|
|
value=positive_count,
|
|
title={'text': "Positive Sentiment"},
|
|
gauge={'axis': {'range': [0, total_count]},
|
|
'bar': {'color': "green"}}
|
|
))
|
|
|
|
fig_negative = go.Figure(go.Indicator(
|
|
mode="gauge+number",
|
|
value=negative_count,
|
|
title={'text': "Negative Sentiment"},
|
|
gauge={'axis': {'range': [0, total_count]},
|
|
'bar': {'color': "red"}}
|
|
))
|
|
|
|
fig_donut = go.Figure(go.Pie(
|
|
labels=['Positive', 'Negative'],
|
|
values=[positive_count, negative_count],
|
|
hole=0.5,
|
|
marker=dict(colors=['green', 'red'])
|
|
))
|
|
fig_donut.update_layout(title_text='Sentiment Distribution')
|
|
|
|
|
|
col1, col2, col3 = st.columns(3)
|
|
|
|
|
|
col1.plotly_chart(fig_positive, use_container_width=True)
|
|
col2.plotly_chart(fig_negative, use_container_width=True)
|
|
col3.plotly_chart(fig_donut, use_container_width=True)
|
|
|
|
st.write(f"News articles found: {total_count}")
|
|
|
|
|
|
st.dataframe(df_clean, use_container_width=True)
|
|
|
|
|
|
csv = df_clean.to_csv(index=False).encode('utf-8')
|
|
st.download_button(
|
|
label="Download CSV",
|
|
data=csv,
|
|
file_name='news_sentiment_analysis.csv',
|
|
mime='text/csv',
|
|
)
|
|
else:
|
|
st.error("Please enter a keyword.") |