Edwin Salguero commited on
Commit ·
94e5687
1
Parent(s): 712bf79
Remove all demo data and update project to use only real FRED API data
Browse filesFiles changed (2)
- frontend/app.py+161-445
- frontend/demo_data.pyDeleted-288
36 unmodified lines3738394041424346 unmodified lines90919293949596979899100101102103104105106107108316 unmodified lines42542642742842943043143243343443543643743843944040 unmodified lines48148248348448548642 unmodified lines52953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462573 unmodified lines69970070170270370470570618 unmodified lines72572672772872973044 unmodified lines77577677777877978072 unmodified lines853854855856857858859860861862863864865866867868869123 unmodified lines99399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111365 unmodified lines117911801181118211831184189 unmodified lines1374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314544 unmodified lines145914601461146214631464101 unmodified lines156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917202 unmodified lines17231724172517261727172817291730173117321733173417351736173748 unmodified lines1786178717881789179017911792179334 unmodified lines182818291830183118321833183436 unmodified linesreturn requests# Initialize flagsDEMO_MODE = FalseANALYTICS_AVAILABLE = FalseFRED_API_AVAILABLE = FalseCONFIG_AVAILABLE = False46 unmodified linesREAL_DATA_MODE = FRED_API_KEY and FRED_API_KEY != 'your-fred-api-key-here'return False# Lazy load demo datadef load_demo_data():"""Load demo data only when needed"""global DEMO_MODEtry:from demo_data import get_demo_dataDEMO_DATA = get_demo_data()DEMO_MODE = Truereturn DEMO_DATAexcept ImportError:DEMO_MODE = Falsereturn None# Custom CSS for enterprise stylingst.markdown("""<style>316 unmodified lines# Initialize AWS clientss3_client, lambda_client = init_aws_clients()config = load_config()# Load demo data if neededif not REAL_DATA_MODE:demo_data = load_demo_data()# Show data mode infoif REAL_DATA_MODE:st.success("🎯 Using real FRED API data for live economic insights.")else:st.info("📊 Using demo data for demonstration. Get a free FRED API key for real data.")# Sidebarwith st.sidebar:40 unmodified linesif REAL_DATA_MODE and FRED_API_AVAILABLE:# Get real insights from FRED APItry:insights = generate_real_insights(FRED_API_KEY)with col1:42 unmodified linesexcept Exception as e:st.error(f"Failed to fetch real data: {e}")# Fallback to demo dataif DEMO_MODE:insights = DEMO_DATA['insights']# ... demo data displayelse:# Static fallbackpasselif DEMO_MODE:insights = DEMO_DATA['insights']with col1:gdp_insight = insights['GDPC1']st.markdown(f"""<div class="metric-card"><h3>📈 GDP Growth</h3><h2>{gdp_insight['growth_rate']}</h2><p>{gdp_insight['current_value']}</p><small>{gdp_insight['trend']}</small></div>""", unsafe_allow_html=True)with col2:indpro_insight = insights['INDPRO']st.markdown(f"""<div class="metric-card"><h3>🏭 Industrial Production</h3><h2>{indpro_insight['growth_rate']}</h2><p>{indpro_insight['current_value']}</p><small>{indpro_insight['trend']}</small></div>""", unsafe_allow_html=True)with col3:cpi_insight = insights['CPIAUCSL']st.markdown(f"""<div class="metric-card"><h3>💰 Inflation Rate</h3><h2>{cpi_insight['growth_rate']}</h2><p>{cpi_insight['current_value']}</p><small>{cpi_insight['trend']}</small></div>""", unsafe_allow_html=True)with col4:unrate_insight = insights['UNRATE']st.markdown(f"""<div class="metric-card"><h3>💼 Unemployment</h3><h2>{unrate_insight['current_value']}</h2><p>{unrate_insight['growth_rate']}</p><small>{unrate_insight['trend']}</small></div>""", unsafe_allow_html=True)else:# Fallback to static datawith col1:st.markdown("""<div class="metric-card"><h3>📈 GDP Growth</h3><h2>2.1%</h2><p>Q4 2024</p></div>""", unsafe_allow_html=True)with col2:st.markdown("""<div class="metric-card"><h3>🏭 Industrial Production</h3><h2>+0.8%</h2><p>Monthly Change</p></div>""", unsafe_allow_html=True)with col3:st.markdown("""<div class="metric-card"><h3>💰 Inflation Rate</h3><h2>3.2%</h2><p>Annual Rate</p></div>""", unsafe_allow_html=True)with col4:st.markdown("""<div class="metric-card"><h3>💼 Unemployment</h3><h2>3.7%</h2><p>Current Rate</p></div>""", unsafe_allow_html=True)# Recent analysis sectionst.markdown("""73 unmodified lines</div>""", unsafe_allow_html=True)if DEMO_MODE:st.info("🎯 Running in demo mode with realistic economic data and insights.")# Analysis configurationst.markdown("""18 unmodified lines)# Date rangeend_date = datetime.now()start_date = end_date - timedelta(days=365*5) # 5 years44 unmodified lines# Run real analysis with FRED API datawith st.spinner(analysis_message):try:# Get real economic datareal_data = get_real_economic_data(FRED_API_KEY,start_date_input.strftime('%Y-%m-%d'),72 unmodified linesexcept Exception as e:st.error(f"❌ Real data analysis failed: {e}")st.info("Falling back to demo analysis...")# Fallback to demo analysisif DEMO_MODE:run_demo_analysis(analysis_type, selected_indicators)elif DEMO_MODE:# Run demo analysisrun_demo_analysis(analysis_type, selected_indicators)else:st.error("No data sources available. Please configure FRED API key or use demo mode.")def generate_analysis_results(analysis_type, real_data, selected_indicators):"""Generate analysis results based on the selected analysis type"""123 unmodified linesreturn {}def run_demo_analysis(analysis_type, selected_indicators):"""Run demo analysis based on selected type"""with st.spinner(f"Running {analysis_type.lower()} analysis with demo data..."):try:# Simulate analysis with demo dataimport timetime.sleep(2) # Simulate processing time# Generate demo results based on analysis typeif analysis_type == "Comprehensive":demo_results = {'forecasting': {'GDPC1': {'backtest': {'mape': 2.1, 'rmse': 0.045},'forecast': [21847, 22123, 22401, 22682]},'INDPRO': {'backtest': {'mape': 1.8, 'rmse': 0.032},'forecast': [102.4, 103.1, 103.8, 104.5]},'RSAFS': {'backtest': {'mape': 2.5, 'rmse': 0.078},'forecast': [579.2, 584.7, 590.3, 595.9]}},'segmentation': {'time_period_clusters': {'n_clusters': 3},'series_clusters': {'n_clusters': 4}},'statistical_modeling': {'correlation': {'significant_correlations': ['GDPC1-INDPRO: 0.85','GDPC1-RSAFS: 0.78','CPIAUCSL-FEDFUNDS: 0.65']}},'insights': {'key_findings': ['Strong correlation between GDP and Industrial Production (0.85)','Inflation showing signs of moderation','Federal Reserve policy rate at 22-year high','Labor market remains tight with low unemployment','Consumer spending resilient despite inflation']}}elif analysis_type == "Forecasting Only":demo_results = {'forecasting': {'GDPC1': {'backtest': {'mape': 2.1, 'rmse': 0.045},'forecast': [21847, 22123, 22401, 22682]},'INDPRO': {'backtest': {'mape': 1.8, 'rmse': 0.032},'forecast': [102.4, 103.1, 103.8, 104.5]}},'insights': {'key_findings': ['Forecasting analysis completed successfully','Time series models applied to selected indicators','Forecast accuracy metrics calculated','Confidence intervals generated']}}elif analysis_type == "Segmentation Only":demo_results = {'segmentation': {'time_period_clusters': {'n_clusters': 3},'series_clusters': {'n_clusters': 4}},'insights': {'key_findings': ['Segmentation analysis completed successfully','Economic regimes identified','Series clustering performed','Pattern recognition applied']}}elif analysis_type == "Statistical Only":demo_results = {'statistical_modeling': {'correlation': {'significant_correlations': ['GDPC1-INDPRO: 0.85','GDPC1-RSAFS: 0.78','CPIAUCSL-FEDFUNDS: 0.65']}},'insights': {'key_findings': ['Statistical analysis completed successfully','Correlation analysis performed','Significance testing completed','Statistical models validated']}}else:demo_results = {}st.success(f"✅ Demo {analysis_type.lower()} analysis completed successfully!")# Display resultsdisplay_analysis_results(demo_results)except Exception as e:st.error(f"❌ Demo analysis failed: {e}")def display_analysis_results(results):"""Display comprehensive analysis results with download options"""st.markdown("""65 unmodified lines# Generate downloadable reportsimport jsonimport io# Create JSON reportreport_data = {189 unmodified lines# Check if AWS clients are available and test bucket accessif s3_client is None:st.subheader("Demo Reports & Insights")st.info("📊 Showing demo reports (AWS not configured)")show_demo_reports = Trueelse:# Test if we can actually access the S3 buckettry:s3_client.head_bucket(Bucket=config['s3_bucket'])st.success(f"✅ Connected to S3 bucket: {config['s3_bucket']}")show_demo_reports = Falseexcept Exception as e:st.warning(f"⚠️ AWS connected but bucket '{config['s3_bucket']}' not accessible: {str(e)}")st.info("📊 Showing demo reports (S3 bucket not accessible)")show_demo_reports = True# Show demo reports if neededif show_demo_reports:demo_reports = [{'title': 'Economic Outlook Q4 2024','date': '2024-12-15','summary': 'Comprehensive analysis of economic indicators and forecasts','insights': ['GDP growth expected to moderate to 2.1% in Q4','Inflation continuing to moderate from peak levels','Federal Reserve likely to maintain current policy stance','Labor market remains tight with strong job creation','Consumer spending resilient despite inflation pressures']},{'title': 'Monetary Policy Analysis','date': '2024-12-10','summary': 'Analysis of Federal Reserve policy and market implications','insights': ['Federal Funds Rate at 22-year high of 5.25%','Yield curve inversion persists, signaling economic uncertainty','Inflation expectations well-anchored around 2%','Financial conditions tightening as intended','Policy normalization expected to begin in 2025']},{'title': 'Labor Market Trends','date': '2024-12-05','summary': 'Analysis of employment and wage trends','insights': ['Unemployment rate at 3.7%, near historic lows','Nonfarm payrolls growing at steady pace','Wage growth moderating but still above pre-pandemic levels','Labor force participation improving gradually','Skills mismatch remains a challenge in certain sectors']}]for i, report in enumerate(demo_reports):with st.expander(f"📊 {report['title']} - {report['date']}"):st.markdown(f"**Summary:** {report['summary']}")st.markdown("**Key Insights:**")for insight in report['insights']:st.markdown(f"• {insight}")else:# Try to get real reports from S3reports = get_available_reports(s3_client, config['s3_bucket'])if reports:st.subheader("Available Reports")for report in reports[:5]: # Show last 5 reportswith st.expander(f"Report: {report['key']} - {report['last_modified'].strftime('%Y-%m-%d %H:%M')}"):report_data = get_report_data(s3_client, config['s3_bucket'], report['key'])if report_data:st.json(report_data)else:st.info("No reports available. Run an analysis to generate reports.")def show_downloads_page(s3_client, config):"""Show comprehensive downloads page with reports and visualizations"""4 unmodified lines</div>""", unsafe_allow_html=True)# Create tabs for different download typestab1, tab2, tab3, tab4 = st.tabs(["📊 Visualizations", "📄 Reports", "📈 Analysis Data", "📦 Bulk Downloads"])101 unmodified linesst.subheader("📄 Analysis Reports")st.info("Download comprehensive analysis reports in various formats")# Generate sample reports for downloadimport jsonimport iofrom datetime import datetime# Sample analysis reportsample_report = {'analysis_timestamp': datetime.now().isoformat(),'summary': {'gdp_growth': '2.1%','inflation_rate': '3.2%','unemployment_rate': '3.7%','industrial_production': '+0.8%'},'key_findings': ['GDP growth remains steady at 2.1%','Inflation continues to moderate from peak levels','Labor market remains tight with strong job creation','Industrial production shows positive momentum'],'risk_factors': ['Geopolitical tensions affecting supply chains','Federal Reserve policy uncertainty','Consumer spending patterns changing'],'opportunities': ['Strong domestic manufacturing growth','Technology sector expansion','Green energy transition investments']}col1, col2, col3 = st.columns(3)with col1:# JSON Reportjson_report = json.dumps(sample_report, indent=2)st.download_button(label="📄 Download JSON Report",data=json_report,file_name=f"economic_analysis_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",mime="application/json")st.write("Comprehensive analysis data in JSON format")with col2:# CSV Summarycsv_data = io.StringIO()csv_data.write("Metric,Value\n")csv_data.write(f"GDP Growth,{sample_report['summary']['gdp_growth']}\n")csv_data.write(f"Inflation Rate,{sample_report['summary']['inflation_rate']}\n")csv_data.write(f"Unemployment Rate,{sample_report['summary']['unemployment_rate']}\n")csv_data.write(f"Industrial Production,{sample_report['summary']['industrial_production']}\n")st.download_button(label="📊 Download CSV Summary",data=csv_data.getvalue(),file_name=f"economic_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",mime="text/csv")st.write("Key metrics in spreadsheet format")with col3:# Text Reporttext_report = f"""ECONOMIC ANALYSIS REPORTGenerated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}SUMMARY METRICS:- GDP Growth: {sample_report['summary']['gdp_growth']}- Inflation Rate: {sample_report['summary']['inflation_rate']}- Unemployment Rate: {sample_report['summary']['unemployment_rate']}- Industrial Production: {sample_report['summary']['industrial_production']}KEY FINDINGS:{chr(10).join([f"• {finding}" for finding in sample_report['key_findings']])}RISK FACTORS:{chr(10).join([f"• {risk}" for risk in sample_report['risk_factors']])}OPPORTUNITIES:{chr(10).join([f"• {opp}" for opp in sample_report['opportunities']])}"""st.download_button(label="📝 Download Text Report",data=text_report,file_name=f"economic_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",mime="text/plain")st.write("Human-readable analysis report")with tab3:st.subheader("📈 Analysis Data")st.info("Download raw data and analysis results for further processing")# Generate sample data filesimport pandas as pdimport numpy as np# Sample economic datadates = pd.date_range('2020-01-01', periods=100, freq='D')economic_data = pd.DataFrame({'GDP': np.random.normal(100, 5, 100).cumsum(),'Inflation': np.random.normal(2, 0.5, 100),'Unemployment': np.random.normal(5, 1, 100),'Industrial_Production': np.random.normal(50, 3, 100)}, index=dates)col1, col2 = st.columns(2)with col1:# CSV Datacsv_data = economic_data.to_csv()st.download_button(label="📊 Download CSV Data",data=csv_data,file_name=f"economic_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",mime="text/csv")st.write("Raw economic time series data")with col2:# Excel Dataexcel_buffer = io.BytesIO()with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:economic_data.to_excel(writer, sheet_name='Economic_Data')# Add summary sheetsummary_df = pd.DataFrame({'Metric': ['Mean', 'Std', 'Min', 'Max'],'GDP': [economic_data['GDP'].mean(), economic_data['GDP'].std(), economic_data['GDP'].min(), economic_data['GDP'].max()],'Inflation': [economic_data['Inflation'].mean(), economic_data['Inflation'].std(), economic_data['Inflation'].min(), economic_data['Inflation'].max()],'Unemployment': [economic_data['Unemployment'].mean(), economic_data['Unemployment'].std(), economic_data['Unemployment'].min(), economic_data['Unemployment'].max()]})summary_df.to_excel(writer, sheet_name='Summary', index=False)excel_buffer.seek(0)st.download_button(label="📈 Download Excel Data",data=excel_buffer.getvalue(),file_name=f"economic_analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")st.write("Multi-sheet Excel workbook with data and summary")with tab4:st.subheader("📦 Bulk Downloads")st.info("Download all available files in one package")# Create a zip file with all available dataimport zipfileimport tempfile2 unmodified lineszip_buffer = io.BytesIO()with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:# Add sample reportszip_file.writestr('reports/economic_analysis.json', json.dumps(sample_report, indent=2))zip_file.writestr('reports/economic_summary.csv', csv_data)zip_file.writestr('reports/economic_report.txt', text_report)# Add sample datazip_file.writestr('data/economic_data.csv', economic_data.to_csv())# Add sample visualizations (if available)try:charts = chart_gen.list_available_charts()for i, chart in enumerate(charts[:5]): # Add first 5 charts48 unmodified linesst.success("✅ FRED API Key Configured")st.info("🎯 Real economic data is being used for analysis.")else:st.warning("⚠️ FRED API Key Not Configured")st.info("📊 Demo data is being used for demonstration.")# Setup instructionswith st.expander("🔧 How to Set Up FRED API"):34 unmodified linesst.write(f"API Endpoint: {config['api_endpoint']}")st.write(f"Analytics Available: {ANALYTICS_AVAILABLE}")st.write(f"Real Data Mode: {REAL_DATA_MODE}")st.write(f"Demo Mode: {DEMO_MODE}")# Data Source Informationst.subheader("Data Sources")36 unmodified lines37383940414246 unmodified lines899091929394316 unmodified lines41141241341441541641741841942042142242342440 unmodified lines46546646746846947047142 unmodified lines51451551651751851952052152252373 unmodified lines59759859960060160260360460560618 unmodified lines62562662762862963063144 unmodified lines67667767867968068168268368472 unmodified lines757758759760761762763764765766123 unmodified lines89089189289389489565 unmodified lines961962963964965966967189 unmodified lines1157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811894 unmodified lines11941195119611971198119912001201120212031204101 unmodified lines13061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914202 unmodified lines142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145348 unmodified lines1502150315041505150615071508150934 unmodified lines154415451546154715481549155036 unmodified linesreturn requests# Initialize flagsANALYTICS_AVAILABLE = FalseFRED_API_AVAILABLE = FalseCONFIG_AVAILABLE = False46 unmodified linesREAL_DATA_MODE = FRED_API_KEY and FRED_API_KEY != 'your-fred-api-key-here'return False# Custom CSS for enterprise stylingst.markdown("""<style>316 unmodified lines# Initialize AWS clientss3_client, lambda_client = init_aws_clients()config = load_config()# Show data mode infoif REAL_DATA_MODE:st.success("🎯 Using real FRED API data for live economic insights.")else:st.error("❌ FRED API key not configured. Please set FRED_API_KEY environment variable.")st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html")return# Sidebarwith st.sidebar:40 unmodified linesif REAL_DATA_MODE and FRED_API_AVAILABLE:# Get real insights from FRED APItry:load_fred_client()insights = generate_real_insights(FRED_API_KEY)with col1:42 unmodified linesexcept Exception as e:st.error(f"Failed to fetch real data: {e}")st.info("Please check your FRED API key configuration.")else:st.error("❌ FRED API not available. Please configure your FRED API key.")st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html")# Recent analysis sectionst.markdown("""73 unmodified lines</div>""", unsafe_allow_html=True)if not REAL_DATA_MODE:st.error("❌ FRED API key not configured. Please set FRED_API_KEY environment variable.")st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html")return# Analysis configurationst.markdown("""18 unmodified lines)# Date rangefrom datetime import datetime, timedeltaend_date = datetime.now()start_date = end_date - timedelta(days=365*5) # 5 years44 unmodified lines# Run real analysis with FRED API datawith st.spinner(analysis_message):try:# Load FRED clientload_fred_client()# Get real economic datareal_data = get_real_economic_data(FRED_API_KEY,start_date_input.strftime('%Y-%m-%d'),72 unmodified linesexcept Exception as e:st.error(f"❌ Real data analysis failed: {e}")st.info("Please check your FRED API key and try again.")else:st.error("❌ FRED API not available. Please configure your FRED API key.")st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html")def generate_analysis_results(analysis_type, real_data, selected_indicators):"""Generate analysis results based on the selected analysis type"""123 unmodified linesreturn {}def display_analysis_results(results):"""Display comprehensive analysis results with download options"""st.markdown("""65 unmodified lines# Generate downloadable reportsimport jsonimport iofrom datetime import datetime# Create JSON reportreport_data = {189 unmodified lines# Check if AWS clients are available and test bucket accessif s3_client is None:st.error("❌ AWS S3 not configured. Please configure AWS credentials to access reports.")st.info("Reports are stored in AWS S3. Configure your AWS credentials to access them.")returnelse:# Test if we can actually access the S3 buckettry:s3_client.head_bucket(Bucket=config['s3_bucket'])st.success(f"✅ Connected to S3 bucket: {config['s3_bucket']}")except Exception as e:st.error(f"❌ Cannot access S3 bucket '{config['s3_bucket']}': {str(e)}")st.info("Please check your AWS credentials and bucket configuration.")return# Try to get real reports from S3reports = get_available_reports(s3_client, config['s3_bucket'])if reports:st.subheader("Available Reports")for report in reports[:10]: # Show last 10 reportswith st.expander(f"Report: {report['key']} - {report['last_modified'].strftime('%Y-%m-%d %H:%M')}"):report_data = get_report_data(s3_client, config['s3_bucket'], report['key'])if report_data:st.json(report_data)else:st.info("No reports available. Run an analysis to generate reports.")st.info("Reports will be automatically generated when you run advanced analytics.")def show_downloads_page(s3_client, config):"""Show comprehensive downloads page with reports and visualizations"""4 unmodified lines</div>""", unsafe_allow_html=True)if not REAL_DATA_MODE:st.error("❌ FRED API key not configured. Please set FRED_API_KEY environment variable.")st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html")return# Create tabs for different download typestab1, tab2, tab3, tab4 = st.tabs(["📊 Visualizations", "📄 Reports", "📈 Analysis Data", "📦 Bulk Downloads"])101 unmodified linesst.subheader("📄 Analysis Reports")st.info("Download comprehensive analysis reports in various formats")if s3_client is None:st.error("❌ AWS S3 not configured. Reports are stored in AWS S3.")st.info("Configure your AWS credentials to access reports.")return# Try to get real reports from S3reports = get_available_reports(s3_client, config['s3_bucket'])if reports:st.success(f"✅ Found {len(reports)} reports available for download")for i, report in enumerate(reports[:10]): # Show last 10 reportscol1, col2 = st.columns([3, 1])with col1:st.write(f"**{report['key']}**")st.write(f"Size: {report['size']:,} bytes | Modified: {report['last_modified'].strftime('%Y-%m-%d %H:%M')}")with col2:try:report_data = get_report_data(s3_client, config['s3_bucket'], report['key'])if report_data:import jsonjson_data = json.dumps(report_data, indent=2)st.download_button(label="📥 Download",data=json_data,file_name=f"{report['key']}.json",mime="application/json",key=f"report_{i}")except Exception as e:st.error("❌ Download failed")else:st.info("No reports available. Run an analysis to generate reports.")with tab3:st.subheader("📈 Analysis Data")st.info("Download raw data and analysis results for further processing")if not REAL_DATA_MODE:st.error("❌ No real data available. Please configure your FRED API key.")return# Generate real economic data filesimport pandas as pdimport numpy as npfrom datetime import datetime, timedeltatry:# Load FRED client and get real dataload_fred_client()real_data = get_real_economic_data(FRED_API_KEY,(datetime.now() - timedelta(days=365)).strftime('%Y-%m-%d'),datetime.now().strftime('%Y-%m-%d'))# Convert to DataFrameif real_data and 'data' in real_data:economic_data = pd.DataFrame(real_data['data'])col1, col2 = st.columns(2)with col1:# CSV Datacsv_data = economic_data.to_csv()st.download_button(label="📊 Download CSV Data",data=csv_data,file_name=f"fred_economic_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",mime="text/csv")st.write("Raw FRED economic time series data")with col2:# Excel Dataexcel_buffer = io.BytesIO()with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:economic_data.to_excel(writer, sheet_name='Economic_Data')# Add summary sheetsummary_df = pd.DataFrame({'Metric': ['Mean', 'Std', 'Min', 'Max'],'Value': [economic_data.mean().mean(), economic_data.std().mean(), economic_data.min().min(), economic_data.max().max()]})summary_df.to_excel(writer, sheet_name='Summary', index=False)excel_buffer.seek(0)st.download_button(label="📈 Download Excel Data",data=excel_buffer.getvalue(),file_name=f"fred_economic_analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx",mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")st.write("Multi-sheet Excel workbook with FRED data and summary")else:st.error("❌ Could not retrieve real economic data.")st.info("Please check your FRED API key and try again.")except Exception as e:st.error(f"❌ Failed to generate data files: {e}")st.info("Please check your FRED API key and try again.")with tab4:st.subheader("📦 Bulk Downloads")st.info("Download all available files in one package")if not REAL_DATA_MODE:st.error("❌ No real data available for bulk download.")return# Create a zip file with all available dataimport zipfileimport tempfile2 unmodified lineszip_buffer = io.BytesIO()with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:# Add real reports if availableif s3_client:reports = get_available_reports(s3_client, config['s3_bucket'])for i, report in enumerate(reports[:5]): # Add first 5 reportstry:report_data = get_report_data(s3_client, config['s3_bucket'], report['key'])if report_data:import jsonzip_file.writestr(f'reports/{report["key"]}.json', json.dumps(report_data, indent=2))except Exception:continue# Add real data if availabletry:load_fred_client()real_data = get_real_economic_data(FRED_API_KEY,(datetime.now() - timedelta(days=365)).strftime('%Y-%m-%d'),datetime.now().strftime('%Y-%m-%d'))if real_data and 'data' in real_data:economic_data = pd.DataFrame(real_data['data'])zip_file.writestr('data/fred_economic_data.csv', economic_data.to_csv())except Exception:pass# Add visualizations if availabletry:charts = chart_gen.list_available_charts()for i, chart in enumerate(charts[:5]): # Add first 5 charts48 unmodified linesst.success("✅ FRED API Key Configured")st.info("🎯 Real economic data is being used for analysis.")else:st.error("❌ FRED API Key Not Configured")st.info("📊 Please configure your FRED API key to access real economic data.")# Setup instructionswith st.expander("🔧 How to Set Up FRED API"):34 unmodified linesst.write(f"API Endpoint: {config['api_endpoint']}")st.write(f"Analytics Available: {ANALYTICS_AVAILABLE}")st.write(f"Real Data Mode: {REAL_DATA_MODE}")st.write(f"FRED API Available: {FRED_API_AVAILABLE}")# Data Source Informationst.subheader("Data Sources")
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288"""FRED ML - Demo Data GeneratorProvides realistic economic data and senior data scientist insights"""
import pandas as pdimport numpy as npfrom datetime import datetime, timedeltaimport random
def generate_economic_data(): """Generate realistic economic data for demonstration""" # Generate date range (last 5 years) end_date = datetime.now() start_date = end_date - timedelta(days=365*5) dates = pd.date_range(start=start_date, end=end_date, freq='ME') # Base values and trends for realistic economic data base_values = { 'GDPC1': 20000, # Real GDP in billions 'INDPRO': 100, # Industrial Production Index 'RSAFS': 500, # Retail Sales in billions 'CPIAUCSL': 250, # Consumer Price Index 'FEDFUNDS': 2.5, # Federal Funds Rate 'DGS10': 3.0, # 10-Year Treasury Rate 'UNRATE': 4.0, # Unemployment Rate 'PAYEMS': 150000, # Total Nonfarm Payrolls (thousands) 'PCE': 18000, # Personal Consumption Expenditures 'M2SL': 21000, # M2 Money Stock 'TCU': 75, # Capacity Utilization 'DEXUSEU': 1.1 # US/Euro Exchange Rate } # Growth rates and volatility for realistic trends growth_rates = { 'GDPC1': 0.02, # 2% annual growth 'INDPRO': 0.015, # 1.5% annual growth 'RSAFS': 0.03, # 3% annual growth 'CPIAUCSL': 0.025, # 2.5% annual inflation 'FEDFUNDS': 0.0, # Policy rate 'DGS10': 0.0, # Market rate 'UNRATE': 0.0, # Unemployment 'PAYEMS': 0.015, # Employment growth 'PCE': 0.025, # Consumption growth 'M2SL': 0.04, # Money supply growth 'TCU': 0.005, # Capacity utilization 'DEXUSEU': 0.0 # Exchange rate } # Generate realistic data data = {'Date': dates} for indicator, base_value in base_values.items(): # Create trend with realistic economic cycles trend = np.linspace(0, len(dates) * growth_rates[indicator], len(dates)) # Add business cycle effects cycle = 0.05 * np.sin(2 * np.pi * np.arange(len(dates)) / 48) # 4-year cycle # Add random noise noise = np.random.normal(0, 0.02, len(dates)) # Combine components values = base_value * (1 + trend + cycle + noise) # Ensure realistic bounds if indicator in ['UNRATE', 'FEDFUNDS', 'DGS10']: values = np.clip(values, 0, 20) elif indicator in ['CPIAUCSL']: values = np.clip(values, 200, 350) elif indicator in ['TCU']: values = np.clip(values, 60, 90) data[indicator] = values return pd.DataFrame(data)
def generate_insights(): """Generate senior data scientist insights""" insights = { 'GDPC1': { 'current_value': '$21,847.2B', 'growth_rate': '+2.1%', 'trend': 'Moderate growth', 'forecast': '+2.3% next quarter', 'key_insight': 'GDP growth remains resilient despite monetary tightening, supported by strong consumer spending and business investment.', 'risk_factors': ['Inflation persistence', 'Geopolitical tensions', 'Supply chain disruptions'], 'opportunities': ['Technology sector expansion', 'Infrastructure investment', 'Green energy transition'] }, 'INDPRO': { 'current_value': '102.4', 'growth_rate': '+0.8%', 'trend': 'Recovery phase', 'forecast': '+0.6% next month', 'key_insight': 'Industrial production shows signs of recovery, with manufacturing leading the rebound. Capacity utilization improving.', 'risk_factors': ['Supply chain bottlenecks', 'Labor shortages', 'Energy price volatility'], 'opportunities': ['Advanced manufacturing', 'Automation adoption', 'Reshoring initiatives'] }, 'RSAFS': { 'current_value': '$579.2B', 'growth_rate': '+3.2%', 'trend': 'Strong consumer spending', 'forecast': '+2.8% next month', 'key_insight': 'Retail sales demonstrate robust consumer confidence, with e-commerce continuing to gain market share.', 'risk_factors': ['Inflation impact on purchasing power', 'Interest rate sensitivity', 'Supply chain issues'], 'opportunities': ['Digital transformation', 'Omnichannel retail', 'Personalization'] }, 'CPIAUCSL': { 'current_value': '312.3', 'growth_rate': '+3.2%', 'trend': 'Moderating inflation', 'forecast': '+2.9% next month', 'key_insight': 'Inflation continues to moderate from peak levels, with core CPI showing signs of stabilization.', 'risk_factors': ['Energy price volatility', 'Wage pressure', 'Supply chain costs'], 'opportunities': ['Productivity improvements', 'Technology adoption', 'Supply chain optimization'] }, 'FEDFUNDS': { 'current_value': '5.25%', 'growth_rate': '0%', 'trend': 'Stable policy rate', 'forecast': '5.25% next meeting', 'key_insight': 'Federal Reserve maintains restrictive stance to combat inflation, with policy rate at 22-year high.', 'risk_factors': ['Inflation persistence', 'Economic slowdown', 'Financial stability'], 'opportunities': ['Policy normalization', 'Inflation targeting', 'Financial regulation'] }, 'DGS10': { 'current_value': '4.12%', 'growth_rate': '-0.15%', 'trend': 'Declining yields', 'forecast': '4.05% next week', 'key_insight': '10-year Treasury yields declining on economic uncertainty and flight to quality. Yield curve inversion persists.', 'risk_factors': ['Economic recession', 'Inflation expectations', 'Geopolitical risks'], 'opportunities': ['Bond market opportunities', 'Portfolio diversification', 'Interest rate hedging'] }, 'UNRATE': { 'current_value': '3.7%', 'growth_rate': '0%', 'trend': 'Stable employment', 'forecast': '3.6% next month', 'key_insight': 'Unemployment rate remains near historic lows, indicating tight labor market conditions.', 'risk_factors': ['Labor force participation', 'Skills mismatch', 'Economic slowdown'], 'opportunities': ['Workforce development', 'Technology training', 'Remote work adoption'] }, 'PAYEMS': { 'current_value': '156,847K', 'growth_rate': '+1.2%', 'trend': 'Steady job growth', 'forecast': '+0.8% next month', 'key_insight': 'Nonfarm payrolls continue steady growth, with healthcare and technology sectors leading job creation.', 'risk_factors': ['Labor shortages', 'Wage pressure', 'Economic uncertainty'], 'opportunities': ['Skills development', 'Industry partnerships', 'Immigration policy'] }, 'PCE': { 'current_value': '$19,847B', 'growth_rate': '+2.8%', 'trend': 'Strong consumption', 'forecast': '+2.5% next quarter', 'key_insight': 'Personal consumption expenditures show resilience, supported by strong labor market and wage growth.', 'risk_factors': ['Inflation impact', 'Interest rate sensitivity', 'Consumer confidence'], 'opportunities': ['Digital commerce', 'Experience economy', 'Sustainable consumption'] }, 'M2SL': { 'current_value': '$20,847B', 'growth_rate': '+2.1%', 'trend': 'Moderate growth', 'forecast': '+1.8% next month', 'key_insight': 'Money supply growth moderating as Federal Reserve tightens monetary policy to combat inflation.', 'risk_factors': ['Inflation expectations', 'Financial stability', 'Economic growth'], 'opportunities': ['Digital payments', 'Financial innovation', 'Monetary policy'] }, 'TCU': { 'current_value': '78.4%', 'growth_rate': '+0.3%', 'trend': 'Improving utilization', 'forecast': '78.7% next quarter', 'key_insight': 'Capacity utilization improving as supply chain issues resolve and demand remains strong.', 'risk_factors': ['Supply chain disruptions', 'Labor shortages', 'Energy constraints'], 'opportunities': ['Efficiency improvements', 'Technology adoption', 'Process optimization'] }, 'DEXUSEU': { 'current_value': '1.087', 'growth_rate': '+0.2%', 'trend': 'Stable exchange rate', 'forecast': '1.085 next week', 'key_insight': 'US dollar remains strong against euro, supported by relative economic performance and interest rate differentials.', 'risk_factors': ['Economic divergence', 'Geopolitical tensions', 'Trade policies'], 'opportunities': ['Currency hedging', 'International trade', 'Investment diversification'] } } return insights
def generate_forecast_data(): """Generate forecast data with confidence intervals""" # Generate future dates (next 4 quarters) last_date = datetime.now() future_dates = pd.date_range(start=last_date + timedelta(days=90), periods=4, freq='QE') forecasts = {} # Realistic forecast scenarios forecast_scenarios = { 'GDPC1': {'growth': 0.02, 'volatility': 0.01}, # 2% quarterly growth 'INDPRO': {'growth': 0.015, 'volatility': 0.008}, # 1.5% monthly growth 'RSAFS': {'growth': 0.025, 'volatility': 0.012}, # 2.5% monthly growth 'CPIAUCSL': {'growth': 0.006, 'volatility': 0.003}, # 0.6% monthly inflation 'FEDFUNDS': {'growth': 0.0, 'volatility': 0.25}, # Stable policy rate 'DGS10': {'growth': -0.001, 'volatility': 0.15}, # Slight decline 'UNRATE': {'growth': -0.001, 'volatility': 0.1}, # Slight decline 'PAYEMS': {'growth': 0.008, 'volatility': 0.005}, # 0.8% monthly growth 'PCE': {'growth': 0.02, 'volatility': 0.01}, # 2% quarterly growth 'M2SL': {'growth': 0.015, 'volatility': 0.008}, # 1.5% monthly growth 'TCU': {'growth': 0.003, 'volatility': 0.002}, # 0.3% quarterly growth 'DEXUSEU': {'growth': -0.001, 'volatility': 0.02} # Slight decline } for indicator, scenario in forecast_scenarios.items(): base_value = 100 # Normalized base value # Generate forecast values forecast_values = [] confidence_intervals = [] for i in range(4): # Add trend and noise value = base_value * (1 + scenario['growth'] * (i + 1) + np.random.normal(0, scenario['volatility'])) # Generate confidence interval lower = value * (1 - 0.05 - np.random.uniform(0, 0.03)) upper = value * (1 + 0.05 + np.random.uniform(0, 0.03)) forecast_values.append(value) confidence_intervals.append({'lower': lower, 'upper': upper}) forecasts[indicator] = { 'forecast': forecast_values, 'confidence_intervals': pd.DataFrame(confidence_intervals), 'dates': future_dates } return forecasts
def generate_correlation_matrix(): """Generate realistic correlation matrix""" # Define realistic correlations between economic indicators correlations = { 'GDPC1': {'INDPRO': 0.85, 'RSAFS': 0.78, 'CPIAUCSL': 0.45, 'FEDFUNDS': -0.32, 'DGS10': -0.28}, 'INDPRO': {'RSAFS': 0.72, 'CPIAUCSL': 0.38, 'FEDFUNDS': -0.25, 'DGS10': -0.22}, 'RSAFS': {'CPIAUCSL': 0.42, 'FEDFUNDS': -0.28, 'DGS10': -0.25}, 'CPIAUCSL': {'FEDFUNDS': 0.65, 'DGS10': 0.58}, 'FEDFUNDS': {'DGS10': 0.82} } # Create correlation matrix indicators = ['GDPC1', 'INDPRO', 'RSAFS', 'CPIAUCSL', 'FEDFUNDS', 'DGS10', 'UNRATE', 'PAYEMS', 'PCE', 'M2SL', 'TCU', 'DEXUSEU'] corr_matrix = pd.DataFrame(index=indicators, columns=indicators) # Fill diagonal with 1 for indicator in indicators: corr_matrix.loc[indicator, indicator] = 1.0 # Fill with realistic correlations for i, indicator1 in enumerate(indicators): for j, indicator2 in enumerate(indicators): if i != j: if indicator1 in correlations and indicator2 in correlations[indicator1]: corr_matrix.loc[indicator1, indicator2] = correlations[indicator1][indicator2] elif indicator2 in correlations and indicator1 in correlations[indicator2]: corr_matrix.loc[indicator1, indicator2] = correlations[indicator2][indicator1] else: # Generate random correlation between -0.3 and 0.3 corr_matrix.loc[indicator1, indicator2] = np.random.uniform(-0.3, 0.3) return corr_matrix
def get_demo_data(): """Get comprehensive demo data""" return { 'economic_data': generate_economic_data(), 'insights': generate_insights(), 'forecasts': generate_forecast_data(), 'correlation_matrix': generate_correlation_matrix() } frontend/app.py+161-445
frontend/demo_data.pyDeleted-288