Spaces:
Running
Running
RobertoBarrosoLuque
commited on
Commit
·
8cc0920
1
Parent(s):
9e054cc
Add scraper
Browse files- configs/prompt_library.yaml +28 -0
- src/modules/__init__.py +0 -0
- src/modules/constants.py +7 -0
- src/modules/data_pipeline.py +503 -0
- src/modules/llm_completions.py +62 -0
configs/prompt_library.yaml
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
extract_rate_decision: |
|
| 2 |
+
You are an expert economist analyzing Federal Reserve meetings. Your task is to extract the key interest rate decision and provide clear, actionable insights from FOMC meeting minutes.
|
| 3 |
+
|
| 4 |
+
CRITICAL INSTRUCTIONS:
|
| 5 |
+
1. Look for the specific target range decision in the "Committee Policy Actions" section
|
| 6 |
+
2. The federal funds rate is expressed as a range (e.g., "4¼ to 4½ percent")
|
| 7 |
+
3. Extract forward guidance about future policy direction
|
| 8 |
+
4. Identify key economic factors driving the decision
|
| 9 |
+
5. Use plain language that business leaders and investors can understand
|
| 10 |
+
|
| 11 |
+
REQUIRED OUTPUT FORMAT:
|
| 12 |
+
- Action: [Raised/Lowered/Maintained] federal funds rate
|
| 13 |
+
- Rate: [Current target range, e.g., "4.25%-4.50%"]
|
| 14 |
+
- Magnitude: [Amount of change, e.g., "0.25 percentage points" or "No change"]
|
| 15 |
+
- Forward Guidance: [What the Fed signaled about future rate changes in 1-2 sentences]
|
| 16 |
+
- Key Economic Factors: [List 3-4 main factors that influenced the decision]
|
| 17 |
+
- Economic Outlook: [Fed's assessment of growth, employment, and inflation in 2-3 sentences]
|
| 18 |
+
- Market Impact: [Likely implications for businesses, consumers, and markets in 1-2 sentences]
|
| 19 |
+
|
| 20 |
+
SPECIFIC SECTIONS TO ANALYZE:
|
| 21 |
+
- "Committee Policy Actions" (for the actual rate decision)
|
| 22 |
+
- "Participants' Views on Current Conditions" (for economic assessment)
|
| 23 |
+
- Post-meeting statement (for forward guidance)
|
| 24 |
+
- Staff economic projections (for outlook)
|
| 25 |
+
|
| 26 |
+
Meeting Date: {meeting_date}
|
| 27 |
+
Title: {meeting_title}
|
| 28 |
+
Meeting Text: {text}
|
src/modules/__init__.py
ADDED
|
File without changes
|
src/modules/constants.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yaml
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
_PATH_TO_CONFIGS = Path(__file__).parents[2] / "configs" / "prompt_library.yaml"
|
| 5 |
+
|
| 6 |
+
with open(_PATH_TO_CONFIGS, "r") as f:
|
| 7 |
+
PROMPT_LIBRARY = yaml.safe_load(f)
|
src/modules/data_pipeline.py
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
import ssl
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Dict, List, Optional, Tuple
|
| 9 |
+
from urllib.parse import urljoin
|
| 10 |
+
|
| 11 |
+
import aiohttp
|
| 12 |
+
import certifi
|
| 13 |
+
import requests
|
| 14 |
+
from bs4 import BeautifulSoup
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
from pydantic import BaseModel
|
| 17 |
+
import pdfplumber
|
| 18 |
+
import tempfile
|
| 19 |
+
|
| 20 |
+
from src.modules.llm_completions import get_llm, run_multi_llm_completions
|
| 21 |
+
from src.modules.constants import PROMPT_LIBRARY
|
| 22 |
+
|
| 23 |
+
DATA_DIR = Path(__file__).parents[2] / "data"
|
| 24 |
+
|
| 25 |
+
class RateDecision(BaseModel):
|
| 26 |
+
"""Enhanced Pydantic model for comprehensive Fed decision analysis"""
|
| 27 |
+
action: str
|
| 28 |
+
rate: str
|
| 29 |
+
magnitude: str
|
| 30 |
+
forward_guidance: str
|
| 31 |
+
key_economic_factors: List[str]
|
| 32 |
+
economic_outlook: str
|
| 33 |
+
market_impact: str
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class Meeting:
|
| 37 |
+
"""Data model for a Fed meeting"""
|
| 38 |
+
|
| 39 |
+
def __init__(self, date: str, title: str, full_text: str, url: str = ""):
|
| 40 |
+
self.date = date
|
| 41 |
+
self.title = title
|
| 42 |
+
self.full_text = full_text
|
| 43 |
+
self.url = url
|
| 44 |
+
self.rate_decision = None
|
| 45 |
+
self.summary = None
|
| 46 |
+
|
| 47 |
+
def to_dict(self) -> Dict:
|
| 48 |
+
return {
|
| 49 |
+
"date": self.date,
|
| 50 |
+
"title": self.title,
|
| 51 |
+
"full_text": self.full_text,
|
| 52 |
+
"url": self.url,
|
| 53 |
+
"rate_decision": self.rate_decision,
|
| 54 |
+
"summary": self.summary
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
@classmethod
|
| 58 |
+
def from_dict(cls, data: Dict) -> 'Meeting':
|
| 59 |
+
meeting = cls(data["date"], data["title"], data["full_text"], data.get("url", ""))
|
| 60 |
+
meeting.rate_decision = data.get("rate_decision")
|
| 61 |
+
meeting.summary = data.get("summary")
|
| 62 |
+
return meeting
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class FedScraper:
|
| 66 |
+
"""Scrapes FOMC meeting minutes from federalreserve.gov"""
|
| 67 |
+
|
| 68 |
+
BASE_URL = "https://www.federalreserve.gov"
|
| 69 |
+
CALENDAR_URL = "https://www.federalreserve.gov/monetarypolicy/fomccalendars.htm"
|
| 70 |
+
|
| 71 |
+
def __init__(self, session: Optional[aiohttp.ClientSession] = None):
|
| 72 |
+
self.session = session
|
| 73 |
+
self._own_session = session is None
|
| 74 |
+
|
| 75 |
+
async def __aenter__(self):
|
| 76 |
+
if self._own_session:
|
| 77 |
+
# Create SSL context with proper certificate verification
|
| 78 |
+
ssl_context = ssl.create_default_context(cafile=certifi.where())
|
| 79 |
+
connector = aiohttp.TCPConnector(ssl=ssl_context)
|
| 80 |
+
|
| 81 |
+
# Add headers to mimic a real browser
|
| 82 |
+
headers = {
|
| 83 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
self.session = aiohttp.ClientSession(
|
| 87 |
+
connector=connector,
|
| 88 |
+
headers=headers,
|
| 89 |
+
timeout=aiohttp.ClientTimeout(total=30)
|
| 90 |
+
)
|
| 91 |
+
return self
|
| 92 |
+
|
| 93 |
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
| 94 |
+
if self._own_session and self.session:
|
| 95 |
+
await self.session.close()
|
| 96 |
+
|
| 97 |
+
def get_calendar_page(self) -> BeautifulSoup:
|
| 98 |
+
"""Get the FOMC calendar page"""
|
| 99 |
+
headers = {
|
| 100 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
# Use requests with SSL verification and retry logic
|
| 104 |
+
session = requests.Session()
|
| 105 |
+
session.headers.update(headers)
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
response = session.get(self.CALENDAR_URL, timeout=30, verify=True)
|
| 109 |
+
response.raise_for_status()
|
| 110 |
+
return BeautifulSoup(response.content, 'html.parser')
|
| 111 |
+
except requests.exceptions.SSLError:
|
| 112 |
+
print("SSL verification failed, trying without verification...")
|
| 113 |
+
response = session.get(self.CALENDAR_URL, timeout=30, verify=False)
|
| 114 |
+
response.raise_for_status()
|
| 115 |
+
return BeautifulSoup(response.content, 'html.parser')
|
| 116 |
+
|
| 117 |
+
async def scrape_meetings(self, max_meetings: int = 20, year_range: Tuple[int, int] = (2022, 2024)) -> List[
|
| 118 |
+
Meeting]:
|
| 119 |
+
"""Scrape multiple meetings"""
|
| 120 |
+
print("Fetching FOMC calendar page...")
|
| 121 |
+
soup = self.get_calendar_page()
|
| 122 |
+
|
| 123 |
+
print(f"Extracting meeting links for years {year_range[0]}-{year_range[1]}...")
|
| 124 |
+
meeting_links = self.extract_meeting_links(soup, year_range)
|
| 125 |
+
|
| 126 |
+
pdf_links = [
|
| 127 |
+
(date, f"FOMC Meeting {date}", link)
|
| 128 |
+
for date, _, link in meeting_links if link.lower().endswith('.pdf')
|
| 129 |
+
]
|
| 130 |
+
|
| 131 |
+
if not meeting_links:
|
| 132 |
+
print("No meeting links found")
|
| 133 |
+
return []
|
| 134 |
+
|
| 135 |
+
print(f"Found {len(meeting_links)} meetings")
|
| 136 |
+
|
| 137 |
+
# Limit number of meetings
|
| 138 |
+
meeting_links = meeting_links[:max_meetings]
|
| 139 |
+
if len(meeting_links) < len(meeting_links):
|
| 140 |
+
print(f"Processing first {max_meetings} meetings")
|
| 141 |
+
|
| 142 |
+
meetings = []
|
| 143 |
+
|
| 144 |
+
async with self: # This will call __aenter__ and __aexit__
|
| 145 |
+
for i, (date, title, url) in enumerate(pdf_links, 1):
|
| 146 |
+
try:
|
| 147 |
+
print(f"\n[{i}/{len(meeting_links)}] Scraping: {date}")
|
| 148 |
+
print(f" URL: {url}")
|
| 149 |
+
|
| 150 |
+
content = await self.scrape_meeting_content(url)
|
| 151 |
+
if content:
|
| 152 |
+
meeting = Meeting(date, title, content, url)
|
| 153 |
+
meetings.append(meeting)
|
| 154 |
+
print(f" Successfully extracted {len(content)} characters")
|
| 155 |
+
else:
|
| 156 |
+
print(f" No content extracted from {url}")
|
| 157 |
+
|
| 158 |
+
# Rate limiting - be respectful to Fed servers
|
| 159 |
+
if i < len(meeting_links):
|
| 160 |
+
print(" Waiting 2 seconds before next request...")
|
| 161 |
+
await asyncio.sleep(2)
|
| 162 |
+
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f" Error scraping meeting {date}: {e}")
|
| 165 |
+
continue
|
| 166 |
+
|
| 167 |
+
print(f"\nSuccessfully scraped {len(meetings)} out of {len(meeting_links)} meetings")
|
| 168 |
+
return meetings
|
| 169 |
+
|
| 170 |
+
async def scrape_meeting_content(self, url: str) -> str:
|
| 171 |
+
"""Scrape content from HTML pages or extract text from PDF files"""
|
| 172 |
+
if not self.session:
|
| 173 |
+
raise RuntimeError("Session not initialized. Use async context manager.")
|
| 174 |
+
|
| 175 |
+
try:
|
| 176 |
+
async with self.session.get(url) as response:
|
| 177 |
+
response.raise_for_status()
|
| 178 |
+
|
| 179 |
+
# Check content type
|
| 180 |
+
content_type = response.headers.get('content-type', '').lower()
|
| 181 |
+
|
| 182 |
+
if 'application/pdf' in content_type or url.lower().endswith('.pdf'):
|
| 183 |
+
print(f" Processing PDF: {url}")
|
| 184 |
+
return await self._extract_pdf_text(response)
|
| 185 |
+
else:
|
| 186 |
+
print(f" Processing HTML: {url}")
|
| 187 |
+
return await self._extract_html_text(response)
|
| 188 |
+
|
| 189 |
+
except Exception as e:
|
| 190 |
+
print(f" Error processing {url}: {e}")
|
| 191 |
+
return ""
|
| 192 |
+
|
| 193 |
+
async def _extract_pdf_text(self, response) -> str:
|
| 194 |
+
"""Extract text from PDF using pdfplumber"""
|
| 195 |
+
try:
|
| 196 |
+
pdf_content = await response.read()
|
| 197 |
+
|
| 198 |
+
# Create temporary file for pdfplumber processing
|
| 199 |
+
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_file:
|
| 200 |
+
tmp_file.write(pdf_content)
|
| 201 |
+
tmp_file.flush()
|
| 202 |
+
|
| 203 |
+
text_content = []
|
| 204 |
+
|
| 205 |
+
try:
|
| 206 |
+
with pdfplumber.open(tmp_file.name) as pdf:
|
| 207 |
+
print(f" Extracting text from {len(pdf.pages)} pages")
|
| 208 |
+
|
| 209 |
+
for page_num, page in enumerate(pdf.pages):
|
| 210 |
+
try:
|
| 211 |
+
page_text = page.extract_text()
|
| 212 |
+
if page_text and page_text.strip():
|
| 213 |
+
# Clean up common PDF artifacts
|
| 214 |
+
page_text = self._clean_pdf_text(page_text)
|
| 215 |
+
text_content.append(page_text)
|
| 216 |
+
except Exception as e:
|
| 217 |
+
print(f" Could not extract text from page {page_num + 1}: {e}")
|
| 218 |
+
continue
|
| 219 |
+
|
| 220 |
+
finally:
|
| 221 |
+
# Always cleanup temp file
|
| 222 |
+
try:
|
| 223 |
+
os.unlink(tmp_file.name)
|
| 224 |
+
except OSError:
|
| 225 |
+
pass
|
| 226 |
+
|
| 227 |
+
# Join all page text
|
| 228 |
+
return '\n\n'.join(text_content)
|
| 229 |
+
|
| 230 |
+
except Exception as e:
|
| 231 |
+
print(f" Error extracting PDF text: {e}")
|
| 232 |
+
return ""
|
| 233 |
+
|
| 234 |
+
@staticmethod
|
| 235 |
+
def _clean_pdf_text(text: str) -> str:
|
| 236 |
+
"""Clean common PDF text extraction artifacts"""
|
| 237 |
+
# Remove excessive whitespace while preserving paragraph breaks
|
| 238 |
+
text = re.sub(r'[ \t]+', ' ', text)
|
| 239 |
+
|
| 240 |
+
# Fix common PDF line break issues
|
| 241 |
+
text = re.sub(r'(\w)-\s*\n\s*(\w)', r'\1\2', text) # Rejoin hyphenated words
|
| 242 |
+
text = re.sub(r'(?<=[.!?])\s*\n\s*(?=[A-Z])', ' ', text) # Join sentences split across lines
|
| 243 |
+
|
| 244 |
+
# Remove page numbers and headers/footers (common patterns)
|
| 245 |
+
text = re.sub(r'\n\s*\d+\s*\n', '\n', text) # Standalone page numbers
|
| 246 |
+
text = re.sub(r'\n\s*Page \d+ of \d+\s*\n', '\n', text) # "Page X of Y"
|
| 247 |
+
|
| 248 |
+
return text.strip()
|
| 249 |
+
|
| 250 |
+
@staticmethod
|
| 251 |
+
async def _extract_html_text(response) -> str:
|
| 252 |
+
"""Extract text from HTML response"""
|
| 253 |
+
try:
|
| 254 |
+
try:
|
| 255 |
+
content = await response.text()
|
| 256 |
+
except UnicodeDecodeError:
|
| 257 |
+
# Fallback for encoding issues
|
| 258 |
+
content_bytes = await response.read()
|
| 259 |
+
content = content_bytes.decode('utf-8', errors='ignore')
|
| 260 |
+
|
| 261 |
+
soup = BeautifulSoup(content, 'html.parser')
|
| 262 |
+
|
| 263 |
+
# Remove script and style elements
|
| 264 |
+
for script in soup(["script", "style"]):
|
| 265 |
+
script.decompose()
|
| 266 |
+
|
| 267 |
+
# Find the main content area
|
| 268 |
+
content_div = (
|
| 269 |
+
soup.find('div', {'class': 'col-xs-12 col-sm-8 col-md-8'}) or
|
| 270 |
+
soup.find('div', {'id': 'article'}) or
|
| 271 |
+
soup.find('div', {'class': 'content'}) or
|
| 272 |
+
soup.find('main') or
|
| 273 |
+
soup.body
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
if content_div:
|
| 277 |
+
text = content_div.get_text(separator=' ', strip=True)
|
| 278 |
+
text = re.sub(r'\s+', ' ', text)
|
| 279 |
+
print(f" Extracted {len(text)} characters from HTML")
|
| 280 |
+
return text.strip()
|
| 281 |
+
|
| 282 |
+
print(" No content found in HTML")
|
| 283 |
+
return ""
|
| 284 |
+
|
| 285 |
+
except Exception as e:
|
| 286 |
+
print(f" Error extracting HTML text: {e}")
|
| 287 |
+
return ""
|
| 288 |
+
|
| 289 |
+
def extract_meeting_links(self, soup: BeautifulSoup, year_range: Tuple[int, int] = (2022, 2024)) -> List[
|
| 290 |
+
Tuple[str, str, str]]:
|
| 291 |
+
"""Extract meeting links from the calendar page - handles both HTML and PDF"""
|
| 292 |
+
meetings = []
|
| 293 |
+
|
| 294 |
+
for link in soup.find_all('a', href=True):
|
| 295 |
+
href = link.get('href', '')
|
| 296 |
+
text = link.get_text().strip()
|
| 297 |
+
|
| 298 |
+
# Find links to meeting minutes (HTML or PDF)
|
| 299 |
+
if ('minutes' in href.lower() and
|
| 300 |
+
('fomcminutes' in href or 'fomc/minutes' in href)):
|
| 301 |
+
|
| 302 |
+
date_match = re.search(r'(\d{4})(\d{2})(\d{2})', href)
|
| 303 |
+
if date_match:
|
| 304 |
+
year, month, day = date_match.groups()
|
| 305 |
+
year_int = int(year)
|
| 306 |
+
|
| 307 |
+
if year_range[0] <= year_int <= year_range[1]:
|
| 308 |
+
date_str = f"{year}-{month}-{day}"
|
| 309 |
+
full_url = urljoin(self.BASE_URL, href)
|
| 310 |
+
|
| 311 |
+
# Identify content type in title
|
| 312 |
+
content_type = "PDF" if href.lower().endswith('.pdf') else "HTML"
|
| 313 |
+
title_with_type = f"{text or 'FOMC Meeting ' + date_str} ({content_type})"
|
| 314 |
+
|
| 315 |
+
meetings.append((date_str, title_with_type, full_url))
|
| 316 |
+
|
| 317 |
+
meetings.sort(key=lambda x: x[0], reverse=True)
|
| 318 |
+
return meetings
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
class DataProcessor:
|
| 323 |
+
"""Processes scraped meeting data using LLM analysis"""
|
| 324 |
+
|
| 325 |
+
def __init__(self, api_key: str, model: str = "small"):
|
| 326 |
+
self.api_key = api_key
|
| 327 |
+
self.model = model
|
| 328 |
+
self.llm = get_llm(model, api_key)
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
async def process_meetings(self, meetings: List[Meeting]) -> List[str]:
|
| 332 |
+
"""Process all meetings with LLM analysis"""
|
| 333 |
+
print(f"Processing {len(meetings)} meetings with LLM analysis...")
|
| 334 |
+
|
| 335 |
+
prompts = [
|
| 336 |
+
PROMPT_LIBRARY['extract_rate_decision'].format(
|
| 337 |
+
meeting_date=meeting.date,
|
| 338 |
+
meeting_title=meeting.title,
|
| 339 |
+
text=meeting.full_text if len(meeting.full_text) < 100000 else meeting.full_text[:100000]
|
| 340 |
+
)
|
| 341 |
+
for meeting in meetings
|
| 342 |
+
]
|
| 343 |
+
|
| 344 |
+
meetings_extracted = await run_multi_llm_completions(
|
| 345 |
+
llm=self.llm,
|
| 346 |
+
prompts=prompts,
|
| 347 |
+
output_class=RateDecision
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
return meetings_extracted
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
class FedDataPipeline:
|
| 354 |
+
"""Main pipeline for scraping and processing Fed meeting data"""
|
| 355 |
+
|
| 356 |
+
def __init__(self, api_key: str, model: str = "small"):
|
| 357 |
+
self.api_key = api_key
|
| 358 |
+
self.model = model
|
| 359 |
+
self.data_dir = DATA_DIR
|
| 360 |
+
self.data_dir.mkdir(exist_ok=True)
|
| 361 |
+
|
| 362 |
+
self.scraper = FedScraper()
|
| 363 |
+
self.processor = DataProcessor(api_key, model)
|
| 364 |
+
|
| 365 |
+
def save_meetings(self, meetings: List[Meeting], filename: str = None) -> str:
|
| 366 |
+
"""Save meetings to JSON file"""
|
| 367 |
+
if filename is None:
|
| 368 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 369 |
+
filename = f"fed_meetings_{timestamp}.json"
|
| 370 |
+
|
| 371 |
+
filepath = self.data_dir / filename
|
| 372 |
+
|
| 373 |
+
meetings_data = [meeting.to_dict() for meeting in meetings]
|
| 374 |
+
|
| 375 |
+
with open(filepath, 'w', encoding='utf-8') as f:
|
| 376 |
+
json.dump(meetings_data, f, indent=2, ensure_ascii=False)
|
| 377 |
+
|
| 378 |
+
print(f"Saved {len(meetings)} meetings to {filepath}")
|
| 379 |
+
return str(filepath)
|
| 380 |
+
|
| 381 |
+
def load_meetings(self, filename: str) -> List[Meeting]:
|
| 382 |
+
"""Load meetings from JSON file"""
|
| 383 |
+
filepath = self.data_dir / filename if not os.path.isabs(filename) else Path(filename)
|
| 384 |
+
|
| 385 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 386 |
+
data = json.load(f)
|
| 387 |
+
|
| 388 |
+
meetings = [Meeting.from_dict(item) for item in data]
|
| 389 |
+
print(f"Loaded {len(meetings)} meetings from {filepath}")
|
| 390 |
+
return meetings
|
| 391 |
+
|
| 392 |
+
async def process_from_scraped_data(self, scraped_filename: str) -> str:
|
| 393 |
+
"""Process already scraped data with LLM analysis"""
|
| 394 |
+
print(f"Loading scraped data from: {scraped_filename}")
|
| 395 |
+
meetings = self.load_meetings(scraped_filename)
|
| 396 |
+
|
| 397 |
+
if not meetings:
|
| 398 |
+
print("No meetings found in scraped data")
|
| 399 |
+
return ""
|
| 400 |
+
|
| 401 |
+
print(f"\nProcessing {len(meetings)} meetings with LLM analysis...")
|
| 402 |
+
processed_results = await self.processor.process_meetings(meetings)
|
| 403 |
+
|
| 404 |
+
# Update meetings with processed results
|
| 405 |
+
if len(processed_results) == len(meetings):
|
| 406 |
+
for i, result in enumerate(processed_results):
|
| 407 |
+
meetings[i].rate_decision = result.dict() if hasattr(result, 'dict') else result
|
| 408 |
+
|
| 409 |
+
# Save final processed data
|
| 410 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 411 |
+
processed_filename = f"fed_meetings_processed_{timestamp}.json"
|
| 412 |
+
output_file = self.save_meetings(meetings, processed_filename)
|
| 413 |
+
|
| 414 |
+
print("\nProcessing completed successfully!")
|
| 415 |
+
print(f"Processed data: {output_file}")
|
| 416 |
+
return output_file
|
| 417 |
+
|
| 418 |
+
async def run_pipeline(self, max_meetings: int = 20, year_range: Tuple[int, int] = (2022, 2024)) -> str:
|
| 419 |
+
"""Run the complete data pipeline"""
|
| 420 |
+
print("Starting Fed AI Savant Data Pipeline...")
|
| 421 |
+
|
| 422 |
+
# Step 1: Scrape meeting data
|
| 423 |
+
print("\n1. Scraping FOMC meeting minutes...")
|
| 424 |
+
meetings = await self.scraper.scrape_meetings(max_meetings, year_range)
|
| 425 |
+
print(f"Scraped {len(meetings)} meetings")
|
| 426 |
+
|
| 427 |
+
if not meetings:
|
| 428 |
+
print("No meetings found to process")
|
| 429 |
+
return ""
|
| 430 |
+
|
| 431 |
+
# Save intermediate scraped data (before LLM processing)
|
| 432 |
+
print("\n1.5. Saving intermediate scraped data...")
|
| 433 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 434 |
+
scraped_filename = f"fed_meetings_scraped_{timestamp}.json"
|
| 435 |
+
scraped_filepath = self.save_meetings(meetings, scraped_filename)
|
| 436 |
+
print(f"Intermediate scraped data saved to: {scraped_filepath}")
|
| 437 |
+
|
| 438 |
+
# Step 2: Process with LLM analysis
|
| 439 |
+
print("\n2. Processing meetings with LLM analysis...")
|
| 440 |
+
processed_results = await self.processor.process_meetings(meetings)
|
| 441 |
+
|
| 442 |
+
# Update meetings with processed results
|
| 443 |
+
if len(processed_results) == len(meetings):
|
| 444 |
+
for i, result in enumerate(processed_results):
|
| 445 |
+
meetings[i].rate_decision = result.dict() if hasattr(result, 'dict') else result
|
| 446 |
+
|
| 447 |
+
# Step 3: Save final processed data
|
| 448 |
+
print("\n3. Saving final processed data...")
|
| 449 |
+
processed_filename = f"fed_meetings_processed_{timestamp}.json"
|
| 450 |
+
output_file = self.save_meetings(meetings, processed_filename)
|
| 451 |
+
|
| 452 |
+
print("\nPipeline completed successfully!")
|
| 453 |
+
print(f"Scraped data: {scraped_filepath}")
|
| 454 |
+
print(f"Processed data: {output_file}")
|
| 455 |
+
return output_file
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
async def main():
|
| 459 |
+
"""Main function for running the pipeline as a script"""
|
| 460 |
+
import argparse
|
| 461 |
+
|
| 462 |
+
# Load environment variables
|
| 463 |
+
load_dotenv()
|
| 464 |
+
|
| 465 |
+
parser = argparse.ArgumentParser(description="Fed AI Savant Data Pipeline")
|
| 466 |
+
parser.add_argument("--max-meetings", type=int, default=3, help="Maximum number of meetings to scrape")
|
| 467 |
+
parser.add_argument("--start-year", type=int, default=2022, help="Start year for meeting range")
|
| 468 |
+
parser.add_argument("--end-year", type=int, default=2025, help="End year for meeting range")
|
| 469 |
+
parser.add_argument("--data-dir", default="data", help="Directory to save data files")
|
| 470 |
+
parser.add_argument("--from-scraped", type=str, help="Process from already scraped data file (skips scraping)")
|
| 471 |
+
|
| 472 |
+
args = parser.parse_args()
|
| 473 |
+
|
| 474 |
+
# Get API key from environment
|
| 475 |
+
api_key = os.getenv("FIREWORKS_API_KEY")
|
| 476 |
+
if not api_key:
|
| 477 |
+
print("Error: FIREWORKS_API_KEY not found in environment variables")
|
| 478 |
+
print("Please create a .env file with: FIREWORKS_API_KEY=your_api_key_here")
|
| 479 |
+
return
|
| 480 |
+
|
| 481 |
+
# Create and run pipeline (using default "small" model)
|
| 482 |
+
pipeline = FedDataPipeline(
|
| 483 |
+
api_key=api_key,
|
| 484 |
+
model="small",
|
| 485 |
+
data_dir=args.data_dir
|
| 486 |
+
)
|
| 487 |
+
|
| 488 |
+
# Check if processing from already scraped data
|
| 489 |
+
if args.from_scraped:
|
| 490 |
+
print(f"Processing from scraped data: {args.from_scraped}")
|
| 491 |
+
output_file = await pipeline.process_from_scraped_data(args.from_scraped)
|
| 492 |
+
else:
|
| 493 |
+
year_range = (args.start_year, args.end_year)
|
| 494 |
+
output_file = await pipeline.run_pipeline(args.max_meetings, year_range)
|
| 495 |
+
|
| 496 |
+
if output_file:
|
| 497 |
+
print(f"\nSuccessfully completed! Data saved to: {output_file}")
|
| 498 |
+
else:
|
| 499 |
+
print("\nPipeline failed or no data processed")
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
if __name__ == "__main__":
|
| 503 |
+
asyncio.run(main())
|
src/modules/llm_completions.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fireworks import LLM
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import asyncio
|
| 4 |
+
|
| 5 |
+
MODELS = {
|
| 6 |
+
"small": "accounts/fireworks/models/gpt-oss-20b",
|
| 7 |
+
"large": "accounts/fireworks/models/gpt-oss-120b"
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
semaphore = asyncio.Semaphore(100)
|
| 11 |
+
|
| 12 |
+
def get_llm(model: str, api_key: str) -> LLM:
|
| 13 |
+
return LLM(model=MODELS[model], api_key=api_key, deployment_type="serverless")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
async def get_llm_completion(llm: LLM, prompt_text: str, output_class: BaseModel = None) -> str:
|
| 17 |
+
if isinstance(output_class, BaseModel):
|
| 18 |
+
return llm.chat.completions.create(
|
| 19 |
+
messages=[
|
| 20 |
+
{
|
| 21 |
+
"role": "user",
|
| 22 |
+
"content": prompt_text
|
| 23 |
+
},
|
| 24 |
+
],
|
| 25 |
+
temperature=0.1,
|
| 26 |
+
output_class=output_class
|
| 27 |
+
)
|
| 28 |
+
return llm.chat.completions.create(
|
| 29 |
+
messages=[
|
| 30 |
+
{
|
| 31 |
+
"role": "user",
|
| 32 |
+
"content": prompt_text
|
| 33 |
+
},
|
| 34 |
+
],
|
| 35 |
+
temperature=0.1
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
async def run_multi_llm_completions(llm: LLM, prompts: list[str], output_class: BaseModel) -> list[str]:
|
| 40 |
+
"""
|
| 41 |
+
Run multiple LLM completions in parallel
|
| 42 |
+
|
| 43 |
+
:param llm:
|
| 44 |
+
:param prompts:
|
| 45 |
+
:param output_class:
|
| 46 |
+
:return:
|
| 47 |
+
"""
|
| 48 |
+
with semaphore:
|
| 49 |
+
if isinstance(output_class, BaseModel):
|
| 50 |
+
tasks = [
|
| 51 |
+
asyncio.create_task(
|
| 52 |
+
get_llm_completion(llm=llm, prompt_text=prompt, output_class=output_class)
|
| 53 |
+
) for prompt in prompts
|
| 54 |
+
]
|
| 55 |
+
else:
|
| 56 |
+
tasks = [
|
| 57 |
+
asyncio.create_task(
|
| 58 |
+
get_llm_completion(llm=llm, prompt_text=prompt)
|
| 59 |
+
) for prompt in prompts
|
| 60 |
+
]
|
| 61 |
+
return await asyncio.gather(*tasks)
|
| 62 |
+
|