Sami2000 commited on
Commit
5242888
·
verified ·
1 Parent(s): 0814da7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -29,7 +29,7 @@ selected_days = date_range[1]
29
  # --- NEW: Subreddit input ---
30
  subreddit = st.text_input("Specify a subreddit (optional, e.g., 'Military' or 'worldnews'). Leave blank for all.", value="")
31
 
32
- query = st.text_input("Enter your topic or query:", value="ENTER VALUE HERE")
33
  max_articles = st.slider("Number of news articles:", 5, 25, 12)
34
 
35
  # --- CLEANING FUNCTION ---
@@ -80,16 +80,23 @@ if st.button("Search"):
80
  # Optional: Show data table
81
  st.dataframe(results_df)
82
 
83
- # Robust sentiment plot (handles missing/dirty data)
84
  sentiment_counts = results_df['sentiment'].value_counts(dropna=True)
85
- # Drop NaN or empty-string sentiment labels
86
  sentiment_counts = sentiment_counts[~sentiment_counts.index.isna() & (sentiment_counts.index != '')]
 
 
87
 
88
- if not sentiment_counts.empty and len(sentiment_counts.index) == len(sentiment_counts.values):
 
 
 
 
 
89
  fig = px.bar(
90
- x=sentiment_counts.index.tolist(),
91
- y=sentiment_counts.values.tolist(),
92
- labels={'x': 'Sentiment', 'y': 'Count'},
 
93
  title='Sentiment Distribution'
94
  )
95
  st.plotly_chart(fig, use_container_width=True)
 
29
  # --- NEW: Subreddit input ---
30
  subreddit = st.text_input("Specify a subreddit (optional, e.g., 'Military' or 'worldnews'). Leave blank for all.", value="")
31
 
32
+ query = st.text_input("Enter your topic or query:", value="US Army INDOPACOM")
33
  max_articles = st.slider("Number of news articles:", 5, 25, 12)
34
 
35
  # --- CLEANING FUNCTION ---
 
80
  # Optional: Show data table
81
  st.dataframe(results_df)
82
 
83
+ # Robust and crash-proof sentiment plot!
84
  sentiment_counts = results_df['sentiment'].value_counts(dropna=True)
 
85
  sentiment_counts = sentiment_counts[~sentiment_counts.index.isna() & (sentiment_counts.index != '')]
86
+ sentiment_counts = sentiment_counts.rename(str)
87
+ sentiment_counts = sentiment_counts[sentiment_counts.index.str.strip() != '']
88
 
89
+ sentiment_df = pd.DataFrame({
90
+ 'Sentiment': sentiment_counts.index,
91
+ 'Count': sentiment_counts.values
92
+ })
93
+
94
+ if not sentiment_df.empty and sentiment_df['Sentiment'].nunique() > 0:
95
  fig = px.bar(
96
+ sentiment_df,
97
+ x='Sentiment',
98
+ y='Count',
99
+ labels={'Sentiment': 'Sentiment', 'Count': 'Count'},
100
  title='Sentiment Distribution'
101
  )
102
  st.plotly_chart(fig, use_container_width=True)