|
1 | | -"""Dagger module for fraud simulation.""" |
| 1 | +# dagger/module.py |
2 | 2 | import dagger |
3 | | -from dagger import dag, function, object_type, Doc |
4 | | -from typing import Annotated |
5 | | - |
6 | | -@object_type |
7 | | -class FraudSimulation: |
8 | | - """A Dagger module to run fraud simulation pipelines.""" |
9 | | - @function |
10 | | - async def run_fraud_sim( |
11 | | - self, |
12 | | - gemini_api_key: Annotated[dagger.Secret, Doc("Gemini API token")], |
13 | | - groq_api_key: Annotated[dagger.Secret, Doc("Groq API token")], |
14 | | - ) -> str: |
15 | | - """ |
16 | | - Runs the fraud simulation pipeline with the provided AI API keys. |
17 | | - """ |
18 | | - # Get the host directory (where dagger call is executed) |
19 | | - # In Dagger Cloud, this will be the workspace directory. |
20 | | - host_dir = dag.host().directory(".") |
21 | | - |
22 | | - # Define the base container with Python |
23 | | - base = ( |
24 | | - dag.container() |
25 | | - .from_("python:3.11-slim") |
26 | | - .with_directory("/app", host_dir) |
27 | | - .with_workdir("/app") |
28 | | - .with_exec(["pip", "install", "pydantic"]) |
29 | | - ) |
30 | | - |
31 | | - print("\n" + "="*50) |
32 | | - print("🚀 STARTING DAGGER FRAUD SIMULATION (CLOUD/MODULE)") |
33 | | - print("="*50 + "\n") |
34 | | - |
35 | | - # Step 1: Generate Accounts |
36 | | - print("👉 Step 1: Generating Accounts...") |
37 | | - step1 = base.with_exec(["python3", "fraud_simulation/generate_accounts.py"]) |
38 | | - |
39 | | - # Step 2: Generate Logs |
40 | | - print("👉 Step 2: Generating Logs...") |
41 | | - step2 = step1.with_exec(["python3", "fraud_simulation/generate_logs.py"]) |
42 | | - |
43 | | - # Step 3: Process Logs with Secrets |
44 | | - print("👉 Step 3: Running Fraud Analysis Agent (Comparing Gemini & Groq)...") |
45 | | - step3 = ( |
46 | | - step2 |
47 | | - .with_secret_variable("GEMINI_API_KEY", gemini_api_key) |
48 | | - .with_secret_variable("GROQ_API_KEY", groq_api_key) |
49 | | - .with_exec(["python3", "fraud_simulation/process_logs.py"]) |
50 | | - ) |
51 | | - |
52 | | - # Step 4: Expose Output |
53 | | - # This makes the results available via the Dagger API/UI |
54 | | - |
55 | | - # To make it "complete" the simulation and show status |
56 | | - await step3.stdout() |
57 | | - |
58 | | - return "✅ Simulation complete! Results available in fraud_simulation_reports/summary.json" |
| 3 | +from dagger import function |
| 4 | + |
| 5 | +@function |
| 6 | +async def run_fraud_sim( |
| 7 | + gemini_api_key: dagger.Secret, |
| 8 | + groq_api_key: dagger.Secret, |
| 9 | +) -> str: |
| 10 | + """ |
| 11 | + Runs the fraud simulation using the global Dagger context. |
| 12 | + """ |
| 13 | + |
| 14 | + # Use global `dagger` / `dag`, not self |
| 15 | + host_dir = dagger.host().directory(".") |
| 16 | + |
| 17 | + base = ( |
| 18 | + dagger.container() |
| 19 | + .from_("python:3.11-slim") |
| 20 | + .with_directory("/app", host_dir) |
| 21 | + .with_workdir("/app") |
| 22 | + .with_exec(["pip", "install", "pydantic"]) |
| 23 | + ) |
| 24 | + |
| 25 | + print("\n" + "=" * 50) |
| 26 | + print("🚀 STARTING DAGGER FRAUD SIMULATION") |
| 27 | + print("=" * 50 + "\n") |
| 28 | + |
| 29 | + step1 = base.with_exec(["python3", "fraud_simulation/generate_accounts.py"]) |
| 30 | + step2 = step1.with_exec(["python3", "fraud_simulation/generate_logs.py"]) |
| 31 | + step3 = ( |
| 32 | + step2 |
| 33 | + .with_secret_variable("GEMINI_API_KEY", gemini_api_key) |
| 34 | + .with_secret_variable("GROQ_API_KEY", groq_api_key) |
| 35 | + .with_exec(["python3", "fraud_simulation/process_logs.py"]) |
| 36 | + ) |
| 37 | + |
| 38 | + # Force streaming stdout to trigger execution |
| 39 | + await step3.stdout() |
| 40 | + |
| 41 | + return "✅ Simulation complete! Results available in fraud_simulation_reports/summary.json" |
0 commit comments