Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ st.markdown(
|
|
| 18 |
unsafe_allow_html=True
|
| 19 |
)
|
| 20 |
st.markdown(
|
| 21 |
-
"<h4 style='text-align:center; color:#375a7f;'>AI-powered
|
| 22 |
unsafe_allow_html=True
|
| 23 |
)
|
| 24 |
st.markdown("---")
|
|
@@ -39,7 +39,7 @@ with col2:
|
|
| 39 |
help="e.g. 'Military', 'worldnews', or leave blank for all"
|
| 40 |
)
|
| 41 |
with col3:
|
| 42 |
-
query = st.text_input("Enter your topic or query:", value="
|
| 43 |
|
| 44 |
max_articles = st.slider("Number of news articles:", 5, 25, 12)
|
| 45 |
|
|
@@ -62,7 +62,7 @@ def get_summary_with_hf_llm(keywords, sentiment_counts, top_subreddits, top_post
|
|
| 62 |
f"Sentiment counts: {dict(sentiment_counts)}\n"
|
| 63 |
f"Most active subreddits: {', '.join(top_subreddits)}\n"
|
| 64 |
f"Example Reddit post titles: {', '.join(top_posts)}\n"
|
| 65 |
-
"Then, suggest 3-5 additional related search terms that could improve situational awareness."
|
| 66 |
)
|
| 67 |
|
| 68 |
url = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
|
@@ -70,7 +70,6 @@ def get_summary_with_hf_llm(keywords, sentiment_counts, top_subreddits, top_post
|
|
| 70 |
resp = requests.post(url, json={"inputs": prompt}, timeout=60)
|
| 71 |
resp.raise_for_status()
|
| 72 |
output = resp.json()
|
| 73 |
-
# Output format can vary; support both possible types:
|
| 74 |
if isinstance(output, list) and "generated_text" in output[0]:
|
| 75 |
return output[0]["generated_text"]
|
| 76 |
elif isinstance(output, dict) and "generated_text" in output:
|
|
@@ -121,7 +120,7 @@ if st.button("π Analyze!"):
|
|
| 121 |
|
| 122 |
st.success(f"Found {len(reddit_data)} Reddit posts. Sentiment analysis complete.")
|
| 123 |
|
| 124 |
-
# --- Sentiment Results Table &
|
| 125 |
results_df = pd.DataFrame(reddit_data)
|
| 126 |
results_df['sentiment'] = sentiment_results
|
| 127 |
|
|
@@ -132,51 +131,4 @@ if st.button("π Analyze!"):
|
|
| 132 |
sentiment_counts = sentiment_counts[sentiment_counts.index.str.strip() != '']
|
| 133 |
|
| 134 |
sentiment_df = pd.DataFrame({
|
| 135 |
-
'Sentiment':
|
| 136 |
-
'Count': sentiment_counts.values
|
| 137 |
-
})
|
| 138 |
-
|
| 139 |
-
# Nice color palette for bars
|
| 140 |
-
palette = ['#183153', '#3277b3', '#375a7f', '#3bb273', '#ffb347', '#e05a47']
|
| 141 |
-
|
| 142 |
-
if not sentiment_df.empty and sentiment_df['Sentiment'].nunique() > 0:
|
| 143 |
-
fig = px.bar(
|
| 144 |
-
sentiment_df,
|
| 145 |
-
x='Sentiment',
|
| 146 |
-
y='Count',
|
| 147 |
-
color='Sentiment',
|
| 148 |
-
color_discrete_sequence=palette,
|
| 149 |
-
text='Count',
|
| 150 |
-
labels={'Sentiment': 'Sentiment', 'Count': 'Count'},
|
| 151 |
-
title='Sentiment Distribution'
|
| 152 |
-
)
|
| 153 |
-
fig.update_traces(marker_line_width=1, textposition="outside")
|
| 154 |
-
fig.update_layout(
|
| 155 |
-
yaxis=dict(title='Count'),
|
| 156 |
-
xaxis=dict(title='Sentiment'),
|
| 157 |
-
showlegend=False,
|
| 158 |
-
plot_bgcolor="#f8fafc",
|
| 159 |
-
paper_bgcolor="#f8fafc",
|
| 160 |
-
font=dict(size=15),
|
| 161 |
-
margin=dict(t=60, b=60, r=40, l=40)
|
| 162 |
-
)
|
| 163 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 164 |
-
else:
|
| 165 |
-
st.info("No valid sentiment data for plotting.", icon="πΆ")
|
| 166 |
-
|
| 167 |
-
# --- AI SUMMARY SECTION ---
|
| 168 |
-
# Get top 3 subreddits and top 3 post titles for summary
|
| 169 |
-
top_subreddits = results_df['subreddit'].value_counts().index[:3].tolist() if 'subreddit' in results_df else []
|
| 170 |
-
top_posts = results_df['body'].dropna().astype(str).str[:50].tolist()[:3] if 'body' in results_df else []
|
| 171 |
-
|
| 172 |
-
st.markdown("### π AI-Generated Summary & Suggestions")
|
| 173 |
-
summary = get_summary_with_hf_llm(
|
| 174 |
-
keywords=keywords,
|
| 175 |
-
sentiment_counts=sentiment_counts,
|
| 176 |
-
top_subreddits=top_subreddits,
|
| 177 |
-
top_posts=top_posts,
|
| 178 |
-
user_query=query
|
| 179 |
-
)
|
| 180 |
-
st.info(summary)
|
| 181 |
-
else:
|
| 182 |
-
st.warning("No news articles found for that query. Try a different topic or broaden the date range.", icon="π°")
|
|
|
|
| 18 |
unsafe_allow_html=True
|
| 19 |
)
|
| 20 |
st.markdown(
|
| 21 |
+
"<h4 style='text-align:center; color:#375a7f;'>AI-powered Open Source Research: See what the news & social web really think</h4>",
|
| 22 |
unsafe_allow_html=True
|
| 23 |
)
|
| 24 |
st.markdown("---")
|
|
|
|
| 39 |
help="e.g. 'Military', 'worldnews', or leave blank for all"
|
| 40 |
)
|
| 41 |
with col3:
|
| 42 |
+
query = st.text_input("Enter your topic or query:", value="Response Here")
|
| 43 |
|
| 44 |
max_articles = st.slider("Number of news articles:", 5, 25, 12)
|
| 45 |
|
|
|
|
| 62 |
f"Sentiment counts: {dict(sentiment_counts)}\n"
|
| 63 |
f"Most active subreddits: {', '.join(top_subreddits)}\n"
|
| 64 |
f"Example Reddit post titles: {', '.join(top_posts)}\n"
|
| 65 |
+
"Then, suggest 3-5 additional related search terms that could improve situational awareness. In another paragraph suggest how the search results could be perceived by the US populace"
|
| 66 |
)
|
| 67 |
|
| 68 |
url = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
|
|
|
| 70 |
resp = requests.post(url, json={"inputs": prompt}, timeout=60)
|
| 71 |
resp.raise_for_status()
|
| 72 |
output = resp.json()
|
|
|
|
| 73 |
if isinstance(output, list) and "generated_text" in output[0]:
|
| 74 |
return output[0]["generated_text"]
|
| 75 |
elif isinstance(output, dict) and "generated_text" in output:
|
|
|
|
| 120 |
|
| 121 |
st.success(f"Found {len(reddit_data)} Reddit posts. Sentiment analysis complete.")
|
| 122 |
|
| 123 |
+
# --- Sentiment Results Table & Pie/Donut Chart ---
|
| 124 |
results_df = pd.DataFrame(reddit_data)
|
| 125 |
results_df['sentiment'] = sentiment_results
|
| 126 |
|
|
|
|
| 131 |
sentiment_counts = sentiment_counts[sentiment_counts.index.str.strip() != '']
|
| 132 |
|
| 133 |
sentiment_df = pd.DataFrame({
|
| 134 |
+
'Sentiment': sentiment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|