akashjayampu commited on
Commit
e48855a
Β·
verified Β·
1 Parent(s): 9e62dbe

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +50 -33
src/streamlit_app.py CHANGED
@@ -1,35 +1,52 @@
1
- import streamlit as st
2
  import os
 
 
 
 
3
  from google_search import google_search
4
- from mistral_llm import analyze_text
5
- from utils import get_embeddings, build_index, search_similar
6
-
7
- st.set_page_config(page_title="Brand Insight Companion", layout="wide")
8
- st.title("πŸ” Real-Time Brand Insight Companion")
9
-
10
- query = st.text_input("Enter a brand or keyword (e.g., 'Nvidia')")
11
- if query:
12
- with st.spinner("πŸ”Ž Searching social media posts..."):
13
- posts = google_search(query, num=7)
14
-
15
- if not posts:
16
- st.warning("❌ No posts found.")
17
- else:
18
- st.success(f"βœ… Fetched {len(posts)} posts.")
19
- summaries = [analyze_text(p[:1024]) for p in posts]
20
-
21
- for i, summary in enumerate(summaries):
22
- st.subheader(f"Post {i+1}")
23
- st.markdown(summary)
24
-
25
- st.markdown("---")
26
- sim_query = st.text_input("Find similar posts to this topic")
27
- if sim_query:
28
- embeddings = get_embeddings(posts)
29
- index = build_index(embeddings)
30
- query_embedding = get_embeddings([sim_query])
31
- dists, idxs = search_similar(index, query_embedding)
32
-
33
- st.subheader("πŸ” Similar Posts")
34
- for i in idxs[0]:
35
- st.markdown(f"- {posts[i]}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import time
3
+ import streamlit as st
4
+ from datetime import datetime
5
+ from dotenv import load_dotenv
6
  from google_search import google_search
7
+ from mistral_llm import summarize_texts, detect_fake_news, analyze_sentiment
8
+
9
+ load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), '..', '.env'))
10
+
11
+ st.set_page_config(page_title="πŸ”₯ Brand Crisis Detector", layout="wide", page_icon="πŸ”₯")
12
+ st.title("πŸ”₯ Real-Time Brand Crisis Detector")
13
+ st.markdown("Analyze web content about your brand in real-time using AI ⚑")
14
+ st.caption(f"πŸ•’ Last refreshed: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
15
+
16
+ query = st.text_input("πŸ” Enter a brand or keyword", placeholder="e.g., Nvidia, Nestle, Jio", label_visibility="visible")
17
+
18
+ if st.button("Start Analysis πŸš€") or (query and st.session_state.get("last_query") != query):
19
+ st.session_state.last_query = query
20
+
21
+ with st.spinner(f"πŸ”Ž Searching Google for **{query}**..."):
22
+ t1 = time.time()
23
+ articles = google_search(query, num_results=10)
24
+ fetch_time = round(time.time() - t1, 2)
25
+
26
+ if not articles:
27
+ st.warning("❌ No results found. Try a different query.")
28
+ st.stop()
29
+
30
+ st.success(f"βœ… Fetched {len(articles)} articles in {fetch_time} seconds.")
31
+
32
+ titles = [a['title'] for a in articles]
33
+ links = [a['link'] for a in articles]
34
+ contents = [a['snippet'] for a in articles]
35
+
36
+ with st.spinner("🧠 Summarizing, classifying, and detecting fake news..."):
37
+ t2 = time.time()
38
+ summaries = summarize_texts(contents)
39
+ sentiments = analyze_sentiment(contents)
40
+ fakeness = detect_fake_news(contents)
41
+ process_time = round(time.time() - t2, 2)
42
+
43
+ st.info(f"⏱️ AI analysis completed in {process_time} seconds.")
44
+
45
+ for i in range(len(articles)):
46
+ with st.container():
47
+ st.subheader(f"{i+1}. {titles[i]}")
48
+ st.markdown(f"**πŸ”— Link:** [{links[i]}]({links[i]})")
49
+ st.markdown(f"**πŸ’¬ Sentiment:** `{sentiments[i]}`")
50
+ st.markdown(f"**πŸ•΅οΈ Fake News Score:** `{fakeness[i]}`")
51
+ st.markdown(f"**πŸ“ Summary:** {summaries[i]}")
52
+ st.markdown("---")