Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,104 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import datetime
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from gnews import GNews
|
| 5 |
-
from transformers import pipeline
|
| 6 |
-
import plotly.graph_objects as go
|
| 7 |
-
|
| 8 |
-
# Load the sentiment analysis model
|
| 9 |
-
pipe = pipeline("text-classification", model="pramudyalyza/bert-indonesian-finetuned-news
|
| 10 |
-
|
| 11 |
-
# Function to process the keyword and get sentiment analysis
|
| 12 |
-
def process_keyword(keyword):
|
| 13 |
-
one_week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
|
| 14 |
-
|
| 15 |
-
news = GNews(language='id', country='ID', max_results=100)
|
| 16 |
-
|
| 17 |
-
search_results = news.get_news(keyword)
|
| 18 |
-
|
| 19 |
-
filtered_headlines = []
|
| 20 |
-
for article in search_results:
|
| 21 |
-
published_date = datetime.datetime.strptime(article['published date'], '%a, %d %b %Y %H:%M:%S %Z')
|
| 22 |
-
if published_date > one_week_ago:
|
| 23 |
-
filtered_headlines.append(article['title'])
|
| 24 |
-
|
| 25 |
-
df = pd.DataFrame(filtered_headlines, columns=['title'])
|
| 26 |
-
df_clean = df.drop_duplicates()
|
| 27 |
-
|
| 28 |
-
df_clean['sentiment'] = df_clean['title'].apply(lambda x: pipe(x)[0]['label'])
|
| 29 |
-
|
| 30 |
-
positive_count = (df_clean['sentiment'] == 'Positive').sum()
|
| 31 |
-
negative_count = (df_clean['sentiment'] == 'Negative').sum()
|
| 32 |
-
neutral_count = (df_clean['sentiment'] == 'Neutral').sum()
|
| 33 |
-
total_count = len(df_clean)
|
| 34 |
-
|
| 35 |
-
return positive_count, negative_count, neutral_count, total_count, df_clean
|
| 36 |
-
|
| 37 |
-
# Streamlit app layout
|
| 38 |
-
st.title("News Sentiment Analysis Dashboard")
|
| 39 |
-
|
| 40 |
-
keyword_input = st.text_input("Enter a keyword to search for news", placeholder="Type a keyword...")
|
| 41 |
-
|
| 42 |
-
if st.button("Analyze"):
|
| 43 |
-
if keyword_input:
|
| 44 |
-
with st.spinner('Scraping and analyzing the data...'):
|
| 45 |
-
positive_count, negative_count, neutral_count, total_count, df_clean = process_keyword(keyword_input)
|
| 46 |
-
|
| 47 |
-
# Create plots
|
| 48 |
-
fig_positive = go.Figure(go.Indicator(
|
| 49 |
-
mode="gauge+number",
|
| 50 |
-
value=positive_count,
|
| 51 |
-
title={'text': "Positive Sentiment"},
|
| 52 |
-
gauge={'axis': {'range': [0, total_count]},
|
| 53 |
-
'bar': {'color': "green"}}
|
| 54 |
-
))
|
| 55 |
-
|
| 56 |
-
fig_negative = go.Figure(go.Indicator(
|
| 57 |
-
mode="gauge+number",
|
| 58 |
-
value=negative_count,
|
| 59 |
-
title={'text': "Negative Sentiment"},
|
| 60 |
-
gauge={'axis': {'range': [0, total_count]},
|
| 61 |
-
'bar': {'color': "red"}}
|
| 62 |
-
))
|
| 63 |
-
|
| 64 |
-
fig_neutral = go.Figure(go.Indicator(
|
| 65 |
-
mode="gauge+number",
|
| 66 |
-
value=neutral_count,
|
| 67 |
-
title={'text': "Neutral Sentiment"},
|
| 68 |
-
gauge={'axis': {'range': [0, total_count]},
|
| 69 |
-
'bar': {'color': "yellow"}}
|
| 70 |
-
))
|
| 71 |
-
|
| 72 |
-
fig_donut = go.Figure(go.Pie(
|
| 73 |
-
labels=['Positive', 'Negative', 'Neutral'],
|
| 74 |
-
values=[positive_count, negative_count, neutral_count],
|
| 75 |
-
hole=0.5,
|
| 76 |
-
marker=dict(colors=['green', 'red', 'yellow'])
|
| 77 |
-
))
|
| 78 |
-
fig_donut.update_layout(title_text='Sentiment Distribution')
|
| 79 |
-
|
| 80 |
-
# Create a horizontal layout using st.columns
|
| 81 |
-
col1, col2, col3 = st.columns(3)
|
| 82 |
-
|
| 83 |
-
# Display results in each column
|
| 84 |
-
col1.plotly_chart(fig_positive, use_container_width=True)
|
| 85 |
-
col2.plotly_chart(fig_negative, use_container_width=True)
|
| 86 |
-
col3.plotly_chart(fig_neutral, use_container_width=True)
|
| 87 |
-
|
| 88 |
-
st.plotly_chart(fig_donut, use_container_width=True)
|
| 89 |
-
|
| 90 |
-
st.write(f"News articles found: {total_count}")
|
| 91 |
-
|
| 92 |
-
# Show DataFrame
|
| 93 |
-
st.dataframe(df_clean, use_container_width=True)
|
| 94 |
-
|
| 95 |
-
# Download CSV
|
| 96 |
-
csv = df_clean.to_csv(index=False).encode('utf-8')
|
| 97 |
-
st.download_button(
|
| 98 |
-
label="Download CSV",
|
| 99 |
-
data=csv,
|
| 100 |
-
file_name='news_sentiment_analysis.csv',
|
| 101 |
-
mime='text/csv',
|
| 102 |
-
)
|
| 103 |
-
else:
|
| 104 |
st.error("Please enter a keyword.")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import datetime
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from gnews import GNews
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
|
| 8 |
+
# Load the sentiment analysis model
|
| 9 |
+
pipe = pipeline("text-classification", model="pramudyalyza/bert-indonesian-finetuned-news")
|
| 10 |
+
|
| 11 |
+
# Function to process the keyword and get sentiment analysis
|
| 12 |
+
def process_keyword(keyword):
|
| 13 |
+
one_week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
|
| 14 |
+
|
| 15 |
+
news = GNews(language='id', country='ID', max_results=100)
|
| 16 |
+
|
| 17 |
+
search_results = news.get_news(keyword)
|
| 18 |
+
|
| 19 |
+
filtered_headlines = []
|
| 20 |
+
for article in search_results:
|
| 21 |
+
published_date = datetime.datetime.strptime(article['published date'], '%a, %d %b %Y %H:%M:%S %Z')
|
| 22 |
+
if published_date > one_week_ago:
|
| 23 |
+
filtered_headlines.append(article['title'])
|
| 24 |
+
|
| 25 |
+
df = pd.DataFrame(filtered_headlines, columns=['title'])
|
| 26 |
+
df_clean = df.drop_duplicates()
|
| 27 |
+
|
| 28 |
+
df_clean['sentiment'] = df_clean['title'].apply(lambda x: pipe(x)[0]['label'])
|
| 29 |
+
|
| 30 |
+
positive_count = (df_clean['sentiment'] == 'Positive').sum()
|
| 31 |
+
negative_count = (df_clean['sentiment'] == 'Negative').sum()
|
| 32 |
+
neutral_count = (df_clean['sentiment'] == 'Neutral').sum()
|
| 33 |
+
total_count = len(df_clean)
|
| 34 |
+
|
| 35 |
+
return positive_count, negative_count, neutral_count, total_count, df_clean
|
| 36 |
+
|
| 37 |
+
# Streamlit app layout
|
| 38 |
+
st.title("News Sentiment Analysis Dashboard")
|
| 39 |
+
|
| 40 |
+
keyword_input = st.text_input("Enter a keyword to search for news", placeholder="Type a keyword...")
|
| 41 |
+
|
| 42 |
+
if st.button("Analyze"):
|
| 43 |
+
if keyword_input:
|
| 44 |
+
with st.spinner('Scraping and analyzing the data...'):
|
| 45 |
+
positive_count, negative_count, neutral_count, total_count, df_clean = process_keyword(keyword_input)
|
| 46 |
+
|
| 47 |
+
# Create plots
|
| 48 |
+
fig_positive = go.Figure(go.Indicator(
|
| 49 |
+
mode="gauge+number",
|
| 50 |
+
value=positive_count,
|
| 51 |
+
title={'text': "Positive Sentiment"},
|
| 52 |
+
gauge={'axis': {'range': [0, total_count]},
|
| 53 |
+
'bar': {'color': "green"}}
|
| 54 |
+
))
|
| 55 |
+
|
| 56 |
+
fig_negative = go.Figure(go.Indicator(
|
| 57 |
+
mode="gauge+number",
|
| 58 |
+
value=negative_count,
|
| 59 |
+
title={'text': "Negative Sentiment"},
|
| 60 |
+
gauge={'axis': {'range': [0, total_count]},
|
| 61 |
+
'bar': {'color': "red"}}
|
| 62 |
+
))
|
| 63 |
+
|
| 64 |
+
fig_neutral = go.Figure(go.Indicator(
|
| 65 |
+
mode="gauge+number",
|
| 66 |
+
value=neutral_count,
|
| 67 |
+
title={'text': "Neutral Sentiment"},
|
| 68 |
+
gauge={'axis': {'range': [0, total_count]},
|
| 69 |
+
'bar': {'color': "yellow"}}
|
| 70 |
+
))
|
| 71 |
+
|
| 72 |
+
fig_donut = go.Figure(go.Pie(
|
| 73 |
+
labels=['Positive', 'Negative', 'Neutral'],
|
| 74 |
+
values=[positive_count, negative_count, neutral_count],
|
| 75 |
+
hole=0.5,
|
| 76 |
+
marker=dict(colors=['green', 'red', 'yellow'])
|
| 77 |
+
))
|
| 78 |
+
fig_donut.update_layout(title_text='Sentiment Distribution')
|
| 79 |
+
|
| 80 |
+
# Create a horizontal layout using st.columns
|
| 81 |
+
col1, col2, col3 = st.columns(3)
|
| 82 |
+
|
| 83 |
+
# Display results in each column
|
| 84 |
+
col1.plotly_chart(fig_positive, use_container_width=True)
|
| 85 |
+
col2.plotly_chart(fig_negative, use_container_width=True)
|
| 86 |
+
col3.plotly_chart(fig_neutral, use_container_width=True)
|
| 87 |
+
|
| 88 |
+
st.plotly_chart(fig_donut, use_container_width=True)
|
| 89 |
+
|
| 90 |
+
st.write(f"News articles found: {total_count}")
|
| 91 |
+
|
| 92 |
+
# Show DataFrame
|
| 93 |
+
st.dataframe(df_clean, use_container_width=True)
|
| 94 |
+
|
| 95 |
+
# Download CSV
|
| 96 |
+
csv = df_clean.to_csv(index=False).encode('utf-8')
|
| 97 |
+
st.download_button(
|
| 98 |
+
label="Download CSV",
|
| 99 |
+
data=csv,
|
| 100 |
+
file_name='news_sentiment_analysis.csv',
|
| 101 |
+
mime='text/csv',
|
| 102 |
+
)
|
| 103 |
+
else:
|
| 104 |
st.error("Please enter a keyword.")
|