gaurv007 commited on
Commit
8267e98
Β·
verified Β·
1 Parent(s): b5839f0

Upload alpha_factory/run.py

Browse files
Files changed (1) hide show
  1. alpha_factory/run.py +49 -35
alpha_factory/run.py CHANGED
@@ -1,5 +1,6 @@
1
  """
2
- Alpha Factory β€” Entry Point v2
 
3
  Run: python -m alpha_factory.run [--dry-run] [--batch-size N] [--interactive]
4
  [--proven] [--enable-brain]
5
  """
@@ -40,6 +41,39 @@ async def setup_models(interactive: bool = False, hf_token: str = None) -> Model
40
  return manager
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def main():
44
  parser = argparse.ArgumentParser(description="Alpha Factory β€” LLM-Driven Alpha Generation Pipeline")
45
  parser.add_argument("--dry-run", action="store_true", help="Run without BRAIN submissions")
@@ -77,45 +111,25 @@ def main():
77
  Ollama: {args.ollama_url}
78
  HF Token: {"βœ“ Set" if hf_token else "βœ— Not set (cloud models unavailable)"}
79
 
80
- [yellow]NOTE: This is v0.2.0 with real personas wired, but BRAIN integration
81
- requires a valid BRAIN_SESSION_TOKEN. See .env.example for setup.[/]
82
  """)
83
 
84
- # Discover and select models (only needed for LLM mode)
85
- if not args.proven:
86
- manager = asyncio.run(setup_models(
87
- interactive=args.interactive,
88
- hf_token=hf_token,
89
- ))
90
- else:
91
- manager = None
92
- console.print(" [green]Proven template mode β€” no LLM model discovery needed[/]")
93
-
94
- # Update LLM config with Ollama URL
95
- config.llm.base_url = f"{args.ollama_url}/v1"
96
-
97
- # Create pipeline
98
- pipeline = AlphaPipeline(config)
99
- if manager:
100
- pipeline.llm = LLMClient(config.llm, model_manager=manager)
101
-
102
- # Initialize BRAIN client if enabled
103
- if config.enable_brain_client:
104
- try:
105
- import aiohttp
106
- session = aiohttp.ClientSession()
107
- asyncio.run(pipeline.init_brain_client(session))
108
- except ImportError:
109
- console.print("[red]aiohttp required for BRAIN client. pip install aiohttp[/]")
110
- config.enable_brain_client = False
111
-
112
  try:
113
- result = asyncio.run(pipeline.run_batch(args.batch_size))
114
- console.print(f"\n[bold]Final: {result}[/]")
 
 
 
 
 
 
 
 
 
 
115
  except KeyboardInterrupt:
116
  console.print("\n[yellow]Interrupted by user[/]")
117
- finally:
118
- pipeline.close()
119
 
120
 
121
  if __name__ == "__main__":
 
1
  """
2
+ Alpha Factory β€” Entry Point v3
3
+ Single asyncio event loop, no session leaks.
4
  Run: python -m alpha_factory.run [--dry-run] [--batch-size N] [--interactive]
5
  [--proven] [--enable-brain]
6
  """
 
41
  return manager
42
 
43
 
44
+ async def run_pipeline(config, manager: ModelManager | None = None):
45
+ """Run the full pipeline under a single event loop."""
46
+ # Update LLM config with Ollama URL
47
+ config.llm.base_url = f"{args.ollama_url}/v1"
48
+
49
+ # Create pipeline
50
+ pipeline = AlphaPipeline(config)
51
+ if manager:
52
+ pipeline.llm = LLMClient(config.llm, model_manager=manager)
53
+
54
+ # Initialize BRAIN client if enabled
55
+ if config.enable_brain_client:
56
+ try:
57
+ import aiohttp
58
+ async with aiohttp.ClientSession() as session:
59
+ await pipeline.init_brain_client(session)
60
+ result = await pipeline.run_batch(config.batch_size)
61
+ except ImportError:
62
+ console.print("[red]aiohttp required for BRAIN client. pip install aiohttp[/]")
63
+ config.enable_brain_client = False
64
+ result = await pipeline.run_batch(config.batch_size)
65
+ except Exception as e:
66
+ console.print(f"[red]BRAIN client error: {e}[/]")
67
+ config.enable_brain_client = False
68
+ result = await pipeline.run_batch(config.batch_size)
69
+ else:
70
+ result = await pipeline.run_batch(config.batch_size)
71
+
72
+ console.print(f"\n[bold]Final: {result}[/]")
73
+ pipeline.close()
74
+ return result
75
+
76
+
77
  def main():
78
  parser = argparse.ArgumentParser(description="Alpha Factory β€” LLM-Driven Alpha Generation Pipeline")
79
  parser.add_argument("--dry-run", action="store_true", help="Run without BRAIN submissions")
 
111
  Ollama: {args.ollama_url}
112
  HF Token: {"βœ“ Set" if hf_token else "βœ— Not set (cloud models unavailable)"}
113
 
114
+ [yellow]NOTE: BRAIN integration requires a valid BRAIN_SESSION_TOKEN.[/]
115
+ [yellow] See .env.example for setup.[/]
116
  """)
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  try:
119
+ if args.proven:
120
+ console.print(" [green]Proven template mode β€” no LLM model discovery needed[/]")
121
+ asyncio.run(run_pipeline(config, manager=None))
122
+ else:
123
+ # Discover and select models, then run pipeline
124
+ async def _discover_and_run():
125
+ manager = await setup_models(
126
+ interactive=args.interactive,
127
+ hf_token=hf_token,
128
+ )
129
+ return await run_pipeline(config, manager=manager)
130
+ asyncio.run(_discover_and_run())
131
  except KeyboardInterrupt:
132
  console.print("\n[yellow]Interrupted by user[/]")
 
 
133
 
134
 
135
  if __name__ == "__main__":