Sigrid De los Santos
adding new tabs to the project as per Theo feedback
3778f9f
raw
history blame
8.82 kB
import os
import sys
import time
import itertools
import streamlit as st
import pandas as pd
from io import StringIO
# Add 'src' to Python path so we can import main.py
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
from main import run_pipeline # We will update this to return report, articles_df, insights_df
# --- Page Config ---
st.set_page_config(page_title="πŸ“° AI News Analyzer", layout="wide")
st.title("🧠 AI-Powered Investing News Analyzer")
# --- API Key Inputs ---
st.subheader("πŸ” API Keys")
openai_api_key = st.text_input("OpenAI API Key", type="password").strip()
tavily_api_key = st.text_input("Tavily API Key", type="password").strip()
# --- Topics ---
st.subheader("πŸ“ˆ Topics of Interest")
topics_data = []
with st.form("topics_form"):
topic_count = st.number_input("How many topics?", min_value=1, max_value=10, value=1, step=1)
for i in range(topic_count):
col1, col2 = st.columns(2)
with col1:
topic = st.text_input(f"Topic {i+1}", key=f"topic_{i}")
with col2:
timespan = st.number_input(f"Timespan (days) for Topic {i+1}", min_value=1, max_value=30, value=7, step=1, key=f"timespan_{i}")
topics_data.append((topic, timespan))
run_button = st.form_submit_button("πŸš€ Run Analysis")
# --- Placeholder for logs ---
log_placeholder = st.empty()
# --- Results Tabs ---
tabs = st.tabs(["πŸ“ Report", "πŸ“‹ Articles", "πŸ“Š Insights"])
if run_button:
if not openai_api_key or not tavily_api_key:
st.error("Please provide both OpenAI and Tavily API keys.")
else:
run_button = False # Disable button
log_placeholder.info("πŸ” Starting analysis...")
# Rotating status messages
status_placeholder = st.empty()
steps = ["Running Tavily search...", "Analyzing with FinBERT & FinGPT...", "Generating LLM summary..."]
cycle = itertools.cycle(steps)
# Display rotating messages while running pipeline
with st.spinner("Working..."):
for _ in range(3):
status_placeholder.text(next(cycle))
time.sleep(0.8)
# Run the pipeline
try:
report_md, articles_df, insights_df = run_pipeline(
topics=topics_data,
openai_api_key=openai_api_key,
tavily_api_key=tavily_api_key
)
log_placeholder.success("βœ… Analysis completed.")
except Exception as e:
log_placeholder.error(f"❌ Error: {e}")
st.stop()
# --- Report Tab ---
with tabs[0]:
st.subheader("πŸ“ AI-Generated Report")
st.markdown(report_md, unsafe_allow_html=True)
# --- Articles Tab ---
with tabs[1]:
st.subheader("πŸ“‹ Articles & Priorities")
if not articles_df.empty:
# Color code priority
articles_df['Priority'] = articles_df['Priority'].map(
lambda x: "πŸ”΄ High" if str(x).lower() == "haute" or str(x).lower() == "high" else "🟒 Low"
)
st.dataframe(articles_df, use_container_width=True)
# CSV download
csv = articles_df.to_csv(index=False)
st.download_button("⬇️ Download Articles CSV", data=csv, file_name="articles.csv", mime="text/csv")
else:
st.warning("No articles found.")
# --- Insights Tab ---
with tabs[2]:
st.subheader("πŸ“Š Company Insights (FinGPT + FinBERT)")
if insights_df is not None and not insights_df.empty:
st.dataframe(insights_df, use_container_width=True)
csv = insights_df.to_csv(index=False)
st.download_button("⬇️ Download Insights CSV", data=csv, file_name="insights.csv", mime="text/csv")
else:
st.info("No company insights generated yet.")
# import os
# import sys
# import tempfile
# import time
# import itertools
# import streamlit as st
# import pandas as pd
# from threading import Thread
# from io import StringIO
# # Add 'src' to Python path so we can import main.py
# sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
# from main import run_pipeline
# st.set_page_config(page_title="πŸ“° AI News Analyzer", layout="wide")
# st.title("🧠 AI-Powered Investing News Analyzer")
# # === API Key Input ===
# st.subheader("πŸ” API Keys")
# openai_api_key = st.text_input("OpenAI API Key", type="password").strip()
# tavily_api_key = st.text_input("Tavily API Key", type="password").strip()
# # === Topic Input ===
# st.subheader("πŸ“ˆ Topics of Interest")
# topics_data = []
# with st.form("topics_form"):
# topic_count = st.number_input("How many topics?", min_value=1, max_value=10, value=1, step=1)
# for i in range(topic_count):
# col1, col2 = st.columns(2)
# with col1:
# topic = st.text_input(f"Topic {i+1}", key=f"topic_{i}")
# with col2:
# days = st.number_input(f"Timespan (days)", min_value=1, max_value=30, value=7, key=f"days_{i}")
# topics_data.append({"topic": topic, "timespan_days": days})
# submitted = st.form_submit_button("Run Analysis")
# # === Submission logic ===
# if submitted:
# if not openai_api_key or not tavily_api_key or not all([td['topic'] for td in topics_data]):
# st.warning("Please fill in all fields.")
# else:
# os.environ["OPENAI_API_KEY"] = openai_api_key
# os.environ["TAVILY_API_KEY"] = tavily_api_key
# df = pd.DataFrame(topics_data)
# with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp_csv:
# df.to_csv(tmp_csv.name, index=False)
# csv_path = tmp_csv.name
# # === UI Elements ===
# spinner_box = st.empty() # For rotating messages
# log_box = st.empty() # For logs
# logs = []
# rotating = True
# def log(msg):
# logs.append(msg)
# log_box.code("\n".join(logs))
# # === Rotating UI Messages ===
# def rotating_messages():
# messages = itertools.cycle([
# "πŸ” Searching financial news...",
# "🧠 Running language models...",
# "πŸ“Š Analyzing investor sentiment...",
# "πŸ“ Summarizing key takeaways...",
# "πŸ’Ή Building markdown reports..."
# ])
# while rotating:
# spinner_box.markdown(f"⏳ {next(messages)}")
# time.sleep(1.5)
# rotator_thread = Thread(target=rotating_messages)
# rotator_thread.start()
# try:
# # Check API Keys
# import openai
# openai.OpenAI(api_key=openai_api_key).models.list()
# log("βœ… OpenAI API key is valid.")
# import requests
# tavily_test = requests.post(
# "https://api.tavily.com/search",
# headers={"Authorization": f"Bearer {tavily_api_key}"},
# json={"query": "test", "days": 1, "max_results": 1}
# )
# if tavily_test.status_code == 200:
# log("βœ… Tavily API key is valid.")
# else:
# raise ValueError(f"Tavily error: {tavily_test.status_code} - {tavily_test.text}")
# # Run the full pipeline
# log("πŸš€ Running analysis pipeline...")
# output_path = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
# rotating = False
# rotator_thread.join()
# spinner_box.success("βœ… Analysis complete!")
# if output_path and isinstance(output_path, list):
# for path in output_path:
# if os.path.exists(path):
# with open(path, 'r', encoding='utf-8') as file:
# html_content = file.read()
# filename = os.path.basename(path)
# st.download_button(
# label=f"πŸ“₯ Download {filename}",
# data=html_content,
# file_name=filename,
# mime="text/html"
# )
# st.components.v1.html(html_content, height=600, scrolling=True)
# else:
# st.error("❌ No reports were generated.")
# except Exception as e:
# rotating = False
# rotator_thread.join()
# spinner_box.error("❌ Failed.")
# log_box.error(f"❌ Error: {e}")