Spaces:
Sleeping
Sleeping
soupstick
commited on
Commit
·
f1027ec
1
Parent(s):
6589a25
fix missing files and business logic
Browse files- Dockerfile +19 -0
- app.py +71 -16
- impact.yaml +39 -0
- state.json +8 -1
Dockerfile
CHANGED
@@ -1 +1,20 @@
|
|
1 |
FROM python:3.10-slim
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
FROM python:3.10-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY requirements.txt .
|
6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
7 |
+
|
8 |
+
COPY . .
|
9 |
+
|
10 |
+
# Create necessary directories
|
11 |
+
RUN mkdir -p state business
|
12 |
+
|
13 |
+
# Copy or create necessary files
|
14 |
+
RUN if [ ! -f "state/health.json" ] && [ -f "state.json" ]; then \
|
15 |
+
cp state.json state/health.json; \
|
16 |
+
fi
|
17 |
+
|
18 |
+
EXPOSE 7860
|
19 |
+
|
20 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
@@ -1,25 +1,80 @@
|
|
1 |
import gradio as gr, json, yaml
|
2 |
from pathlib import Path
|
|
|
3 |
def load_health():
|
4 |
-
try:
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def load_verticals():
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def score(vertical, freshness):
|
10 |
-
h=load_health()
|
11 |
-
if h.get('overall')!='pass':
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return f'✅ Allowed. score≈{base:.2f}, health={h}'
|
|
|
15 |
def business(vertical):
|
16 |
-
_,cfg=load_verticals()
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
with gr.Blocks() as demo:
|
20 |
gr.Markdown('# Amazon-Style Risk Pipeline — Multi-Vertical Demo')
|
21 |
-
v=gr.Dropdown(choices=verts, value=verts[0])
|
22 |
-
f=gr.Number(value=12, label='freshness_sec')
|
23 |
-
out=gr.Textbox(
|
24 |
-
gr.
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr, json, yaml
|
2 |
from pathlib import Path
|
3 |
+
|
4 |
def load_health():
|
5 |
+
try:
|
6 |
+
# Try the expected path first
|
7 |
+
if Path('state/health.json').exists():
|
8 |
+
return json.loads(Path('state/health.json').read_text())
|
9 |
+
# Fall back to the existing state.json file
|
10 |
+
elif Path('state.json').exists():
|
11 |
+
return json.loads(Path('state.json').read_text())
|
12 |
+
else:
|
13 |
+
return {'overall':'unknown'}
|
14 |
+
except:
|
15 |
+
return {'overall':'unknown'}
|
16 |
+
|
17 |
def load_verticals():
|
18 |
+
try:
|
19 |
+
cfg = yaml.safe_load(Path('business/impact.yaml').read_text())
|
20 |
+
return list(cfg['verticals'].keys()), cfg
|
21 |
+
except:
|
22 |
+
# Fallback configuration if file doesn't exist
|
23 |
+
default_cfg = {
|
24 |
+
'verticals': {
|
25 |
+
'fraud_detection': {
|
26 |
+
'kpis': ['precision', 'recall', 'f1_score'],
|
27 |
+
'scenarios': ['payment_fraud', 'account_takeover', 'identity_theft']
|
28 |
+
},
|
29 |
+
'content_moderation': {
|
30 |
+
'kpis': ['accuracy', 'response_time', 'false_positive_rate'],
|
31 |
+
'scenarios': ['spam_detection', 'hate_speech', 'inappropriate_content']
|
32 |
+
},
|
33 |
+
'supply_chain_risk': {
|
34 |
+
'kpis': ['risk_score', 'coverage', 'detection_rate'],
|
35 |
+
'scenarios': ['vendor_risk', 'logistics_disruption', 'quality_issues']
|
36 |
+
}
|
37 |
+
}
|
38 |
+
}
|
39 |
+
return list(default_cfg['verticals'].keys()), default_cfg
|
40 |
+
|
41 |
def score(vertical, freshness):
|
42 |
+
h = load_health()
|
43 |
+
if h.get('overall') != 'pass':
|
44 |
+
return f'❌ Policy block. Health={h}'
|
45 |
+
|
46 |
+
base = {
|
47 |
+
'fraud_detection': 0.87,
|
48 |
+
'content_moderation': 0.12,
|
49 |
+
'supply_chain_risk': 0.42
|
50 |
+
}.get(vertical, 0.5)
|
51 |
+
|
52 |
+
if float(freshness) > 60:
|
53 |
+
return '❌ Freshness>60 → block'
|
54 |
+
|
55 |
return f'✅ Allowed. score≈{base:.2f}, health={h}'
|
56 |
+
|
57 |
def business(vertical):
|
58 |
+
_, cfg = load_verticals()
|
59 |
+
v = cfg['verticals'][vertical]
|
60 |
+
return json.dumps({'kpis': v['kpis'], 'scenarios': v['scenarios']}, indent=2)
|
61 |
+
|
62 |
+
# Initialize
|
63 |
+
verts, _ = load_verticals()
|
64 |
+
|
65 |
with gr.Blocks() as demo:
|
66 |
gr.Markdown('# Amazon-Style Risk Pipeline — Multi-Vertical Demo')
|
67 |
+
v = gr.Dropdown(choices=verts, value=verts[0], label="Vertical")
|
68 |
+
f = gr.Number(value=12, label='freshness_sec')
|
69 |
+
out = gr.Textbox(label="Score Result")
|
70 |
+
biz = gr.Code(language='json', label="Business Config")
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
score_btn = gr.Button('Score')
|
74 |
+
business_btn = gr.Button('Load Business')
|
75 |
+
|
76 |
+
score_btn.click(score, [v, f], out)
|
77 |
+
business_btn.click(business, v, biz)
|
78 |
+
|
79 |
+
if __name__ == "__main__":
|
80 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
impact.yaml
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
verticals:
|
2 |
+
fraud_detection:
|
3 |
+
kpis:
|
4 |
+
- precision
|
5 |
+
- recall
|
6 |
+
- f1_score
|
7 |
+
- false_positive_rate
|
8 |
+
scenarios:
|
9 |
+
- payment_fraud
|
10 |
+
- account_takeover
|
11 |
+
- identity_theft
|
12 |
+
- synthetic_identity
|
13 |
+
- card_not_present
|
14 |
+
|
15 |
+
content_moderation:
|
16 |
+
kpis:
|
17 |
+
- accuracy
|
18 |
+
- response_time
|
19 |
+
- false_positive_rate
|
20 |
+
- coverage
|
21 |
+
scenarios:
|
22 |
+
- spam_detection
|
23 |
+
- hate_speech
|
24 |
+
- inappropriate_content
|
25 |
+
- misinformation
|
26 |
+
- adult_content
|
27 |
+
|
28 |
+
supply_chain_risk:
|
29 |
+
kpis:
|
30 |
+
- risk_score
|
31 |
+
- coverage
|
32 |
+
- detection_rate
|
33 |
+
- time_to_detect
|
34 |
+
scenarios:
|
35 |
+
- vendor_risk
|
36 |
+
- logistics_disruption
|
37 |
+
- quality_issues
|
38 |
+
- regulatory_compliance
|
39 |
+
- geopolitical_risk
|
state.json
CHANGED
@@ -3,5 +3,12 @@
|
|
3 |
"quality_status": "pass",
|
4 |
"drift_status": "pass",
|
5 |
"privacy_status": "pass",
|
6 |
-
"policy_status": "pass"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
}
|
|
|
3 |
"quality_status": "pass",
|
4 |
"drift_status": "pass",
|
5 |
"privacy_status": "pass",
|
6 |
+
"policy_status": "pass",
|
7 |
+
"last_updated": "2025-01-20T10:30:00Z",
|
8 |
+
"metrics": {
|
9 |
+
"data_quality_score": 0.95,
|
10 |
+
"drift_detection_score": 0.88,
|
11 |
+
"privacy_compliance_score": 1.0,
|
12 |
+
"policy_compliance_score": 0.97
|
13 |
+
}
|
14 |
}
|