-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribution_engine.py
More file actions
470 lines (380 loc) · 16.1 KB
/
Copy pathattribution_engine.py
File metadata and controls
470 lines (380 loc) · 16.1 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
"""
YSenseAI Attribution Infrastructure
Copyright 2025 Alton Lee Wei Bin
Licensed under Apache License 2.0
This file demonstrates the core attribution engine implementation
establishing prior art for AI content attribution systems.
"""
import hashlib
import json
import uuid
from datetime import datetime, timezone
from typing import Dict, List, Optional, Any, Tuple
from enum import Enum
from dataclasses import dataclass, asdict
class ContributionType(Enum):
"""Types of contributions in the system"""
HUMAN = "human"
AI = "ai"
HYBRID = "hybrid"
class ConsentTier(Enum):
"""Consent tiers based on Z Protocol"""
PUBLIC = "public"
PERSONAL = "personal"
CULTURAL = "cultural"
SACRED = "sacred"
THERAPEUTIC = "therapeutic"
@dataclass
class AttributionRecord:
"""Data structure for attribution records"""
contributor_id: str
contribution_type: ContributionType
content_hash: str
timestamp: str
verification_status: str
consent_level: ConsentTier
metadata: Dict[str, Any]
blockchain_hash: Optional[str] = None
parent_hash: Optional[str] = None
attribution_score: float = 0.0
class AttributionEngine:
"""
Core engine for tracking AI and human contributions.
Implements novel attribution scoring and blockchain verification.
"""
def __init__(self):
self.attribution_chain: List[AttributionRecord] = []
self.contributor_registry: Dict[str, Dict] = {}
self.verification_queue: List[str] = []
self.consensus_threshold = 0.95
def generate_content_hash(self, content: Any) -> str:
"""
Generate cryptographic hash of content
Args:
content: Content to hash (string, dict, or bytes)
Returns:
SHA-256 hash of content
"""
if isinstance(content, dict):
content = json.dumps(content, sort_keys=True)
elif not isinstance(content, bytes):
content = str(content).encode('utf-8')
return hashlib.sha256(content).hexdigest()
def calculate_attribution_weight(self,
contribution_type: ContributionType,
content: Any,
metadata: Dict) -> float:
"""
Calculate attribution weight using novel scoring algorithm
Args:
contribution_type: Type of contribution
content: The actual content
metadata: Additional metadata about contribution
Returns:
Attribution weight score between 0 and 1
"""
base_weights = {
ContributionType.HUMAN: 1.0,
ContributionType.AI: 0.8,
ContributionType.HYBRID: 0.9
}
weight = base_weights[contribution_type]
# Adjust based on content complexity
if metadata.get('complexity_score', 0) > 0.7:
weight *= 1.1
# Adjust based on originality
if metadata.get('originality_score', 0) > 0.8:
weight *= 1.2
# Adjust based on verification status
if metadata.get('verified', False):
weight *= 1.05
return min(weight, 1.0) # Cap at 1.0
def get_consent_level(self, contributor_id: str) -> ConsentTier:
"""
Retrieve consent level for a contributor
Args:
contributor_id: Unique identifier for contributor
Returns:
Consent tier for the contributor
"""
contributor = self.contributor_registry.get(contributor_id, {})
return contributor.get('consent_tier', ConsentTier.PERSONAL)
def extract_metadata(self, content: Any) -> Dict:
"""
Extract metadata from content using AI analysis
Args:
content: Content to analyze
Returns:
Dictionary of extracted metadata
"""
metadata = {
'content_length': len(str(content)),
'timestamp': datetime.now(timezone.utc).isoformat(),
'content_type': type(content).__name__,
'extraction_version': '1.0.0'
}
# Simulate complexity analysis
metadata['complexity_score'] = min(len(str(content)) / 1000, 1.0)
# Simulate originality analysis
metadata['originality_score'] = 0.85 # Would use ML model in production
return metadata
def write_to_blockchain(self, record: AttributionRecord) -> str:
"""
Write attribution record to blockchain
Args:
record: Attribution record to write
Returns:
Blockchain transaction hash
"""
# Get previous block hash for chain integrity
previous_hash = self.attribution_chain[-1].blockchain_hash if self.attribution_chain else "0"
# Create block data
block_data = {
'previous_hash': previous_hash,
'timestamp': record.timestamp,
'record': asdict(record),
'nonce': 0
}
# Simulate proof of work (simplified)
while True:
block_string = json.dumps(block_data, sort_keys=True)
block_hash = hashlib.sha256(block_string.encode()).hexdigest()
# Simple difficulty: hash must start with "00"
if block_hash.startswith("00"):
return block_hash
block_data['nonce'] += 1
def record_contribution(self,
contributor_id: str,
content: Any,
contribution_type: ContributionType,
metadata: Optional[Dict] = None) -> AttributionRecord:
"""
Record a contribution with full attribution metadata
Args:
contributor_id: Unique identifier for contributor
content: The actual contribution content
contribution_type: Type of contribution (human/ai/hybrid)
metadata: Optional additional metadata
Returns:
Complete attribution record with blockchain hash
"""
# Register contributor if new
if contributor_id not in self.contributor_registry:
self.register_contributor(contributor_id)
# Generate timestamp
timestamp = datetime.now(timezone.utc).isoformat()
# Extract and merge metadata
extracted_metadata = self.extract_metadata(content)
if metadata:
extracted_metadata.update(metadata)
# Calculate attribution score
attribution_score = self.calculate_attribution_weight(
contribution_type, content, extracted_metadata
)
# Create attribution record
record = AttributionRecord(
contributor_id=contributor_id,
contribution_type=contribution_type,
content_hash=self.generate_content_hash(content),
timestamp=timestamp,
verification_status='pending',
consent_level=self.get_consent_level(contributor_id),
metadata=extracted_metadata,
attribution_score=attribution_score
)
# Write to blockchain
blockchain_hash = self.write_to_blockchain(record)
record.blockchain_hash = blockchain_hash
# Add to chain
self.attribution_chain.append(record)
# Queue for verification
self.verification_queue.append(blockchain_hash)
return record
def register_contributor(self,
contributor_id: str,
contributor_type: ContributionType = ContributionType.HUMAN,
consent_tier: ConsentTier = ConsentTier.PERSONAL) -> Dict:
"""
Register a new contributor in the system
Args:
contributor_id: Unique identifier for contributor
contributor_type: Type of contributor
consent_tier: Initial consent tier
Returns:
Contributor registration record
"""
registration = {
'contributor_id': contributor_id,
'contributor_type': contributor_type,
'consent_tier': consent_tier,
'registration_date': datetime.now(timezone.utc).isoformat(),
'status': 'active',
'contribution_count': 0,
'total_attribution_score': 0.0
}
self.contributor_registry[contributor_id] = registration
return registration
def verify_chain_integrity(self) -> Tuple[bool, Optional[str]]:
"""
Verify the integrity of the attribution chain
Returns:
Tuple of (is_valid, error_message)
"""
if not self.attribution_chain:
return True, None
for i, record in enumerate(self.attribution_chain):
if i == 0:
continue
# Verify link to previous record
previous_record = self.attribution_chain[i - 1]
if record.parent_hash != previous_record.blockchain_hash:
return False, f"Chain broken at index {i}"
# Verify record hash
record_data = {
'contributor_id': record.contributor_id,
'content_hash': record.content_hash,
'timestamp': record.timestamp
}
calculated_hash = self.generate_content_hash(record_data)
if calculated_hash != record.content_hash:
return False, f"Invalid hash at index {i}"
return True, None
def get_attribution_summary(self, contributor_id: Optional[str] = None) -> Dict:
"""
Get attribution summary for a contributor or entire system
Args:
contributor_id: Optional contributor ID to filter by
Returns:
Attribution summary statistics
"""
if contributor_id:
records = [r for r in self.attribution_chain
if r.contributor_id == contributor_id]
else:
records = self.attribution_chain
if not records:
return {'total_contributions': 0}
summary = {
'total_contributions': len(records),
'human_contributions': len([r for r in records
if r.contribution_type == ContributionType.HUMAN]),
'ai_contributions': len([r for r in records
if r.contribution_type == ContributionType.AI]),
'hybrid_contributions': len([r for r in records
if r.contribution_type == ContributionType.HYBRID]),
'average_attribution_score': sum(r.attribution_score for r in records) / len(records),
'verified_contributions': len([r for r in records
if r.verification_status == 'verified']),
'consent_distribution': {}
}
# Calculate consent distribution
for tier in ConsentTier:
count = len([r for r in records if r.consent_level == tier])
if count > 0:
summary['consent_distribution'][tier.value] = count
return summary
def export_attribution_proof(self,
contribution_hash: str,
format: str = 'json') -> str:
"""
Export proof of attribution for legal/verification purposes
Args:
contribution_hash: Hash of the contribution
format: Export format (json, xml, etc.)
Returns:
Formatted attribution proof
"""
# Find the record
record = None
for r in self.attribution_chain:
if r.content_hash == contribution_hash or r.blockchain_hash == contribution_hash:
record = r
break
if not record:
raise ValueError(f"No record found for hash: {contribution_hash}")
proof = {
'attribution_proof': {
'version': '1.0',
'generated_at': datetime.now(timezone.utc).isoformat(),
'record': asdict(record),
'chain_position': self.attribution_chain.index(record),
'chain_length': len(self.attribution_chain),
'verification': {
'chain_integrity': self.verify_chain_integrity()[0],
'blockchain_hash': record.blockchain_hash,
'content_hash': record.content_hash
}
}
}
if format == 'json':
return json.dumps(proof, indent=2)
else:
raise NotImplementedError(f"Format {format} not yet supported")
# Example usage and testing
if __name__ == "__main__":
# Initialize attribution engine
engine = AttributionEngine()
# Register contributors
engine.register_contributor(
"human_001",
ContributionType.HUMAN,
ConsentTier.PUBLIC
)
engine.register_contributor(
"ai_claude",
ContributionType.AI,
ConsentTier.PUBLIC
)
# Record human contribution
human_contribution = {
'text': 'Walking on the beach at sunset in Kelantan...',
'image': 'beach_sunset.jpg',
'metadata': {
'location': 'Kelantan, Malaysia',
'date': '2013-07-15'
}
}
human_record = engine.record_contribution(
contributor_id="human_001",
content=human_contribution,
contribution_type=ContributionType.HUMAN,
metadata={'source': 'perception_toolkit', 'layer': 'narrative'}
)
print(f"Human contribution recorded: {human_record.blockchain_hash}")
# Record AI contribution
ai_contribution = {
'analysis': 'This memory contains deep emotional resonance...',
'extracted_themes': ['nostalgia', 'connection', 'nature'],
'vibe_distillation': ['golden', 'infinite', 'peaceful']
}
ai_record = engine.record_contribution(
contributor_id="ai_claude",
content=ai_contribution,
contribution_type=ContributionType.AI,
metadata={'model': 'claude-3', 'confidence': 0.92}
)
print(f"AI contribution recorded: {ai_record.blockchain_hash}")
# Record hybrid contribution
hybrid_contribution = {
'human_memory': human_contribution,
'ai_enhancement': ai_contribution,
'synthesis': 'A moment of transcendent beauty captured and understood'
}
hybrid_record = engine.record_contribution(
contributor_id="human_001",
content=hybrid_contribution,
contribution_type=ContributionType.HYBRID,
metadata={'collaboration_type': 'human_ai_synthesis'}
)
print(f"Hybrid contribution recorded: {hybrid_record.blockchain_hash}")
# Verify chain integrity
is_valid, error = engine.verify_chain_integrity()
print(f"\nChain integrity: {'Valid' if is_valid else f'Invalid - {error}'}")
# Get attribution summary
summary = engine.get_attribution_summary()
print(f"\nAttribution Summary:")
print(json.dumps(summary, indent=2))
# Export attribution proof
proof = engine.export_attribution_proof(human_record.blockchain_hash)
print(f"\nAttribution Proof for human contribution:")
print(proof)