Sigrid De los Santos commited on
Commit
c319a1e
Β·
1 Parent(s): 4b24cc9

App is ready

Browse files
Files changed (2) hide show
  1. app.py +53 -25
  2. src/main.py +2 -2
app.py CHANGED
@@ -2,10 +2,12 @@ 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'))
@@ -56,24 +58,40 @@ if submitted:
56
  spinner_box = st.empty()
57
  log_box = st.empty()
58
  logs = []
 
59
 
60
  def log(msg):
61
  logs.append(msg)
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",
@@ -84,50 +102,58 @@ if submitted:
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
-
98
-
99
- # === Articles Tab ===
100
  with tab_report:
 
101
  if html_paths:
102
- # Add one button to download the latest report
103
  latest_report = html_paths[-1]
104
  with open(latest_report, 'r', encoding='utf-8') as f:
105
  html_content = f.read()
106
 
 
107
  st.download_button(
108
- label=f"⬇️ Download {os.path.basename(latest_report)}",
109
  data=html_content,
110
  file_name=os.path.basename(latest_report),
111
  mime="text/html"
112
  )
113
 
114
- # Display the report
115
  st.components.v1.html(html_content, height=600, scrolling=True)
116
  else:
117
  st.error("❌ No reports were generated.")
118
- # with tab_articles:
119
- # st.subheader("πŸ“‹ Articles Table")
120
- # if not articles_df.empty:
121
- # st.dataframe(articles_df[["Title", "URL", "Summary", "Priority", "Date", "Sentiment", "Confidence", "Signal"]],
122
- # use_container_width=True)
123
- # st.download_button(
124
- # label="⬇️ Download Articles CSV",
125
- # data=articles_df.to_csv(index=False).encode("utf-8"),
126
- # file_name="articles.csv",
127
- # mime="text/csv"
128
- # )
129
- # else:
130
- # st.info("No articles available.")
 
 
 
 
131
 
132
  # === Insights Tab ===
133
  with tab_insights:
@@ -144,6 +170,8 @@ if submitted:
144
  st.info("No insights available.")
145
 
146
  except Exception as e:
 
 
147
  spinner_box.error("❌ Failed.")
148
  log_box.error(f"❌ Error: {e}")
149
 
 
2
  import sys
3
  import tempfile
4
  import time
5
+ import itertools
6
  import streamlit as st
7
  import pandas as pd
8
  import requests
9
  import openai
10
+ from threading import Thread
11
 
12
  # Add 'src' to Python path
13
  sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
 
58
  spinner_box = st.empty()
59
  log_box = st.empty()
60
  logs = []
61
+ rotating = True
62
 
63
  def log(msg):
64
  logs.append(msg)
65
  log_box.code("\n".join(logs))
66
 
67
+ # === Rotating UI Messages ===
68
+ def rotating_messages():
69
+ messages = itertools.cycle([
70
+ "πŸ” Searching financial news...",
71
+ "🧠 Running AI analysis...",
72
+ "πŸ“Š Evaluating sentiment...",
73
+ "πŸ“ Generating report...",
74
+ "πŸ’Ή Finalizing insights..."
75
+ ])
76
+ while rotating:
77
+ spinner_box.markdown(f"⏳ {next(messages)}")
78
+ time.sleep(1.5)
79
+
80
+ rotator_thread = Thread(target=rotating_messages)
81
+ rotator_thread.start()
82
 
83
+ try:
84
+ # Check API Keys
85
  try:
86
  client = openai.OpenAI(api_key=openai_api_key)
87
  client.models.list()
88
  log("βœ… OpenAI API key is valid.")
89
  except Exception as e:
90
  log(f"❌ OpenAI API Key Error: {e}")
91
+ rotating = False
92
+ rotator_thread.join()
93
  st.stop()
94
 
 
95
  try:
96
  response = requests.post(
97
  "https://api.tavily.com/search",
 
102
  log("βœ… Tavily API key is valid.")
103
  else:
104
  log(f"❌ Tavily Key Error: {response.status_code} {response.text}")
105
+ rotating = False
106
+ rotator_thread.join()
107
  st.stop()
108
  except Exception as e:
109
  log(f"❌ Tavily API Key Error: {e}")
110
+ rotating = False
111
+ rotator_thread.join()
112
  st.stop()
113
 
114
+ with st.spinner("⏳ Running analysis..."):
115
+ html_paths, articles_df, insights_df = run_pipeline(csv_path, tavily_api_key, progress_callback=log)
116
+
117
+ rotating = False
118
+ rotator_thread.join()
119
  spinner_box.success("βœ… Analysis complete!")
120
 
121
  # === Report Tab ===
 
 
 
122
  with tab_report:
123
+ st.subheader("πŸ“ Latest Report")
124
  if html_paths:
 
125
  latest_report = html_paths[-1]
126
  with open(latest_report, 'r', encoding='utf-8') as f:
127
  html_content = f.read()
128
 
129
+ # Download button for HTML report
130
  st.download_button(
131
+ label="⬇️ Download Report (HTML)",
132
  data=html_content,
133
  file_name=os.path.basename(latest_report),
134
  mime="text/html"
135
  )
136
 
 
137
  st.components.v1.html(html_content, height=600, scrolling=True)
138
  else:
139
  st.error("❌ No reports were generated.")
140
+
141
+ # === Articles Tab ===
142
+ with tab_articles:
143
+ st.subheader("πŸ“‹ Articles Table")
144
+ if not articles_df.empty:
145
+ st.dataframe(
146
+ articles_df[["Title", "URL", "Priority", "Sentiment", "Confidence", "Signal", "Date"]],
147
+ use_container_width=True
148
+ )
149
+ st.download_button(
150
+ label="⬇️ Download Articles CSV",
151
+ data=articles_df.to_csv(index=False).encode("utf-8"),
152
+ file_name="articles.csv",
153
+ mime="text/csv"
154
+ )
155
+ else:
156
+ st.info("No articles available.")
157
 
158
  # === Insights Tab ===
159
  with tab_insights:
 
170
  st.info("No insights available.")
171
 
172
  except Exception as e:
173
+ rotating = False
174
+ rotator_thread.join()
175
  spinner_box.error("❌ Failed.")
176
  log_box.error(f"❌ Error: {e}")
177
 
src/main.py CHANGED
@@ -45,8 +45,8 @@ def run_value_investing_analysis(csv_path, progress_callback=None):
45
  for _, row in current_df.iterrows():
46
  topic = row.get("topic")
47
  timespan = row.get("timespan_days", 7)
48
- if progress_callback:
49
- progress_callback(f"πŸ” Processing topic: {topic} ({timespan} days)")
50
  # try:
51
  # news = fetch_deep_news(topic, timespan)
52
  # if progress_callback:
 
45
  for _, row in current_df.iterrows():
46
  topic = row.get("topic")
47
  timespan = row.get("timespan_days", 7)
48
+ # if progress_callback:
49
+ # progress_callback(f"πŸ” Processing topic: {topic} ({timespan} days)")
50
  # try:
51
  # news = fetch_deep_news(topic, timespan)
52
  # if progress_callback: