Spaces:
Runtime error
Runtime error
Sigrid De los Santos
commited on
Commit
·
7d27314
1
Parent(s):
edbb216
fixing date issues
Browse files- src/news_analysis.py +87 -77
src/news_analysis.py
CHANGED
|
@@ -217,7 +217,6 @@ from datetime import datetime, timedelta # Ensure this is at the top
|
|
| 217 |
def fetch_deep_news(topic, days):
|
| 218 |
all_results = []
|
| 219 |
seen_urls = set()
|
| 220 |
-
cutoff_date = datetime.now() - timedelta(days=days)
|
| 221 |
|
| 222 |
base_queries = [
|
| 223 |
topic,
|
|
@@ -253,28 +252,13 @@ def fetch_deep_news(topic, days):
|
|
| 253 |
for item in response.get("results", []):
|
| 254 |
url = item.get("url")
|
| 255 |
content = item.get("content", "") or item.get("summary", "") or item.get("title", "")
|
| 256 |
-
if
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
if pub_date < cutoff_date:
|
| 264 |
-
continue # Skip articles too old
|
| 265 |
-
date_str = pub_date.strftime("%Y-%m-%d")
|
| 266 |
-
except Exception:
|
| 267 |
-
date_str = "Unknown"
|
| 268 |
-
else:
|
| 269 |
-
date_str = "Unknown"
|
| 270 |
-
|
| 271 |
-
all_results.append({
|
| 272 |
-
"title": item.get("title"),
|
| 273 |
-
"url": url,
|
| 274 |
-
"content": content,
|
| 275 |
-
"date": date_str
|
| 276 |
-
})
|
| 277 |
-
seen_urls.add(url)
|
| 278 |
|
| 279 |
except Exception as e:
|
| 280 |
print(f"⚠️ Tavily request failed for query '{query}': {e}")
|
|
@@ -282,59 +266,9 @@ def fetch_deep_news(topic, days):
|
|
| 282 |
print(f"📰 Total articles collected: {len(all_results)}")
|
| 283 |
return all_results
|
| 284 |
|
| 285 |
-
# def fetch_deep_news(topic, days):
|
| 286 |
-
# all_results = []
|
| 287 |
-
# seen_urls = set()
|
| 288 |
-
|
| 289 |
-
# base_queries = [
|
| 290 |
-
# topic,
|
| 291 |
-
# f"{topic} AND startup",
|
| 292 |
-
# f"{topic} AND acquisition OR merger OR funding",
|
| 293 |
-
# f"{topic} AND CEO OR executive OR leadership",
|
| 294 |
-
# f"{topic} AND venture capital OR Series A OR Series B",
|
| 295 |
-
# f"{topic} AND government grant OR approval OR contract",
|
| 296 |
-
# f"{topic} AND underrated OR small-cap OR micro-cap"
|
| 297 |
-
# ]
|
| 298 |
-
|
| 299 |
-
# investor_queries = [
|
| 300 |
-
# f"{topic} AND BlackRock OR Vanguard OR SoftBank",
|
| 301 |
-
# f"{topic} AND Elon Musk OR Sam Altman OR Peter Thiel",
|
| 302 |
-
# f"{topic} AND Berkshire Hathaway OR Warren Buffett",
|
| 303 |
-
# f"{topic} AND institutional investor OR hedge fund",
|
| 304 |
-
# ]
|
| 305 |
-
|
| 306 |
-
# related_terms = get_related_terms(topic)
|
| 307 |
-
# synonym_queries = [f"{term} AND {kw}" for term in related_terms for kw in ["startup", "funding", "merger", "acquisition"]]
|
| 308 |
-
|
| 309 |
-
# all_queries = base_queries + investor_queries + synonym_queries
|
| 310 |
-
|
| 311 |
-
# for query in all_queries:
|
| 312 |
-
# try:
|
| 313 |
-
# print(f"🔍 Tavily query: {query}")
|
| 314 |
-
# response = tavily_search(query, days)
|
| 315 |
-
|
| 316 |
-
# if not isinstance(response, dict) or "results" not in response:
|
| 317 |
-
# print(f"⚠️ Tavily API response issue: {response}")
|
| 318 |
-
# continue
|
| 319 |
-
|
| 320 |
-
# for item in response.get("results", []):
|
| 321 |
-
# url = item.get("url")
|
| 322 |
-
# content = item.get("content", "") or item.get("summary", "") or item.get("title", "")
|
| 323 |
-
# if url and url not in seen_urls and len(content) > 150:
|
| 324 |
-
# all_results.append({
|
| 325 |
-
# "title": item.get("title"),
|
| 326 |
-
# "url": url,
|
| 327 |
-
# "content": content
|
| 328 |
-
# })
|
| 329 |
-
# seen_urls.add(url)
|
| 330 |
-
|
| 331 |
-
# except Exception as e:
|
| 332 |
-
# print(f"⚠️ Tavily request failed for query '{query}': {e}")
|
| 333 |
-
|
| 334 |
-
# print(f"📰 Total articles collected: {len(all_results)}")
|
| 335 |
-
# return all_results
|
| 336 |
-
|
| 337 |
# === Generate Markdown Report ===
|
|
|
|
|
|
|
| 338 |
def generate_value_investor_report(topic, news_results, max_articles=20, max_chars_per_article=400):
|
| 339 |
news_results = news_results[:max_articles]
|
| 340 |
|
|
@@ -353,7 +287,7 @@ def generate_value_investor_report(topic, news_results, max_articles=20, max_cha
|
|
| 353 |
)
|
| 354 |
|
| 355 |
prompt = PromptTemplate.from_template("""
|
| 356 |
-
You're a highly focused value investor. Analyze this week's news on "{Topic}".
|
| 357 |
|
| 358 |
Your goal is to uncover:
|
| 359 |
- Meaningful events (e.g., CEO joining a startup, insider buys, big-name partnerships)
|
|
@@ -403,9 +337,85 @@ Include context and macroeconomic/regulatory angles. Add an intro on sentiment a
|
|
| 403 |
])
|
| 404 |
prompt_value = chat_prompt.format_prompt(
|
| 405 |
Topic=topic,
|
| 406 |
-
ArticleSummaries=article_summary
|
|
|
|
| 407 |
).to_messages()
|
| 408 |
|
| 409 |
llm = get_llm()
|
| 410 |
result = llm.invoke(prompt_value)
|
| 411 |
return result.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
def fetch_deep_news(topic, days):
|
| 218 |
all_results = []
|
| 219 |
seen_urls = set()
|
|
|
|
| 220 |
|
| 221 |
base_queries = [
|
| 222 |
topic,
|
|
|
|
| 252 |
for item in response.get("results", []):
|
| 253 |
url = item.get("url")
|
| 254 |
content = item.get("content", "") or item.get("summary", "") or item.get("title", "")
|
| 255 |
+
if url and url not in seen_urls and len(content) > 150:
|
| 256 |
+
all_results.append({
|
| 257 |
+
"title": item.get("title"),
|
| 258 |
+
"url": url,
|
| 259 |
+
"content": content
|
| 260 |
+
})
|
| 261 |
+
seen_urls.add(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
except Exception as e:
|
| 264 |
print(f"⚠️ Tavily request failed for query '{query}': {e}")
|
|
|
|
| 266 |
print(f"📰 Total articles collected: {len(all_results)}")
|
| 267 |
return all_results
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
# === Generate Markdown Report ===
|
| 270 |
+
from datetime import datetime
|
| 271 |
+
|
| 272 |
def generate_value_investor_report(topic, news_results, max_articles=20, max_chars_per_article=400):
|
| 273 |
news_results = news_results[:max_articles]
|
| 274 |
|
|
|
|
| 287 |
)
|
| 288 |
|
| 289 |
prompt = PromptTemplate.from_template("""
|
| 290 |
+
You're a highly focused value investor. Today is {Today}. Analyze this week's news on "{Topic}".
|
| 291 |
|
| 292 |
Your goal is to uncover:
|
| 293 |
- Meaningful events (e.g., CEO joining a startup, insider buys, big-name partnerships)
|
|
|
|
| 337 |
])
|
| 338 |
prompt_value = chat_prompt.format_prompt(
|
| 339 |
Topic=topic,
|
| 340 |
+
ArticleSummaries=article_summary,
|
| 341 |
+
Today=datetime.now().strftime("%B %d, %Y")
|
| 342 |
).to_messages()
|
| 343 |
|
| 344 |
llm = get_llm()
|
| 345 |
result = llm.invoke(prompt_value)
|
| 346 |
return result.content
|
| 347 |
+
|
| 348 |
+
# def generate_value_investor_report(topic, news_results, max_articles=20, max_chars_per_article=400):
|
| 349 |
+
# news_results = news_results[:max_articles]
|
| 350 |
+
|
| 351 |
+
# for item in news_results:
|
| 352 |
+
# text = item.get("summary") or item.get("content", "")
|
| 353 |
+
# result = analyze_article(text)
|
| 354 |
+
# item["fin_sentiment"] = result.get("sentiment", "neutral")
|
| 355 |
+
# item["fin_confidence"] = result.get("confidence", 0.0)
|
| 356 |
+
# item["investment_decision"] = result.get("investment_decision", "Watch")
|
| 357 |
+
|
| 358 |
+
# article_summary = "".join(
|
| 359 |
+
# f"- **{item['title']}**: { (item.get('content') or item.get('summary', ''))[:max_chars_per_article]}... "
|
| 360 |
+
# f"(Sentiment: {item['fin_sentiment'].title()}, Confidence: {item['fin_confidence']:.2f}, "
|
| 361 |
+
# f"Decision: {item['investment_decision']}) [link]({item['url']})\n"
|
| 362 |
+
# for item in news_results
|
| 363 |
+
# )
|
| 364 |
+
|
| 365 |
+
# prompt = PromptTemplate.from_template("""
|
| 366 |
+
# You're a highly focused value investor. Analyze this week's news on "{Topic}".
|
| 367 |
+
|
| 368 |
+
# Your goal is to uncover:
|
| 369 |
+
# - Meaningful events (e.g., CEO joining a startup, insider buys, big-name partnerships)
|
| 370 |
+
# - Startups or small caps that may signal undervalued opportunity
|
| 371 |
+
# - Connections to key individuals or institutions (e.g., Elon Musk investing, Sam Altman joining)
|
| 372 |
+
# - Companies with strong fundamentals: low P/E, low P/B, high ROE, recent IPOs, moats, or high free cash flow - THEY MUST INCLUDE THE LINK AS WELL and this is very important
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
# ### News
|
| 376 |
+
# {ArticleSummaries}
|
| 377 |
+
|
| 378 |
+
# Write a markdown memo with:
|
| 379 |
+
# 1. **Key Value Signals**
|
| 380 |
+
# 2. **Stocks or Startups to Watch** -- MUST: Explanation of each or why they are interesting AND ALSO MUST: the Add on each of the stocks the Price-to-Earnings Ratio · Price-to-Book Ratio · Debt-to-Equity Ratio · Free Cash Flow · PEG Ratio · fore each, this is very important
|
| 381 |
+
# 3. **What Smart Money Might Be Acting On**
|
| 382 |
+
# 4. **References**
|
| 383 |
+
# 5. **Investment Hypothesis**
|
| 384 |
+
|
| 385 |
+
# ### 📌 Executive Summary
|
| 386 |
+
|
| 387 |
+
# Summarize the topic's current investment environment in 3–4 bullet points. Include sentiment, risks, and catalysts.
|
| 388 |
+
|
| 389 |
+
# ---
|
| 390 |
+
|
| 391 |
+
# ### 📊 Signals and Analysis (Include Sources)
|
| 392 |
+
|
| 393 |
+
# For each important news item, write a short paragraph with:
|
| 394 |
+
# - What happened
|
| 395 |
+
# - Why it matters (financially)
|
| 396 |
+
# - Embedded source as `[source title](url)`
|
| 397 |
+
# - Bold any key financial terms (e.g., **Series A**, **merger**, **valuation**)
|
| 398 |
+
|
| 399 |
+
# ---
|
| 400 |
+
|
| 401 |
+
# ### 🧠 Investment Thesis
|
| 402 |
+
|
| 403 |
+
# Give a reasoned conclusion:
|
| 404 |
+
# - Is this a buy/sell/watch opportunity?
|
| 405 |
+
# - What’s the risk/reward?
|
| 406 |
+
# - What signals or themes matter most?
|
| 407 |
+
|
| 408 |
+
# Include context and macroeconomic/regulatory angles. Add an intro on sentiment and market trends for the week.
|
| 409 |
+
# """)
|
| 410 |
+
|
| 411 |
+
# chat_prompt = ChatPromptTemplate.from_messages([
|
| 412 |
+
# SystemMessagePromptTemplate(prompt=prompt)
|
| 413 |
+
# ])
|
| 414 |
+
# prompt_value = chat_prompt.format_prompt(
|
| 415 |
+
# Topic=topic,
|
| 416 |
+
# ArticleSummaries=article_summary
|
| 417 |
+
# ).to_messages()
|
| 418 |
+
|
| 419 |
+
# llm = get_llm()
|
| 420 |
+
# result = llm.invoke(prompt_value)
|
| 421 |
+
# return result.content
|