Spaces:
Runtime error
Runtime error
Sigrid De los Santos
commited on
Commit
·
48a2803
1
Parent(s):
ff72975
debugging for analysis tables
Browse files- app.py +41 -49
- src/main.py +13 -1
- src/news_analysis.py +1 -1
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 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'))
|
|
@@ -34,12 +34,13 @@ with st.form("topics_form"):
|
|
| 34 |
submitted = st.form_submit_button("Run Analysis")
|
| 35 |
|
| 36 |
# === Tabs Setup ===
|
| 37 |
-
tab_report, tab_articles, tab_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 |
articles_df = pd.DataFrame()
|
| 44 |
insights_df = pd.DataFrame()
|
| 45 |
html_paths = []
|
|
@@ -52,6 +53,7 @@ if submitted:
|
|
| 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,8 +62,9 @@ if submitted:
|
|
| 60 |
log_box.code("\n".join(logs))
|
| 61 |
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
try:
|
| 66 |
client = openai.OpenAI(api_key=openai_api_key)
|
| 67 |
client.models.list()
|
|
@@ -70,6 +73,7 @@ if submitted:
|
|
| 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,56 +89,49 @@ if submitted:
|
|
| 85 |
log(f"❌ Tavily API Key Error: {e}")
|
| 86 |
st.stop()
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
st.success("✅ Analysis complete!")
|
| 93 |
|
| 94 |
# === Report Tab ===
|
|
|
|
|
|
|
|
|
|
| 95 |
with tab_report:
|
| 96 |
if html_paths:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 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 |
|
| 118 |
-
# === Articles Tab ===
|
| 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
|
| 128 |
-
data=
|
| 129 |
-
file_name=
|
| 130 |
-
mime="text/
|
| 131 |
)
|
|
|
|
|
|
|
|
|
|
| 132 |
else:
|
| 133 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# === Insights Tab ===
|
| 136 |
with tab_insights:
|
| 137 |
-
st.subheader("📊
|
| 138 |
if not insights_df.empty:
|
| 139 |
st.dataframe(insights_df, use_container_width=True)
|
| 140 |
st.download_button(
|
|
@@ -146,13 +143,8 @@ if submitted:
|
|
| 146 |
else:
|
| 147 |
st.info("No insights available.")
|
| 148 |
|
| 149 |
-
# === Debug Tab ===
|
| 150 |
-
with tab_debug:
|
| 151 |
-
st.subheader("🛠 Debug Log")
|
| 152 |
-
st.code("\n".join(logs) if logs else "No logs yet.")
|
| 153 |
-
|
| 154 |
except Exception as e:
|
| 155 |
-
|
| 156 |
log_box.error(f"❌ Error: {e}")
|
| 157 |
|
| 158 |
|
|
|
|
| 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'))
|
|
|
|
| 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 = []
|
|
|
|
| 53 |
df.to_csv(tmp_csv.name, index=False)
|
| 54 |
csv_path = tmp_csv.name
|
| 55 |
|
| 56 |
+
spinner_box = st.empty()
|
| 57 |
log_box = st.empty()
|
| 58 |
logs = []
|
| 59 |
|
|
|
|
| 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()
|
|
|
|
| 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",
|
|
|
|
| 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:
|
| 134 |
+
st.subheader("📊 Investment Insights")
|
| 135 |
if not insights_df.empty:
|
| 136 |
st.dataframe(insights_df, use_container_width=True)
|
| 137 |
st.download_button(
|
|
|
|
| 143 |
else:
|
| 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 |
|
| 150 |
|
src/main.py
CHANGED
|
@@ -26,6 +26,16 @@ def derive_priority(sentiment, confidence):
|
|
| 26 |
return "Medium"
|
| 27 |
return "Low"
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# === Main Analysis ===
|
| 30 |
def run_value_investing_analysis(csv_path, progress_callback=None):
|
| 31 |
current_df = pd.read_csv(csv_path)
|
|
@@ -99,10 +109,12 @@ def run_value_investing_analysis(csv_path, progress_callback=None):
|
|
| 99 |
|
| 100 |
try:
|
| 101 |
report_body = generate_value_investor_report(topic, news)
|
|
|
|
|
|
|
| 102 |
filename = f"{topic.replace(' ', '_').lower()}_{datetime.now().strftime('%Y-%m-%d')}.md"
|
| 103 |
filepath = os.path.join(DATA_DIR, filename)
|
| 104 |
with open(filepath, "w", encoding="utf-8") as f:
|
| 105 |
-
f.write(
|
| 106 |
except Exception as e:
|
| 107 |
if progress_callback:
|
| 108 |
progress_callback(f"[REPORT ERROR] {e}")
|
|
|
|
| 26 |
return "Medium"
|
| 27 |
return "Low"
|
| 28 |
|
| 29 |
+
# === Metrics Box ===
|
| 30 |
+
def build_metrics_box(topic, num_articles):
|
| 31 |
+
now = datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 32 |
+
return f"""
|
| 33 |
+
> **Topic:** `{topic}`
|
| 34 |
+
> **Articles Collected:** `{num_articles}`
|
| 35 |
+
> **Generated:** `{now}`
|
| 36 |
+
---
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
# === Main Analysis ===
|
| 40 |
def run_value_investing_analysis(csv_path, progress_callback=None):
|
| 41 |
current_df = pd.read_csv(csv_path)
|
|
|
|
| 109 |
|
| 110 |
try:
|
| 111 |
report_body = generate_value_investor_report(topic, news)
|
| 112 |
+
metrics_md = build_metrics_box(topic, len(news))
|
| 113 |
+
full_md = metrics_md + report_body
|
| 114 |
filename = f"{topic.replace(' ', '_').lower()}_{datetime.now().strftime('%Y-%m-%d')}.md"
|
| 115 |
filepath = os.path.join(DATA_DIR, filename)
|
| 116 |
with open(filepath, "w", encoding="utf-8") as f:
|
| 117 |
+
f.write(full_md)
|
| 118 |
except Exception as e:
|
| 119 |
if progress_callback:
|
| 120 |
progress_callback(f"[REPORT ERROR] {e}")
|
src/news_analysis.py
CHANGED
|
@@ -298,7 +298,7 @@ Your goal is to uncover:
|
|
| 298 |
|
| 299 |
Write a markdown memo with:
|
| 300 |
1. **Key Value Signals**
|
| 301 |
-
2. **Stocks or Startups to Watch**
|
| 302 |
3. **What Smart Money Might Be Acting On**
|
| 303 |
4. **References**
|
| 304 |
5. **Investment Hypothesis**
|
|
|
|
| 298 |
|
| 299 |
Write a markdown memo with:
|
| 300 |
1. **Key Value Signals**
|
| 301 |
+
2. **Stocks or Startups to Watch** -- Add on each of those the Price-to-Earnings Ratio · Price-to-Book Ratio · Debt-to-Equity Ratio · Free Cash Flow · PEG Ratio · fore each, this is very important
|
| 302 |
3. **What Smart Money Might Be Acting On**
|
| 303 |
4. **References**
|
| 304 |
5. **Investment Hypothesis**
|