Sigrid De los Santos commited on
Commit
31df7d3
Β·
1 Parent(s): 0858d17
Files changed (1) hide show
  1. app.py +56 -9
app.py CHANGED
@@ -1,8 +1,11 @@
1
  import os
2
  import sys
3
  import tempfile
 
4
  import streamlit as st
5
  import pandas as pd
 
 
6
 
7
  # Add 'src' to Python path
8
  sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
@@ -11,12 +14,12 @@ from main import run_pipeline
11
  st.set_page_config(page_title="πŸ“° AI News Analyzer", layout="wide")
12
  st.title("🧠 AI-Powered Investing News Analyzer")
13
 
14
- # --- API Keys ---
15
  st.subheader("πŸ” API Keys")
16
  openai_api_key = st.text_input("OpenAI API Key", type="password").strip()
17
  tavily_api_key = st.text_input("Tavily API Key", type="password").strip()
18
 
19
- # --- Topics ---
20
  st.subheader("πŸ“ˆ Topics of Interest")
21
  topics_data = []
22
  with st.form("topics_form"):
@@ -30,18 +33,21 @@ with st.form("topics_form"):
30
  topics_data.append({"topic": topic, "timespan_days": days})
31
  submitted = st.form_submit_button("Run Analysis")
32
 
33
- # --- Tabs ---
34
  tab_report, tab_articles, tab_insights = st.tabs(["πŸ“ Report", "πŸ“‹ Articles", "πŸ“Š Insights"])
35
- articles_df = pd.DataFrame()
36
- insights_df = pd.DataFrame()
37
- html_paths = []
38
 
39
  if submitted:
40
  if not openai_api_key or not tavily_api_key or not all([td['topic'] for td in topics_data]):
41
  st.warning("Please fill in all fields.")
42
  else:
 
 
 
 
 
43
  os.environ["OPENAI_API_KEY"] = openai_api_key
44
  os.environ["TAVILY_API_KEY"] = tavily_api_key
 
45
  df = pd.DataFrame(topics_data)
46
  with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp_csv:
47
  df.to_csv(tmp_csv.name, index=False)
@@ -56,11 +62,38 @@ if submitted:
56
  log_box.code("\n".join(logs))
57
 
58
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  spinner_box.markdown("⏳ Running analysis pipeline...")
60
  html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
61
  spinner_box.success("βœ… Analysis complete!")
62
 
63
- # Report Tab
64
  with tab_report:
65
  if html_paths:
66
  for path in html_paths:
@@ -70,18 +103,32 @@ if submitted:
70
  else:
71
  st.error("❌ No reports were generated.")
72
 
73
- # Articles Tab
74
  with tab_articles:
 
75
  if not articles_df.empty:
76
  st.dataframe(articles_df[["Title", "URL", "Summary", "Priority", "Date"]],
77
  use_container_width=True)
 
 
 
 
 
 
78
  else:
79
  st.info("No articles available.")
80
 
81
- # Insights Tab
82
  with tab_insights:
 
83
  if not insights_df.empty:
84
  st.dataframe(insights_df, use_container_width=True)
 
 
 
 
 
 
85
  else:
86
  st.info("No insights available.")
87
 
 
1
  import os
2
  import sys
3
  import tempfile
4
+ import time
5
  import streamlit as st
6
  import pandas as pd
7
+ import requests
8
+ import openai
9
 
10
  # Add 'src' to Python path
11
  sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
 
14
  st.set_page_config(page_title="πŸ“° AI News Analyzer", layout="wide")
15
  st.title("🧠 AI-Powered Investing News Analyzer")
16
 
17
+ # === API Key Input ===
18
  st.subheader("πŸ” API Keys")
19
  openai_api_key = st.text_input("OpenAI API Key", type="password").strip()
20
  tavily_api_key = st.text_input("Tavily API Key", type="password").strip()
21
 
22
+ # === Topic Input ===
23
  st.subheader("πŸ“ˆ Topics of Interest")
24
  topics_data = []
25
  with st.form("topics_form"):
 
33
  topics_data.append({"topic": topic, "timespan_days": days})
34
  submitted = st.form_submit_button("Run Analysis")
35
 
36
+ # === Tabs Setup ===
37
  tab_report, tab_articles, tab_insights = st.tabs(["πŸ“ Report", "πŸ“‹ Articles", "πŸ“Š Insights"])
 
 
 
38
 
39
  if submitted:
40
  if not openai_api_key or not tavily_api_key or not all([td['topic'] for td in topics_data]):
41
  st.warning("Please fill in all fields.")
42
  else:
43
+ # Reset old results
44
+ articles_df = pd.DataFrame()
45
+ insights_df = pd.DataFrame()
46
+ html_paths = []
47
+
48
  os.environ["OPENAI_API_KEY"] = openai_api_key
49
  os.environ["TAVILY_API_KEY"] = tavily_api_key
50
+
51
  df = pd.DataFrame(topics_data)
52
  with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp_csv:
53
  df.to_csv(tmp_csv.name, index=False)
 
62
  log_box.code("\n".join(logs))
63
 
64
  try:
65
+ spinner_box.markdown("⏳ Checking API keys...")
66
+
67
+ # === Check OpenAI Key ===
68
+ try:
69
+ client = openai.OpenAI(api_key=openai_api_key)
70
+ client.models.list()
71
+ log("βœ… OpenAI API key is valid.")
72
+ except Exception as e:
73
+ log(f"❌ OpenAI API Key Error: {e}")
74
+ st.stop()
75
+
76
+ # === Check Tavily Key ===
77
+ try:
78
+ response = requests.post(
79
+ "https://api.tavily.com/search",
80
+ headers={"Authorization": f"Bearer {tavily_api_key}"},
81
+ json={"query": "test", "days": 1, "max_results": 1}
82
+ )
83
+ if response.status_code == 200:
84
+ log("βœ… Tavily API key is valid.")
85
+ else:
86
+ log(f"❌ Tavily Key Error: {response.status_code} {response.text}")
87
+ st.stop()
88
+ except Exception as e:
89
+ log(f"❌ Tavily API Key Error: {e}")
90
+ st.stop()
91
+
92
  spinner_box.markdown("⏳ Running analysis pipeline...")
93
  html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
94
  spinner_box.success("βœ… Analysis complete!")
95
 
96
+ # === Report Tab ===
97
  with tab_report:
98
  if html_paths:
99
  for path in html_paths:
 
103
  else:
104
  st.error("❌ No reports were generated.")
105
 
106
+ # === Articles Tab ===
107
  with tab_articles:
108
+ st.subheader("πŸ“‹ Articles Table")
109
  if not articles_df.empty:
110
  st.dataframe(articles_df[["Title", "URL", "Summary", "Priority", "Date"]],
111
  use_container_width=True)
112
+ st.download_button(
113
+ label="⬇️ Download Articles CSV",
114
+ data=articles_df.to_csv(index=False).encode("utf-8"),
115
+ file_name="articles.csv",
116
+ mime="text/csv"
117
+ )
118
  else:
119
  st.info("No articles available.")
120
 
121
+ # === Insights Tab ===
122
  with tab_insights:
123
+ st.subheader("πŸ“Š Investment Insights")
124
  if not insights_df.empty:
125
  st.dataframe(insights_df, use_container_width=True)
126
+ st.download_button(
127
+ label="⬇️ Download Insights CSV",
128
+ data=insights_df.to_csv(index=False).encode("utf-8"),
129
+ file_name="insights.csv",
130
+ mime="text/csv"
131
+ )
132
  else:
133
  st.info("No insights available.")
134