Spaces:
Sleeping
Sleeping
Sigrid De los Santos
commited on
Commit
Β·
31df7d3
1
Parent(s):
0858d17
debugging
Browse files
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 |
-
#
|
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 |
-
#
|
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 |
-
#
|
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 |
|