ekjotsingh commited on
Commit
7598305
·
verified ·
1 Parent(s): d3761a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -15
app.py CHANGED
@@ -1,8 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import storage
3
  import worker
4
  import time
5
- import os
6
  import importlib
7
 
8
  # Initialize DB on launch
@@ -40,28 +59,32 @@ def export_csv():
40
  return None
41
 
42
  def run_backtest_ui():
43
- """Runs the backtest logic properly."""
44
  try:
45
- # Import directly inside function to ensure fresh run
 
 
 
 
46
  import backtest
47
  importlib.reload(backtest)
48
 
49
- # Call the function explicitly
50
- result_path = backtest.run_backtest()
51
 
52
- if result_path and os.path.exists(result_path):
53
- return result_path, "✅ Simulation Complete"
 
54
  else:
55
- return None, "❌ Simulation Failed (Check Logs)"
56
 
57
  except Exception as e:
58
- return None, f"❌ Error: {str(e)}"
59
-
60
 
61
  # --- UI ---
62
  with gr.Blocks(title="Kairo 14.1 Persistent", theme=gr.themes.Base()) as demo:
63
- gr.Markdown("# 🦅 Kairo 14.1: Persistent Hunter")
64
- gr.Markdown("Automatic Indian Market Scanner & Strategy Engine")
65
 
66
  # --- SECTION 1: CONTROLS ---
67
  with gr.Row():
@@ -85,13 +108,13 @@ with gr.Blocks(title="Kairo 14.1 Persistent", theme=gr.themes.Base()) as demo:
85
  )
86
 
87
  # --- SECTION 3: STRATEGY LAB ---
88
- gr.Markdown("### 🧪 Strategy Backtest (1995 - Present)")
89
  with gr.Row():
90
  with gr.Column(scale=1):
91
- sim_btn = gr.Button("Run Simulation (Hybrid Strategy)", variant="secondary")
92
  sim_status = gr.Textbox(label="Simulation Status", value="Idle", interactive=False)
93
  with gr.Column(scale=3):
94
- sim_image = gr.Image(label="Performance Curve", type="filepath")
95
 
96
  # --- EVENT LISTENERS ---
97
 
 
1
+ import os
2
+ import sys
3
+ import asyncio
4
+
5
+ # --- CRITICAL FIX: SILENCE ASYNCIO NOISE ---
6
+ # This block suppresses the "Invalid file descriptor" error caused by Python 3.13 on Spaces
7
+ if sys.platform != "win32":
8
+ selector_events = sys.modules.get("asyncio.selector_events")
9
+ if selector_events:
10
+ # Monkey-patch the _remove_reader method to ignore the specific error
11
+ _original_remove_reader = selector_events.BaseSelectorEventLoop._remove_reader
12
+ def _silent_remove_reader(self, fd):
13
+ try:
14
+ _original_remove_reader(self, fd)
15
+ except ValueError:
16
+ # Ignore "Invalid file descriptor"
17
+ pass
18
+ selector_events.BaseSelectorEventLoop._remove_reader = _silent_remove_reader
19
+ # -------------------------------------------
20
+
21
  import gradio as gr
22
  import storage
23
  import worker
24
  import time
 
25
  import importlib
26
 
27
  # Initialize DB on launch
 
59
  return None
60
 
61
  def run_backtest_ui():
62
+ """Runs the backtest.py script and returns the image."""
63
  try:
64
+ # Check if backtest.py exists
65
+ if not os.path.exists("backtest.py"):
66
+ return None, "⚠️ backtest.py not found. Please create the file first."
67
+
68
+ # Import and reload to ensure we run the latest code
69
  import backtest
70
  importlib.reload(backtest)
71
 
72
+ # Run the engine
73
+ output_file = backtest.backtest_engine()
74
 
75
+ # Check if the image was generated
76
+ if output_file and os.path.exists(output_file):
77
+ return output_file, "✅ Simulation Complete"
78
  else:
79
+ return None, "❌ Simulation finished but no chart found."
80
 
81
  except Exception as e:
82
+ return None, f"❌ Error running backtest: {str(e)}"
 
83
 
84
  # --- UI ---
85
  with gr.Blocks(title="Kairo 14.1 Persistent", theme=gr.themes.Base()) as demo:
86
+ gr.Markdown("# 🦅 Kairo 14.1: Global Momentum Hunter")
87
+ gr.Markdown("Multi-Asset Strategy: Equity, Gold, Silver, Oil.")
88
 
89
  # --- SECTION 1: CONTROLS ---
90
  with gr.Row():
 
108
  )
109
 
110
  # --- SECTION 3: STRATEGY LAB ---
111
+ gr.Markdown("### 🧪 Momentum Rotation Strategy (Equity + Commodities)")
112
  with gr.Row():
113
  with gr.Column(scale=1):
114
+ sim_btn = gr.Button("Run Global Strategy Simulation", variant="secondary")
115
  sim_status = gr.Textbox(label="Simulation Status", value="Idle", interactive=False)
116
  with gr.Column(scale=3):
117
+ sim_image = gr.Image(label="Strategy Performance", type="filepath")
118
 
119
  # --- EVENT LISTENERS ---
120