-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path4d_use_generated_attributes.py
More file actions
93 lines (74 loc) · 2.75 KB
/
4d_use_generated_attributes.py
File metadata and controls
93 lines (74 loc) · 2.75 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
"""
Use generated attributes example demonstrating classification and validation.
This example shows how to use the generated attributes from 4c for document
classification and attribute validation.
"""
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
from multi_class_text_classifier import TextClassifier
from multi_class_text_classifier.models.data_models import (
AttributeValidationConfig,
AttributeEvaluationConfig
)
print("🎯 Use Generated Attributes Example")
print("=" * 50)
print("Using generated attributes for classification and validation.\n")
# Check if generated attributes exist
if not os.path.exists("output/generated_document_classification_attributes.json"):
print("❌ Generated attributes file not found!")
print("Please run 4c_attribute_generation_example.py first to generate attributes.")
exit()
# Configure attribute validation with generated attributes
attribute_config = AttributeValidationConfig(
enabled=True,
attributes_path="output/generated_document_classification_attributes.json",
evaluation_config=AttributeEvaluationConfig(
model_id="us.amazon.nova-lite-v1:0",
temperature=0.1,
max_tokens=1000
)
)
# Initialize classifier with attribute validation
print("🔧 Initializing classifier with generated attributes...")
classifier = TextClassifier(
dataset_path="datasets/document_classification_dataset.json",
embeddings_path="datasets/document_classification_embeddings.pkl.gz",
attribute_config=attribute_config
)
print("✅ Classifier initialized!\n")
# Test with a complete invoice
test_text = """INVOICE #INV-2024-001
Bill To: ABC Corporation
123 Business St
New York, NY 10001
From: XYZ Services LLC
456 Service Ave
Boston, MA 02101
Amount Due: $2,500.00
Due Date: March 15, 2024
Please remit payment within 30 days of invoice date."""
print("📝 Classifying text:")
print(f" {test_text[:80]}...")
print()
# Classify with attribute validation
result = classifier.predict(test_text)
# Display results
print("🎯 Results:")
print(f" Predicted Class: {result.predicted_class.name}")
print(f" Similarity Score: {result.similarity_score:.3f}")
if result.attribute_validation:
attr_result = result.attribute_validation
print(f" Attribute Score: {attr_result.overall_score:.3f}")
print(f"\n✅ Attribute Details:")
if attr_result.conditions_met:
print(f" ✓ Conditions Met:")
for condition in attr_result.conditions_met:
print(f" • {condition}")
if attr_result.conditions_not_met:
print(f" ✗ Conditions Not Met:")
for condition in attr_result.conditions_not_met:
print(f" • {condition}")
else:
print(f" Attribute Score: N/A")
print()