DanielKiani commited on
Commit
f7f28db
ยท
verified ยท
1 Parent(s): 531d3f6

Update scripts/app.py

Browse files
Files changed (1) hide show
  1. scripts/app.py +36 -52
scripts/app.py CHANGED
@@ -528,75 +528,59 @@ def get_recent_transactions():
528
 
529
  custom_css = """
530
  .metric-box { background-color: #1f2937; padding: 20px; border-radius: 12px; border: 1px solid #374151; text-align: center; }
531
- .metric-label { font-size: 1.1em; color: #9ca3af; margin-bottom: 5px; }
532
  .metric-value { font-size: 2.2em; font-weight: 700; color: #e5e7eb; }
533
- .disclaimer-box { background-color: #374151; padding: 15px; border-radius: 8px; border-left: 4px solid #f59e0b; color: #d1d5db; font-size: 0.9em; margin-bottom: 20px; }
534
  """
535
 
536
- with gr.Blocks(css=custom_css, title="Deep RL Portfolio Manager") as demo:
537
- # JS to force dark mode
538
  gr.HTML("""<script>function forceDark(){document.body.classList.add('dark');} forceDark(); setTimeout(forceDark, 500);</script>""")
539
-
540
  gr.Markdown("# ๐Ÿง  Deep RL & LLM Portfolio Manager")
541
 
542
  with gr.Tabs():
543
- with gr.TabItem("๐Ÿ“Š Live Dashboard"):
544
- nw_val, dc_val = get_dashboard_metrics()
545
  with gr.Row():
546
- gr.HTML(f"<div class='metric-box'><div class='metric-label'>Current Net Worth</div><div class='metric-value'>{nw_val}</div></div>")
547
- 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>")
548
  with gr.Row():
549
- with gr.Column(scale=3): history_chart = gr.Plot(value=get_portfolio_history_plot())
550
  with gr.Row():
551
- with gr.Column(scale=1): allocation_chart = gr.Plot(value=get_current_allocation_plot())
552
- with gr.Column(scale=2):
553
- gr.Markdown("### Recent Transactions")
554
- transactions_table = gr.Dataframe(value=get_recent_transactions(), interactive=False)
555
-
556
- with gr.TabItem("๐Ÿ”ฎ Forecast & AI Analysis"):
557
- gr.Markdown("### Generate Tomorrow's Strategy")
558
- run_btn = gr.Button("๐Ÿš€ Run Overnight Analysis", variant="primary", size="lg")
559
- status_output = gr.Textbox(label="System Status", interactive=False, lines=1)
560
- gr.Markdown("---")
561
  with gr.Row():
562
  with gr.Column(scale=2):
563
- gr.Markdown("### ๐Ÿ“ˆ Suggested Position")
564
- allocation_output = gr.Dataframe(headers=["Asset", "Allocation"], interactive=False)
565
- gr.Markdown("### ๐Ÿง  XAI Feature Importance")
566
- xai_output_plot = gr.Plot(label="Influential Factors", show_label=False)
567
  with gr.Column(scale=3):
568
- analysis_report_html = gr.HTML(label="AI Report")
569
- run_btn.click(fn=predict_and_analyze, inputs=None, outputs=[status_output, allocation_output, xai_output_plot, analysis_report_html])
570
 
571
- with gr.TabItem("๐Ÿ“… Historical Data Analyst"):
572
- gr.Markdown("### Analyze Past Market Performance")
573
  with gr.Row():
574
  with gr.Column(scale=1):
575
- all_tickers = ASSETS + list(FRED_IDS.values())
576
- avail_tickers = [t for t in all_tickers if DASHBOARD_DATA_DF is not None and t in DASHBOARD_DATA_DF.columns]
577
- asset_selector = gr.Dropdown(choices=avail_tickers, value=avail_tickers[:3] if avail_tickers else [], multiselect=True, label="Select Assets")
578
- period_selector = gr.Dropdown(choices=list(TIME_PERIODS.keys()), value="1 Year", label="Select Period")
579
- analyze_btn = gr.Button("๐Ÿ”Ž Run Analysis", variant="primary")
580
- with gr.Column(scale=3):
581
- historical_plot = gr.Plot(label="Performance Plot")
582
- gr.Markdown("---")
583
- historical_analysis_md = gr.Markdown("### ๐Ÿค– AI Analyst Report\n\n*Click 'Run Analysis' to generate.*")
584
- analyze_btn.click(fn=run_historical_analysis, inputs=[asset_selector, period_selector], outputs=[historical_plot, historical_analysis_md])
585
-
586
- with gr.TabItem("๐Ÿ”™ Historical Simulation"):
587
- gr.Markdown("### Backtest Strategy")
588
- gr.HTML(f"<div class='disclaimer-box'>โš ๏ธ Model trained on {TRAIN_START_DATE} to {TRAIN_END_DATE}. Outside usage may be inaccurate.</div>")
589
  with gr.Row():
590
- with gr.Column(scale=1):
591
- start_date_input = gr.Textbox(label="Start Date", value=(datetime.now()-timedelta(days=365)).strftime('%Y-%m-%d'))
592
- end_date_input = gr.Textbox(label="End Date", value=(datetime.now()-timedelta(days=1)).strftime('%Y-%m-%d'))
593
- sim_btn = gr.Button("โ–ถ๏ธ Run Simulation", variant="primary")
594
- sim_status = gr.Textbox(label="Status", interactive=False, lines=1)
595
- with gr.Column(scale=3):
596
- sim_plot = gr.Plot(label="Performance")
597
- gr.Markdown("---")
598
- sim_metrics_df = gr.Dataframe(interactive=False, wrap=True, label="Professional Metrics")
599
- sim_btn.click(fn=run_historical_simulation, inputs=[start_date_input, end_date_input], outputs=[sim_plot, sim_status, sim_metrics_df])
600
 
601
  if __name__ == "__main__":
602
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, share=True)
 
528
 
529
  custom_css = """
530
  .metric-box { background-color: #1f2937; padding: 20px; border-radius: 12px; border: 1px solid #374151; text-align: center; }
531
+ .metric-label { font-size: 1.1em; color: #9ca3af; }
532
  .metric-value { font-size: 2.2em; font-weight: 700; color: #e5e7eb; }
 
533
  """
534
 
535
+ with gr.Blocks(css=custom_css, title="Deep RL Manager") as demo:
 
536
  gr.HTML("""<script>function forceDark(){document.body.classList.add('dark');} forceDark(); setTimeout(forceDark, 500);</script>""")
 
537
  gr.Markdown("# ๐Ÿง  Deep RL & LLM Portfolio Manager")
538
 
539
  with gr.Tabs():
540
+ with gr.TabItem("๐Ÿ“Š Dashboard"):
541
+ nw, chg = get_dash_metrics()
542
  with gr.Row():
543
+ gr.HTML(f"<div class='metric-box'><div class='metric-label'>Simulated Net Worth</div><div class='metric-value'>{nw}</div></div>")
544
+ gr.HTML(f"<div class='metric-box'><div class='metric-label'>24h Change</div><div class='metric-value'>{chg}</div></div>")
545
  with gr.Row():
546
+ with gr.Column(scale=3): gr.Plot(value=get_hist_plot(), label="History")
547
  with gr.Row():
548
+ with gr.Column(scale=1): gr.Plot(value=get_current_allocation_plot(), label="Allocation")
549
+ with gr.Column(scale=2): gr.Dataframe(value=get_trans(), label="Transactions")
550
+
551
+ with gr.TabItem("๐Ÿ”ฎ Forecast"):
552
+ run_btn = gr.Button("๐Ÿš€ Run Analysis", variant="primary")
553
+ stat = gr.Textbox(label="Status", lines=1)
 
 
 
 
554
  with gr.Row():
555
  with gr.Column(scale=2):
556
+ alloc_out = gr.Dataframe(headers=["Asset", "Allocation"])
557
+ xai_out = gr.Plot(label="XAI Factors")
 
 
558
  with gr.Column(scale=3):
559
+ ai_out = gr.HTML(label="Report")
560
+ run_btn.click(predict_and_analyze, None, [stat, alloc_out, xai_out, ai_out])
561
 
562
+ with gr.TabItem("๐Ÿ“… Analyst"):
 
563
  with gr.Row():
564
  with gr.Column(scale=1):
565
+ 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]
566
+ sel = gr.Dropdown(choices=ticks, multiselect=True, label="Assets")
567
+ per = gr.Dropdown(choices=list(TIME_PERIODS.keys()), value="1 Year", label="Period")
568
+ btn = gr.Button("Analyze", variant="primary")
569
+ with gr.Column(scale=3): plot = gr.Plot()
570
+ rep = gr.Markdown()
571
+ btn.click(run_historical_analysis, [sel, per], [plot, rep])
572
+
573
+ with gr.TabItem("๐Ÿ”™ Simulation"):
574
+ gr.Markdown("### Backtest")
 
 
 
 
575
  with gr.Row():
576
+ s_date = gr.Textbox(label="Start", value=(datetime.now()-timedelta(days=365)).strftime('%Y-%m-%d'))
577
+ e_date = gr.Textbox(label="End", value=(datetime.now()-timedelta(days=1)).strftime('%Y-%m-%d'))
578
+ s_btn = gr.Button("Run", variant="primary")
579
+ s_stat = gr.Textbox(label="Status")
580
+ s_plot = gr.Plot()
581
+ # FIX: Using Dataframe instead of Markdown for metrics table
582
+ s_metrics = gr.Dataframe(label="Professional Metrics")
583
+ s_btn.click(run_historical_simulation, [s_date, e_date], [s_plot, s_stat, s_metrics])
 
 
584
 
585
  if __name__ == "__main__":
586
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True, share=True)