-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge_inference.py
More file actions
125 lines (100 loc) · 3.55 KB
/
edge_inference.py
File metadata and controls
125 lines (100 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
"""
FairProp Inspector - Edge Inference Example
===========================================
Demonstrates edge-native inference with error handling,
batch processing, and performance monitoring.
"""
import time
import sys
import os
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from src.inference.predict import predict
def check_compliance(text: str, model_path: str = "artifacts/model") -> dict:
"""
Check a single text for FHA compliance.
Args:
text: Property description to check
model_path: Path to trained model
Returns:
Dictionary with results and metadata
"""
start_time = time.time()
try:
label, confidence = predict(text, model_path)
latency_ms = (time.time() - start_time) * 1000
return {
"text": text,
"label": label,
"confidence": confidence,
"latency_ms": latency_ms,
"status": "success"
}
except Exception as e:
return {
"text": text,
"status": "error",
"error": str(e)
}
def batch_check(texts: list[str], model_path: str = "artifacts/model") -> list[dict]:
"""
Check multiple texts for compliance.
Args:
texts: List of property descriptions
model_path: Path to trained model
Returns:
List of results
"""
results = []
for text in texts:
result = check_compliance(text, model_path)
results.append(result)
return results
def main():
print("=" * 60)
print("FairProp Inspector - Edge Inference Demo")
print("=" * 60)
print()
# Test cases covering different violation categories
test_cases = [
"No kids under 12 allowed", # Familial status
"Christian community preferred", # Religion
"Perfect for active adults", # Age (subtle)
"Great school district nearby", # Compliant
"Wheelchair accessible entrance", # Compliant (accessibility)
"No section 8", # Economic discrimination
"Ideal for young professionals", # Age (subtle)
"Walking distance to shops and restaurants", # Compliant
]
print(f"Processing {len(test_cases)} property descriptions...\n")
# Batch processing
results = batch_check(test_cases)
# Display results
violations = 0
total_latency = 0
for i, result in enumerate(results, 1):
if result["status"] == "success":
icon = "✗" if result["label"] == "NON_COMPLIANT" else "✓"
print(f"{i}. {icon} {result['text'][:50]}...")
print(f" Label: {result['label']}")
print(f" Confidence: {result['confidence']:.1%}")
print(f" Latency: {result['latency_ms']:.1f}ms")
print()
if result["label"] == "NON_COMPLIANT":
violations += 1
total_latency += result["latency_ms"]
else:
print(f"{i}. ⚠ Error: {result['error']}")
print()
# Summary
print("=" * 60)
print("Summary")
print("=" * 60)
print(f"Total processed: {len(results)}")
print(f"Violations detected: {violations}")
print(f"Average latency: {total_latency / len(results):.1f}ms")
print(f"Privacy: ✅ All processing done locally (no data egress)")
print()
if __name__ == "__main__":
main()