Spaces:
Sleeping
Sleeping
Beepeen78 commited on
Commit ·
a2ece46
1
Parent(s): 510e777
Fix: Pin scikit-learn version and add powerbi_export
Browse files- powerbi_export.py +59 -0
- requirements.txt +1 -1
powerbi_export.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Power BI export module for fraud detection pipeline.
|
| 3 |
+
Exports transaction data and metrics in Power BI-friendly format.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import sys
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Add powerbi directory to path to import prepare_powerbi_data
|
| 12 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
from powerbi.prepare_powerbi_data import export_for_powerbi, POWERBI_OUT
|
| 16 |
+
except ImportError:
|
| 17 |
+
# Fallback if powerbi module structure is different
|
| 18 |
+
POWERBI_OUT = Path("powerbi/out")
|
| 19 |
+
POWERBI_OUT.mkdir(parents=True, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def export_powerbi_csvs(result_df: pd.DataFrame, output_dir: Path = None):
|
| 23 |
+
"""
|
| 24 |
+
Export fraud detection results to Power BI format.
|
| 25 |
+
|
| 26 |
+
This function is called by the Gradio app to automatically export
|
| 27 |
+
scored transactions and metrics for Power BI consumption.
|
| 28 |
+
|
| 29 |
+
Args:
|
| 30 |
+
result_df: DataFrame with fraud detection results (must include
|
| 31 |
+
'fraud_probability', 'fraud_prediction', 'risk_level')
|
| 32 |
+
output_dir: Optional output directory (defaults to powerbi/out)
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
tuple: (transactions_path, metrics_path)
|
| 36 |
+
"""
|
| 37 |
+
if output_dir is None:
|
| 38 |
+
output_dir = POWERBI_OUT
|
| 39 |
+
|
| 40 |
+
# Ensure output directory exists
|
| 41 |
+
output_dir = Path(output_dir)
|
| 42 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
# Use the existing export function from powerbi module
|
| 46 |
+
tx_path, metrics_path = export_for_powerbi(result_df, output_dir)
|
| 47 |
+
return tx_path, metrics_path
|
| 48 |
+
except Exception as e:
|
| 49 |
+
# If the powerbi module isn't available, do a simple export
|
| 50 |
+
print(f"⚠️ Warning: Could not use full Power BI export: {e}")
|
| 51 |
+
print(" Falling back to simple CSV export...")
|
| 52 |
+
|
| 53 |
+
# Simple fallback export
|
| 54 |
+
tx_path = output_dir / "transactions_scored.csv"
|
| 55 |
+
result_df.to_csv(tx_path, index=False)
|
| 56 |
+
print(f"✅ Exported {len(result_df)} transactions to {tx_path}")
|
| 57 |
+
|
| 58 |
+
return tx_path, None
|
| 59 |
+
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# Core ML and Data Processing
|
| 2 |
pandas>=2.0.0,<3.0.0
|
| 3 |
numpy>=1.26.0,<2.0.0
|
| 4 |
-
scikit-learn
|
| 5 |
joblib>=1.3.0,<2.0.0
|
| 6 |
|
| 7 |
# Gradio and Web Interface
|
|
|
|
| 1 |
# Core ML and Data Processing
|
| 2 |
pandas>=2.0.0,<3.0.0
|
| 3 |
numpy>=1.26.0,<2.0.0
|
| 4 |
+
scikit-learn==1.7.1
|
| 5 |
joblib>=1.3.0,<2.0.0
|
| 6 |
|
| 7 |
# Gradio and Web Interface
|