File size: 7,136 Bytes
2c41dce |
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 |
"""
Main Entry Point
Demonstrates orchestrator usage with example workflows.
No business logic - just a thin interface layer.
"""
from dotenv import load_dotenv
load_dotenv()
from core.orchestrator import Orchestrator
print(">>> MAIN FILE LOADED <<<")
def example_text_proof():
"""Example: Create proof from text content."""
print("\n=== Example 1: Text Proof Creation ===")
orchestrator = Orchestrator()
# Create proof from text
result = orchestrator.create_proof({
"type": "text",
"content": "This is a confidential document that needs timestamping."
})
if result["success"]:
print(f"β Proof created: {result['proof_id']}")
print(f" Hash: {result['proof'].content_hash}")
print(f" Timestamp: {result['proof'].timestamp}")
# AI explanation (if available)
if "assistant" in result:
print(f"\n π€ AI Explanation:")
print(f" {result['assistant']['response']}")
else:
print(f"β Failed: {result['message']}")
return result
def example_file_proof():
"""Example: Create proof from file content."""
print("\n=== Example 2: File Proof Creation ===")
orchestrator = Orchestrator()
# Simulate file content
file_content = b"Binary file content here"
result = orchestrator.create_proof({
"type": "file",
"content": file_content,
"filename": "document.pdf"
})
if result["success"]:
print(f"β Proof created: {result['proof_id']}")
print(f" Hash: {result['proof'].content_hash}")
print(f" File: {result['proof'].metadata['filename']}")
print(f" OCR Status: {result['proof'].ocr_status}")
if result['proof'].extracted_text:
print(f" Extracted Text: {result['proof'].extracted_text[:100]}...")
else:
print(f"β Failed: {result['message']}")
return result
def example_image_ocr():
"""Example: Create proof from image with OCR."""
print("\n=== Example 5: Image Proof with OCR ===")
orchestrator = Orchestrator()
# Note: In real usage, this would be actual image bytes
# For demo, we'll just show the structure
print("Note: This example requires actual image bytes with text.")
print("Skipping OCR demo - install Tesseract and provide real image to test.")
# Example structure:
# with open("invoice.png", "rb") as f:
# image_bytes = f.read()
#
# result = orchestrator.create_proof({
# "type": "file",
# "content": image_bytes,
# "filename": "invoice.png"
# })
#
# if result["success"]:
# print(f"β Proof created with OCR")
# print(f" Extracted: {result['proof'].extracted_text}")
print("β OCR integration ready for image inputs")
def example_ai_assistant():
"""Example: Ask AI assistant about proofs."""
print("\n=== Example 6: AI Assistant Q&A ===")
orchestrator = Orchestrator()
if not orchestrator.ai_sidecar.enabled:
print("β οΈ AI assistant is disabled")
print(" Enable with: AI_ENABLED=true GEMINI_API_KEY=your-key")
return
# Create a proof first
create_result = orchestrator.create_proof({
"type": "text",
"content": "Important contract signed on December 24, 2024"
})
if not create_result["success"]:
print("Failed to create proof for demo")
return
proof_id = create_result["proof_id"]
# Ask AI about the proof
questions = [
"What does this proof guarantee?",
"How can I verify this proof later?",
"What should I do with this proof ID?"
]
for question in questions:
print(f"\n Q: {question}")
result = orchestrator.ask_assistant(question, proof_id)
if result["success"]:
print(f" π€ A: {result['assistant']['response']}")
else:
print(f" β {result['message']}")
def example_verification():
"""Example: Verify an existing proof."""
print("\n=== Example 3: Proof Verification ===")
orchestrator = Orchestrator()
# First create a proof
original_content = "Verify this content"
create_result = orchestrator.create_proof({
"type": "text",
"content": original_content
})
if not create_result["success"]:
print("Failed to create proof for verification")
return
proof_id = create_result["proof_id"]
print(f"Created proof: {proof_id}")
# Verify with correct content
verify_result = orchestrator.verify_proof(
proof_id,
original_content.encode('utf-8')
)
if verify_result["success"]:
vr = verify_result["verification_result"]
status = "β VALID" if vr.is_valid else "β INVALID"
print(f"{status}: {vr.message}")
# AI explanation (if available)
if "assistant" in verify_result:
print(f"\n π€ AI Explanation:")
print(f" {verify_result['assistant']['response']}")
else:
print(f"β Verification failed: {verify_result['message']}")
# Verify with tampered content
print("\nAttempting verification with tampered content:")
tampered_result = orchestrator.verify_proof(
proof_id,
b"Tampered content"
)
if tampered_result["success"]:
vr = tampered_result["verification_result"]
status = "β VALID" if vr.is_valid else "β INVALID"
print(f"{status}: {vr.message}")
def example_retrieval():
"""Example: Retrieve a stored proof."""
print("\n=== Example 4: Proof Retrieval ===")
orchestrator = Orchestrator()
# Create a proof first
create_result = orchestrator.create_proof({
"type": "text",
"content": "Retrieve this later"
})
if not create_result["success"]:
print("Failed to create proof")
return
proof_id = create_result["proof_id"]
# Retrieve it
get_result = orchestrator.get_proof(proof_id)
if get_result["success"]:
proof = get_result["proof"]
print(f"β Retrieved proof: {proof.proof_id}")
print(f" Hash: {proof.content_hash}")
print(f" Size: {proof.content_size} bytes")
print(f" Created: {proof.timestamp}")
else:
print(f"β Failed: {get_result['message']}")
def main():
"""Run all examples."""
print("=" * 60)
print("PROOF-OF-EXISTENCE SYSTEM - Priority-3 MVP")
print("Deterministic Core + OCR + AI Sidecar")
print("=" * 60)
try:
example_text_proof()
example_file_proof()
example_verification()
example_retrieval()
example_image_ocr()
example_ai_assistant()
print("\n" + "=" * 60)
print("All examples completed successfully!")
print("=" * 60)
except Exception as e:
print(f"\nβ Error running examples: {str(e)}")
if __name__ == "__main__":
main() |