Update scripts/app.py
Browse files527 unmodified lines528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602527 unmodified linescustom_css = """.metric-box { background-color: #1f2937; padding: 20px; border-radius: 12px; border: 1px solid #374151; text-align: center; }.metric-label { font-size: 1.1em; color: #9ca3af; margin-bottom: 5px; }.metric-value { font-size: 2.2em; font-weight: 700; color: #e5e7eb; }.disclaimer-box { background-color: #374151; padding: 15px; border-radius: 8px; border-left: 4px solid #f59e0b; color: #d1d5db; font-size: 0.9em; margin-bottom: 20px; }"""with gr.Blocks(css=custom_css, title="Deep RL Portfolio Manager") as demo:# JS to force dark modegr.HTML("""<script>function forceDark(){document.body.classList.add('dark');} forceDark(); setTimeout(forceDark, 500);</script>""")gr.Markdown("# ๐ง Deep RL & LLM Portfolio Manager")with gr.Tabs():with gr.TabItem("๐ Live Dashboard"):nw_val, dc_val = get_dashboard_metrics()with gr.Row():gr.HTML(f"<div class='metric-box'><div class='metric-label'>Current Net Worth</div><div class='metric-value'>{nw_val}</div></div>")gr.HTML(f"<div class='metric-box'><div class='metric-label'>24h Change</div><div class='metric-value' style='color: #10b981;'>{dc_val}</div></div>")with gr.Row():with gr.Column(scale=3): history_chart = gr.Plot(value=get_portfolio_history_plot())with gr.Row():with gr.Column(scale=1): allocation_chart = gr.Plot(value=get_current_allocation_plot())with gr.Column(scale=2):gr.Markdown("### Recent Transactions")transactions_table = gr.Dataframe(value=get_recent_transactions(), interactive=False)with gr.TabItem("๐ฎ Forecast & AI Analysis"):gr.Markdown("### Generate Tomorrow's Strategy")run_btn = gr.Button("๐ Run Overnight Analysis", variant="primary", size="lg")status_output = gr.Textbox(label="System Status", interactive=False, lines=1)gr.Markdown("---")with gr.Row():with gr.Column(scale=2):gr.Markdown("### ๐ Suggested Position")allocation_output = gr.Dataframe(headers=["Asset", "Allocation"], interactive=False)gr.Markdown("### ๐ง XAI Feature Importance")xai_output_plot = gr.Plot(label="Influential Factors", show_label=False)with gr.Column(scale=3):analysis_report_html = gr.HTML(label="AI Report")run_btn.click(fn=predict_and_analyze, inputs=None, outputs=[status_output, allocation_output, xai_output_plot, analysis_report_html])with gr.TabItem("๐ Historical Data Analyst"):gr.Markdown("### Analyze Past Market Performance")with gr.Row():with gr.Column(scale=1):all_tickers = ASSETS + list(FRED_IDS.values())avail_tickers = [t for t in all_tickers if DASHBOARD_DATA_DF is not None and t in DASHBOARD_DATA_DF.columns]asset_selector = gr.Dropdown(choices=avail_tickers, value=avail_tickers[:3] if avail_tickers else [], multiselect=True, label="Select Assets")period_selector = gr.Dropdown(choices=list(TIME_PERIODS.keys()), value="1 Year", label="Select Period")analyze_btn = gr.Button("๐ Run Analysis", variant="primary")with gr.Column(scale=3):historical_plot = gr.Plot(label="Performance Plot")gr.Markdown("---")historical_analysis_md = gr.Markdown("### ๐ค AI Analyst Report\n\n*Click 'Run Analysis' to generate.*")analyze_btn.click(fn=run_historical_analysis, inputs=[asset_selector, period_selector], outputs=[historical_plot, historical_analysis_md])with gr.TabItem("๐ Historical Simulation"):gr.Markdown("### Backtest Strategy")gr.HTML(f"<div class='disclaimer-box'>โ ๏ธ Model trained on {TRAIN_START_DATE} to {TRAIN_END_DATE}. Outside usage may be inaccurate.</div>")with gr.Row():with gr.Column(scale=1):start_date_input = gr.Textbox(label="Start Date", value=(datetime.now()-timedelta(days=365)).strftime('%Y-%m-%d'))end_date_input = gr.Textbox(label="End Date", value=(datetime.now()-timedelta(days=1)).strftime('%Y-%m-%d'))sim_btn = gr.Button("โถ๏ธ Run Simulation", variant="primary")sim_status = gr.Textbox(label="Status", interactive=False, lines=1)with gr.Column(scale=3):sim_plot = gr.Plot(label="Performance")gr.Markdown("---")sim_metrics_df = gr.Dataframe(interactive=False, wrap=True, label="Professional Metrics")sim_btn.click(fn=run_historical_simulation, inputs=[start_date_input, end_date_input], outputs=[sim_plot, sim_status, sim_metrics_df])if __name__ == "__main__":demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, share=True)527 unmodified lines528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586527 unmodified linescustom_css = """.metric-box { background-color: #1f2937; padding: 20px; border-radius: 12px; border: 1px solid #374151; text-align: center; }.metric-label { font-size: 1.1em; color: #9ca3af; }.metric-value { font-size: 2.2em; font-weight: 700; color: #e5e7eb; }"""with gr.Blocks(css=custom_css, title="Deep RL Manager") as demo:gr.HTML("""<script>function forceDark(){document.body.classList.add('dark');} forceDark(); setTimeout(forceDark, 500);</script>""")gr.Markdown("# ๐ง Deep RL & LLM Portfolio Manager")with gr.Tabs():with gr.TabItem("๐ Dashboard"):nw, chg = get_dash_metrics()with gr.Row():gr.HTML(f"<div class='metric-box'><div class='metric-label'>Simulated Net Worth</div><div class='metric-value'>{nw}</div></div>")gr.HTML(f"<div class='metric-box'><div class='metric-label'>24h Change</div><div class='metric-value'>{chg}</div></div>")with gr.Row():with gr.Column(scale=3): gr.Plot(value=get_hist_plot(), label="History")with gr.Row():with gr.Column(scale=1): gr.Plot(value=get_current_allocation_plot(), label="Allocation")with gr.Column(scale=2): gr.Dataframe(value=get_trans(), label="Transactions")with gr.TabItem("๐ฎ Forecast"):run_btn = gr.Button("๐ Run Analysis", variant="primary")stat = gr.Textbox(label="Status", lines=1)with gr.Row():with gr.Column(scale=2):alloc_out = gr.Dataframe(headers=["Asset", "Allocation"])xai_out = gr.Plot(label="XAI Factors")with gr.Column(scale=3):ai_out = gr.HTML(label="Report")run_btn.click(predict_and_analyze, None, [stat, alloc_out, xai_out, ai_out])with gr.TabItem("๐ Analyst"):with gr.Row():with gr.Column(scale=1):ticks = ASSETS + FRED_IDS.values() if DASHBOARD_DATA_DF is None else [c for c in ASSETS + list(FRED_IDS.values()) if c in DASHBOARD_DATA_DF.columns]sel = gr.Dropdown(choices=ticks, multiselect=True, label="Assets")per = gr.Dropdown(choices=list(TIME_PERIODS.keys()), value="1 Year", label="Period")btn = gr.Button("Analyze", variant="primary")with gr.Column(scale=3): plot = gr.Plot()rep = gr.Markdown()btn.click(run_historical_analysis, [sel, per], [plot, rep])with gr.TabItem("๐ Simulation"):gr.Markdown("### Backtest")with gr.Row():s_date = gr.Textbox(label="Start", value=(datetime.now()-timedelta(days=365)).strftime('%Y-%m-%d'))e_date = gr.Textbox(label="End", value=(datetime.now()-timedelta(days=1)).strftime('%Y-%m-%d'))s_btn = gr.Button("Run", variant="primary")s_stat = gr.Textbox(label="Status")s_plot = gr.Plot()# FIX: Using Dataframe instead of Markdown for metrics tables_metrics = gr.Dataframe(label="Professional Metrics")s_btn.click(run_historical_simulation, [s_date, e_date], [s_plot, s_stat, s_metrics])if __name__ == "__main__":demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, share=True)
scripts/app.py+36-52