Sigrid De los Santos commited on
Commit
3b0bca5
Β·
1 Parent(s): c1747ff

debugging for analysis tables

Browse files
Files changed (2) hide show
  1. app.py +87 -27
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,12 +1,13 @@
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
  sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
11
  from main import run_pipeline
12
 
@@ -51,7 +52,6 @@ if submitted:
51
  df.to_csv(tmp_csv.name, index=False)
52
  csv_path = tmp_csv.name
53
 
54
- spinner_box = st.empty()
55
  log_box = st.empty()
56
  logs = []
57
 
@@ -60,9 +60,8 @@ if submitted:
60
  log_box.code("\n".join(logs))
61
 
62
  try:
63
- spinner_box.markdown("⏳ Checking API keys...")
64
-
65
- # === Check OpenAI Key ===
66
  try:
67
  client = openai.OpenAI(api_key=openai_api_key)
68
  client.models.list()
@@ -71,7 +70,6 @@ if submitted:
71
  log(f"❌ OpenAI API Key Error: {e}")
72
  st.stop()
73
 
74
- # === Check Tavily Key ===
75
  try:
76
  response = requests.post(
77
  "https://api.tavily.com/search",
@@ -87,9 +85,11 @@ if submitted:
87
  log(f"❌ Tavily API Key Error: {e}")
88
  st.stop()
89
 
90
- spinner_box.markdown("⏳ Running analysis pipeline...")
91
- html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
92
- spinner_box.success("βœ… Analysis complete!")
 
 
93
 
94
  # === Report Tab ===
95
  with tab_report:
@@ -98,6 +98,20 @@ if submitted:
98
  with open(path, 'r', encoding='utf-8') as f:
99
  html_content = f.read()
100
  st.components.v1.html(html_content, height=600, scrolling=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  else:
102
  st.error("❌ No reports were generated.")
103
 
@@ -105,8 +119,10 @@ if submitted:
105
  with tab_articles:
106
  st.subheader("πŸ“‹ Articles Table")
107
  if not articles_df.empty:
108
- st.dataframe(articles_df[["Title", "URL", "Summary", "Priority", "Sentiment", "Confidence", "Signal", "Date"]],
109
- use_container_width=True)
 
 
110
  st.download_button(
111
  label="⬇️ Download Articles CSV",
112
  data=articles_df.to_csv(index=False).encode("utf-8"),
@@ -136,17 +152,19 @@ if submitted:
136
  st.code("\n".join(logs) if logs else "No logs yet.")
137
 
138
  except Exception as e:
139
- spinner_box.error("❌ Failed.")
140
  log_box.error(f"❌ Error: {e}")
141
 
142
 
143
  # import os
144
  # import sys
145
  # import tempfile
 
146
  # import streamlit as st
147
  # import pandas as pd
 
 
148
 
149
- # # Add 'src' to Python path so we can import main.py
150
  # sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
151
  # from main import run_pipeline
152
 
@@ -163,7 +181,6 @@ if submitted:
163
  # topics_data = []
164
  # with st.form("topics_form"):
165
  # topic_count = st.number_input("How many topics?", min_value=1, max_value=10, value=1, step=1)
166
-
167
  # for i in range(topic_count):
168
  # col1, col2 = st.columns(2)
169
  # with col1:
@@ -171,20 +188,19 @@ if submitted:
171
  # with col2:
172
  # days = st.number_input(f"Timespan (days)", min_value=1, max_value=30, value=7, key=f"days_{i}")
173
  # topics_data.append({"topic": topic, "timespan_days": days})
174
-
175
  # submitted = st.form_submit_button("Run Analysis")
176
 
177
  # # === Tabs Setup ===
178
- # tab_report, tab_articles, tab_insights = st.tabs(["πŸ“ Report", "πŸ“‹ Articles", "πŸ“Š Insights"])
179
- # articles_df = pd.DataFrame()
180
- # insights_df = pd.DataFrame()
181
- # html_paths = []
182
 
183
- # # === Submission logic ===
184
  # if submitted:
185
  # if not openai_api_key or not tavily_api_key or not all([td['topic'] for td in topics_data]):
186
  # st.warning("Please fill in all fields.")
187
  # else:
 
 
 
 
188
  # os.environ["OPENAI_API_KEY"] = openai_api_key
189
  # os.environ["TAVILY_API_KEY"] = tavily_api_key
190
 
@@ -202,14 +218,38 @@ if submitted:
202
  # log_box.code("\n".join(logs))
203
 
204
  # try:
205
- # spinner_box.markdown("⏳ Running analysis pipeline...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
- # # Run the full pipeline
208
  # html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
209
-
210
  # spinner_box.success("βœ… Analysis complete!")
211
 
212
- # # --- Report Tab ---
213
  # with tab_report:
214
  # if html_paths:
215
  # for path in html_paths:
@@ -219,20 +259,40 @@ if submitted:
219
  # else:
220
  # st.error("❌ No reports were generated.")
221
 
222
- # # --- Articles Tab ---
223
  # with tab_articles:
 
224
  # if not articles_df.empty:
225
- # st.dataframe(articles_df, use_container_width=True)
 
 
 
 
 
 
 
226
  # else:
227
  # st.info("No articles available.")
228
 
229
- # # --- Insights Tab ---
230
  # with tab_insights:
 
231
  # if not insights_df.empty:
232
  # st.dataframe(insights_df, use_container_width=True)
 
 
 
 
 
 
233
  # else:
234
  # st.info("No insights available.")
235
 
 
 
 
 
 
236
  # except Exception as e:
237
  # spinner_box.error("❌ Failed.")
238
  # log_box.error(f"❌ Error: {e}")
 
1
  import os
2
  import sys
3
  import tempfile
 
4
  import streamlit as st
5
  import pandas as pd
6
  import requests
7
  import openai
8
+ import pdfkit
9
 
10
+ # Add 'src' to Python path
11
  sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
12
  from main import run_pipeline
13
 
 
52
  df.to_csv(tmp_csv.name, index=False)
53
  csv_path = tmp_csv.name
54
 
 
55
  log_box = st.empty()
56
  logs = []
57
 
 
60
  log_box.code("\n".join(logs))
61
 
62
  try:
63
+ # === Check API Keys ===
64
+ st.info("πŸ”‘ **Checking API Keys...**")
 
65
  try:
66
  client = openai.OpenAI(api_key=openai_api_key)
67
  client.models.list()
 
70
  log(f"❌ OpenAI API Key Error: {e}")
71
  st.stop()
72
 
 
73
  try:
74
  response = requests.post(
75
  "https://api.tavily.com/search",
 
85
  log(f"❌ Tavily API Key Error: {e}")
86
  st.stop()
87
 
88
+ # === Run Analysis with Spinner ===
89
+ with st.spinner("⏳ **Analyzing topics, please wait...**"):
90
+ html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
91
+
92
+ st.success("βœ… Analysis complete!")
93
 
94
  # === Report Tab ===
95
  with tab_report:
 
98
  with open(path, 'r', encoding='utf-8') as f:
99
  html_content = f.read()
100
  st.components.v1.html(html_content, height=600, scrolling=True)
101
+
102
+ # Generate PDF from HTML
103
+ pdf_path = path.replace(".html", ".pdf")
104
+ try:
105
+ pdfkit.from_string(html_content, pdf_path)
106
+ with open(pdf_path, "rb") as pdf_file:
107
+ st.download_button(
108
+ label="⬇️ Download Report (PDF)",
109
+ data=pdf_file,
110
+ file_name=os.path.basename(pdf_path),
111
+ mime="application/pdf"
112
+ )
113
+ except Exception as e:
114
+ st.error(f"PDF generation failed: {e}")
115
  else:
116
  st.error("❌ No reports were generated.")
117
 
 
119
  with tab_articles:
120
  st.subheader("πŸ“‹ Articles Table")
121
  if not articles_df.empty:
122
+ st.dataframe(
123
+ articles_df[["Title", "URL", "Summary", "Priority", "Sentiment", "Confidence", "Signal", "Date"]],
124
+ use_container_width=True
125
+ )
126
  st.download_button(
127
  label="⬇️ Download Articles CSV",
128
  data=articles_df.to_csv(index=False).encode("utf-8"),
 
152
  st.code("\n".join(logs) if logs else "No logs yet.")
153
 
154
  except Exception as e:
155
+ st.error("❌ Failed.")
156
  log_box.error(f"❌ Error: {e}")
157
 
158
 
159
  # import os
160
  # import sys
161
  # import tempfile
162
+ # import time
163
  # import streamlit as st
164
  # import pandas as pd
165
+ # import requests
166
+ # import openai
167
 
 
168
  # sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
169
  # from main import run_pipeline
170
 
 
181
  # topics_data = []
182
  # with st.form("topics_form"):
183
  # topic_count = st.number_input("How many topics?", min_value=1, max_value=10, value=1, step=1)
 
184
  # for i in range(topic_count):
185
  # col1, col2 = st.columns(2)
186
  # with col1:
 
188
  # with col2:
189
  # days = st.number_input(f"Timespan (days)", min_value=1, max_value=30, value=7, key=f"days_{i}")
190
  # topics_data.append({"topic": topic, "timespan_days": days})
 
191
  # submitted = st.form_submit_button("Run Analysis")
192
 
193
  # # === Tabs Setup ===
194
+ # tab_report, tab_articles, tab_insights, tab_debug = st.tabs(["πŸ“ Report", "πŸ“‹ Articles", "πŸ“Š Insights", "πŸ›  Debug"])
 
 
 
195
 
 
196
  # if submitted:
197
  # if not openai_api_key or not tavily_api_key or not all([td['topic'] for td in topics_data]):
198
  # st.warning("Please fill in all fields.")
199
  # else:
200
+ # articles_df = pd.DataFrame()
201
+ # insights_df = pd.DataFrame()
202
+ # html_paths = []
203
+
204
  # os.environ["OPENAI_API_KEY"] = openai_api_key
205
  # os.environ["TAVILY_API_KEY"] = tavily_api_key
206
 
 
218
  # log_box.code("\n".join(logs))
219
 
220
  # try:
221
+ # spinner_box.markdown("⏳ Checking API keys...")
222
+
223
+ # # === Check OpenAI Key ===
224
+ # try:
225
+ # client = openai.OpenAI(api_key=openai_api_key)
226
+ # client.models.list()
227
+ # log("βœ… OpenAI API key is valid.")
228
+ # except Exception as e:
229
+ # log(f"❌ OpenAI API Key Error: {e}")
230
+ # st.stop()
231
+
232
+ # # === Check Tavily Key ===
233
+ # try:
234
+ # response = requests.post(
235
+ # "https://api.tavily.com/search",
236
+ # headers={"Authorization": f"Bearer {tavily_api_key}"},
237
+ # json={"query": "test", "days": 1, "max_results": 1}
238
+ # )
239
+ # if response.status_code == 200:
240
+ # log("βœ… Tavily API key is valid.")
241
+ # else:
242
+ # log(f"❌ Tavily Key Error: {response.status_code} {response.text}")
243
+ # st.stop()
244
+ # except Exception as e:
245
+ # log(f"❌ Tavily API Key Error: {e}")
246
+ # st.stop()
247
 
248
+ # spinner_box.markdown("⏳ Running analysis pipeline...")
249
  # html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
 
250
  # spinner_box.success("βœ… Analysis complete!")
251
 
252
+ # # === Report Tab ===
253
  # with tab_report:
254
  # if html_paths:
255
  # for path in html_paths:
 
259
  # else:
260
  # st.error("❌ No reports were generated.")
261
 
262
+ # # === Articles Tab ===
263
  # with tab_articles:
264
+ # st.subheader("πŸ“‹ Articles Table")
265
  # if not articles_df.empty:
266
+ # st.dataframe(articles_df[["Title", "URL", "Summary", "Priority", "Sentiment", "Confidence", "Signal", "Date"]],
267
+ # use_container_width=True)
268
+ # st.download_button(
269
+ # label="⬇️ Download Articles CSV",
270
+ # data=articles_df.to_csv(index=False).encode("utf-8"),
271
+ # file_name="articles.csv",
272
+ # mime="text/csv"
273
+ # )
274
  # else:
275
  # st.info("No articles available.")
276
 
277
+ # # === Insights Tab ===
278
  # with tab_insights:
279
+ # st.subheader("πŸ“Š Top Investment Insights")
280
  # if not insights_df.empty:
281
  # st.dataframe(insights_df, use_container_width=True)
282
+ # st.download_button(
283
+ # label="⬇️ Download Insights CSV",
284
+ # data=insights_df.to_csv(index=False).encode("utf-8"),
285
+ # file_name="insights.csv",
286
+ # mime="text/csv"
287
+ # )
288
  # else:
289
  # st.info("No insights available.")
290
 
291
+ # # === Debug Tab ===
292
+ # with tab_debug:
293
+ # st.subheader("πŸ›  Debug Log")
294
+ # st.code("\n".join(logs) if logs else "No logs yet.")
295
+
296
  # except Exception as e:
297
  # spinner_box.error("❌ Failed.")
298
  # log_box.error(f"❌ Error: {e}")
requirements.txt CHANGED
@@ -87,3 +87,4 @@ urllib3==2.5.0
87
  zipp==3.23.0
88
  zstandard==0.23.0
89
  xhtml2pdf
 
 
87
  zipp==3.23.0
88
  zstandard==0.23.0
89
  xhtml2pdf
90
+ pdfkit