Update app.py
Browse files
app.py
CHANGED
|
@@ -1,145 +1,127 @@
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
-
import
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
-
from transformers import pipeline
|
| 6 |
import google.generativeai as genai
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
-
|
| 12 |
-
if GEMINI_API_KEY:
|
| 13 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
-
gemini_model = genai.GenerativeModel("gemini-1.5-flash")
|
| 15 |
-
else:
|
| 16 |
-
gemini_model = None
|
| 17 |
-
|
| 18 |
-
# Hugging Face sentiment pipeline
|
| 19 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
def generate_posts(hashtag
|
|
|
|
| 23 |
posts = []
|
| 24 |
-
source = "
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
prompt = f"Generate {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
source = "huggingface"
|
| 36 |
-
|
| 37 |
-
if not posts: # fallback
|
| 38 |
-
template = [
|
| 39 |
-
f"Not sure how I feel about {hashtag} π€",
|
| 40 |
-
f"{hashtag} totally failed expectations π ",
|
| 41 |
-
f"People are talking about {hashtag} everywhere π",
|
| 42 |
-
f"{hashtag} campaign is the best thing this year π",
|
| 43 |
f"I'm disappointed with {hashtag} π",
|
|
|
|
| 44 |
f"Super excited about {hashtag} π₯",
|
|
|
|
|
|
|
| 45 |
f"I love {hashtag}! It's amazing β€οΈ",
|
| 46 |
]
|
| 47 |
-
posts = random.choices(
|
| 48 |
|
| 49 |
return posts, source
|
| 50 |
|
| 51 |
-
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
inputs=[hashtag, num_posts, visualization, use_gemini],
|
| 140 |
-
outputs=[posts_table, plot_out, summary]
|
| 141 |
)
|
| 142 |
-
|
| 143 |
-
# ----------------- MAIN -----------------
|
| 144 |
-
if __name__ == "__main__":
|
| 145 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
import plotly.express as px
|
|
|
|
| 6 |
import google.generativeai as genai
|
| 7 |
+
from transformers import pipeline
|
| 8 |
|
| 9 |
+
# ========== API KEYS ==========
|
| 10 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 12 |
|
| 13 |
+
# ========== POST GENERATOR ==========
|
| 14 |
+
def generate_posts(hashtag, n=20):
|
| 15 |
+
"""Try Gemini first, fallback to HuggingFace generated posts"""
|
| 16 |
posts = []
|
| 17 |
+
source = "Gemini"
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
prompt = f"Generate {n} realistic, diverse, short social media posts about {hashtag}. Use emojis."
|
| 21 |
+
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(prompt)
|
| 22 |
+
text = response.text.strip().split("\n")
|
| 23 |
+
posts = [t for t in text if len(t.strip()) > 0][:n]
|
| 24 |
+
except Exception:
|
| 25 |
+
source = "HuggingFace"
|
| 26 |
+
base_posts = [
|
| 27 |
+
f"{hashtag} is the best thing ever π",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
f"I'm disappointed with {hashtag} π",
|
| 29 |
+
f"Not sure how I feel about {hashtag} π€",
|
| 30 |
f"Super excited about {hashtag} π₯",
|
| 31 |
+
f"People are talking about {hashtag} everywhere π",
|
| 32 |
+
f"{hashtag} totally failed expectations π ",
|
| 33 |
f"I love {hashtag}! It's amazing β€οΈ",
|
| 34 |
]
|
| 35 |
+
posts = random.choices(base_posts, k=n)
|
| 36 |
|
| 37 |
return posts, source
|
| 38 |
|
| 39 |
+
# ========== SENTIMENT ANALYSIS ==========
|
| 40 |
+
def analyze_posts(posts):
|
| 41 |
+
results = sentiment_pipeline(posts)
|
| 42 |
+
data = []
|
| 43 |
+
for post, res in zip(posts, results):
|
| 44 |
+
data.append({
|
| 45 |
+
"Post": post,
|
| 46 |
+
"Sentiment": res["label"],
|
| 47 |
+
"Confidence": round(res["score"], 2)
|
| 48 |
+
})
|
| 49 |
+
return pd.DataFrame(data)
|
| 50 |
+
|
| 51 |
+
# ========== STREAMLIT UI ==========
|
| 52 |
+
st.set_page_config(
|
| 53 |
+
page_title="AI Sentiment Analyzer",
|
| 54 |
+
page_icon="π",
|
| 55 |
+
layout="wide",
|
| 56 |
+
initial_sidebar_state="expanded"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
st.markdown(
|
| 60 |
+
"""
|
| 61 |
+
<style>
|
| 62 |
+
body {
|
| 63 |
+
background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
|
| 64 |
+
color: white;
|
| 65 |
+
}
|
| 66 |
+
.stButton button {
|
| 67 |
+
background: linear-gradient(45deg, #ff6b6b, #f7b733);
|
| 68 |
+
color: white;
|
| 69 |
+
font-weight: bold;
|
| 70 |
+
border-radius: 12px;
|
| 71 |
+
padding: 10px 20px;
|
| 72 |
+
transition: 0.3s;
|
| 73 |
+
}
|
| 74 |
+
.stButton button:hover {
|
| 75 |
+
transform: scale(1.05);
|
| 76 |
+
background: linear-gradient(45deg, #36d1dc, #5b86e5);
|
| 77 |
+
}
|
| 78 |
+
.moving {
|
| 79 |
+
animation: float 3s ease-in-out infinite;
|
| 80 |
+
}
|
| 81 |
+
@keyframes float {
|
| 82 |
+
0% { transform: translatey(0px); }
|
| 83 |
+
50% { transform: translatey(-10px); }
|
| 84 |
+
100% { transform: translatey(0px); }
|
| 85 |
+
}
|
| 86 |
+
</style>
|
| 87 |
+
""",
|
| 88 |
+
unsafe_allow_html=True
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
st.title("π Social Media Sentiment Analyzer")
|
| 92 |
+
st.markdown("### Stream posts β’ Analyze moods β’ Visualize trends with animations")
|
| 93 |
+
|
| 94 |
+
# Sidebar
|
| 95 |
+
st.sidebar.header("βοΈ Controls")
|
| 96 |
+
hashtag = st.sidebar.text_input("Enter Hashtag", "#gla")
|
| 97 |
+
num_posts = st.sidebar.slider("Number of Posts", 5, 50, 20)
|
| 98 |
+
vis_type = st.sidebar.selectbox("Choose Visualization", ["Bar", "Pie", "Line"])
|
| 99 |
+
|
| 100 |
+
if st.sidebar.button("π Run Analysis"):
|
| 101 |
+
with st.spinner("Generating posts and analyzing sentiments..."):
|
| 102 |
+
posts, source = generate_posts(hashtag, num_posts)
|
| 103 |
+
df = analyze_posts(posts)
|
| 104 |
+
|
| 105 |
+
st.success(f"β
Posts generated by **{source}** | Total: {len(posts)}")
|
| 106 |
+
|
| 107 |
+
st.dataframe(df, use_container_width=True)
|
| 108 |
+
|
| 109 |
+
# ===== VISUALIZATIONS =====
|
| 110 |
+
if vis_type == "Bar":
|
| 111 |
+
fig = px.bar(df, x="Sentiment", color="Sentiment", title=f"Sentiment Distribution for {hashtag}", animation_frame=None)
|
| 112 |
+
elif vis_type == "Pie":
|
| 113 |
+
fig = px.pie(df, names="Sentiment", title=f"Sentiment Share for {hashtag}", hole=0.4)
|
| 114 |
+
else:
|
| 115 |
+
fig = px.line(df, y="Confidence", title=f"Confidence Trend for {hashtag}")
|
| 116 |
+
|
| 117 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 118 |
+
|
| 119 |
+
# ===== Moving 3D-like floating animation =====
|
| 120 |
+
st.markdown(
|
| 121 |
+
f"""
|
| 122 |
+
<div class="moving" style="font-size:24px; text-align:center; margin-top:30px;">
|
| 123 |
+
π π₯ β€οΈ π Trending Vibes around <b>{hashtag}</b>
|
| 124 |
+
</div>
|
| 125 |
+
""",
|
| 126 |
+
unsafe_allow_html=True
|
|
|
|
|
|
|
| 127 |
)
|
|
|
|
|
|
|
|
|
|
|
|