bluestpanda commited on
Commit
23ca2e7
·
1 Parent(s): 4351d85
src/__pycache__/streamlit_app.cpython-311.pyc ADDED
Binary file (22.4 kB). View file
 
src/__pycache__/structure_analysis.cpython-311.pyc ADDED
Binary file (5.76 kB). View file
 
src/streamlit_app.py CHANGED
@@ -24,7 +24,9 @@ try:
24
  classify_data_structure,
25
  get_hierarchy_summary
26
  )
27
- except ImportError:
 
 
28
  st.error("⚠️ structure_analysis.py not found. Make sure all files are uploaded.")
29
  st.stop()
30
 
@@ -38,25 +40,45 @@ def analyze_with_llm(data: Dict[str, Any], target_field: str = "rotation_enabled
38
  Analyze data and generate a prompt for LLM analysis.
39
  Returns structured analysis without requiring Ollama.
40
  """
 
 
 
 
41
  # Detect summary fields
 
42
  summary_fields = detect_summary_fields(data)
 
 
 
43
  classification = classify_data_structure(data)
 
 
 
44
  hierarchy_summary = get_hierarchy_summary(data)
 
45
 
46
  # Extract samples
 
47
  sample_object = {}
48
  if 'results' in data:
49
- for section in data['results'].values():
 
 
50
  if isinstance(section, list) and len(section) > 0:
51
  sample_object = section[0]
 
52
  break
53
  elif isinstance(section, dict):
54
  for key, value in section.items():
55
  if isinstance(value, list) and len(value) > 0:
56
  sample_object = value[0] if isinstance(value[0], dict) else {}
 
57
  break
 
 
58
 
59
  summary_sample = data.get('results', {}).get('summary', {}) or data.get('summary', {})
 
60
 
61
  # Count objects with target field
62
  def count_objects_with_field(obj, field_name):
@@ -71,9 +93,12 @@ def analyze_with_llm(data: Dict[str, Any], target_field: str = "rotation_enabled
71
  count += count_objects_with_field(item, field_name)
72
  return count
73
 
 
74
  total_objects = count_objects_with_field(data, target_field)
 
75
 
76
  # Generate analysis
 
77
  analysis = {
78
  "summary_fields_detected": summary_fields[:10],
79
  "classification": classification,
@@ -83,15 +108,23 @@ def analyze_with_llm(data: Dict[str, Any], target_field: str = "rotation_enabled
83
  "summary_sample": summary_sample,
84
  "recommended_fields": []
85
  }
 
86
 
87
  # Recommend fields based on priority
 
88
  if summary_fields:
89
  analysis["recommended_fields"].extend(summary_fields[:3])
 
90
  if classification.get('config_fields'):
91
  analysis["recommended_fields"].extend(classification['config_fields'][:2])
 
92
  if sample_object:
93
- analysis["recommended_fields"].extend([k for k in sample_object.keys() if target_field in k.lower()])
 
 
94
 
 
 
95
  return analysis
96
 
97
 
@@ -146,12 +179,20 @@ def main():
146
  )
147
 
148
  if uploaded_file is not None:
 
 
 
149
  # Read and parse JSON
150
  try:
 
151
  content = uploaded_file.read()
 
 
 
152
  data = json.loads(content)
153
 
154
  st.success("✅ File loaded successfully!")
 
155
 
156
  # Sidebar for settings
157
  with st.sidebar:
@@ -167,9 +208,17 @@ def main():
167
  # Analyze button
168
  if st.button("🔍 Analyze", type="primary"):
169
  with st.spinner("Analyzing data structure..."):
170
- analysis_result = analyze_with_llm(data, target_field)
171
- st.session_state.analysis_result = analysis_result
172
- st.session_state.data = data
 
 
 
 
 
 
 
 
173
 
174
  # Display results if available
175
  if st.session_state.analysis_result:
 
24
  classify_data_structure,
25
  get_hierarchy_summary
26
  )
27
+ st.success("✅ Successfully imported structure_analysis module")
28
+ except ImportError as e:
29
+ st.error(f"⚠️ Import error: {e}")
30
  st.error("⚠️ structure_analysis.py not found. Make sure all files are uploaded.")
31
  st.stop()
32
 
 
40
  Analyze data and generate a prompt for LLM analysis.
41
  Returns structured analysis without requiring Ollama.
42
  """
43
+ print(f"DEBUG: Starting analysis with target_field: {target_field}")
44
+ print(f"DEBUG: Data type: {type(data)}")
45
+ print(f"DEBUG: Data keys: {list(data.keys()) if isinstance(data, dict) else 'Not a dict'}")
46
+
47
  # Detect summary fields
48
+ print("DEBUG: Detecting summary fields...")
49
  summary_fields = detect_summary_fields(data)
50
+ print(f"DEBUG: Found summary fields: {summary_fields}")
51
+
52
+ print("DEBUG: Classifying data structure...")
53
  classification = classify_data_structure(data)
54
+ print(f"DEBUG: Classification result: {classification}")
55
+
56
+ print("DEBUG: Getting hierarchy summary...")
57
  hierarchy_summary = get_hierarchy_summary(data)
58
+ print(f"DEBUG: Hierarchy summary: {hierarchy_summary}")
59
 
60
  # Extract samples
61
+ print("DEBUG: Extracting samples...")
62
  sample_object = {}
63
  if 'results' in data:
64
+ print("DEBUG: Found 'results' key in data")
65
+ for section_name, section in data['results'].items():
66
+ print(f"DEBUG: Processing section '{section_name}': {type(section)}")
67
  if isinstance(section, list) and len(section) > 0:
68
  sample_object = section[0]
69
+ print(f"DEBUG: Found sample object from list: {sample_object}")
70
  break
71
  elif isinstance(section, dict):
72
  for key, value in section.items():
73
  if isinstance(value, list) and len(value) > 0:
74
  sample_object = value[0] if isinstance(value[0], dict) else {}
75
+ print(f"DEBUG: Found sample object from dict list: {sample_object}")
76
  break
77
+ else:
78
+ print("DEBUG: No 'results' key found in data")
79
 
80
  summary_sample = data.get('results', {}).get('summary', {}) or data.get('summary', {})
81
+ print(f"DEBUG: Summary sample: {summary_sample}")
82
 
83
  # Count objects with target field
84
  def count_objects_with_field(obj, field_name):
 
93
  count += count_objects_with_field(item, field_name)
94
  return count
95
 
96
+ print("DEBUG: Counting objects with target field...")
97
  total_objects = count_objects_with_field(data, target_field)
98
+ print(f"DEBUG: Total objects with '{target_field}': {total_objects}")
99
 
100
  # Generate analysis
101
+ print("DEBUG: Generating analysis...")
102
  analysis = {
103
  "summary_fields_detected": summary_fields[:10],
104
  "classification": classification,
 
108
  "summary_sample": summary_sample,
109
  "recommended_fields": []
110
  }
111
+ print(f"DEBUG: Initial analysis: {analysis}")
112
 
113
  # Recommend fields based on priority
114
+ print("DEBUG: Generating field recommendations...")
115
  if summary_fields:
116
  analysis["recommended_fields"].extend(summary_fields[:3])
117
+ print(f"DEBUG: Added summary fields: {summary_fields[:3]}")
118
  if classification.get('config_fields'):
119
  analysis["recommended_fields"].extend(classification['config_fields'][:2])
120
+ print(f"DEBUG: Added config fields: {classification['config_fields'][:2]}")
121
  if sample_object:
122
+ target_related = [k for k in sample_object.keys() if target_field in k.lower()]
123
+ analysis["recommended_fields"].extend(target_related)
124
+ print(f"DEBUG: Added target-related fields: {target_related}")
125
 
126
+ print(f"DEBUG: Final recommended fields: {analysis['recommended_fields']}")
127
+ print("DEBUG: Analysis completed successfully")
128
  return analysis
129
 
130
 
 
179
  )
180
 
181
  if uploaded_file is not None:
182
+ # Debug file upload info
183
+ st.info(f"📁 File uploaded: {uploaded_file.name} (Size: {uploaded_file.size} bytes)")
184
+
185
  # Read and parse JSON
186
  try:
187
+ st.info("🔄 Reading file content...")
188
  content = uploaded_file.read()
189
+ st.info(f"📄 Content length: {len(content)} characters")
190
+
191
+ st.info("🔄 Parsing JSON...")
192
  data = json.loads(content)
193
 
194
  st.success("✅ File loaded successfully!")
195
+ st.info(f"📊 Data structure: {type(data)} with {len(data) if isinstance(data, (dict, list)) else 'unknown'} top-level items")
196
 
197
  # Sidebar for settings
198
  with st.sidebar:
 
208
  # Analyze button
209
  if st.button("🔍 Analyze", type="primary"):
210
  with st.spinner("Analyzing data structure..."):
211
+ st.info(f"🎯 Analyzing with target field: {target_field}")
212
+ try:
213
+ analysis_result = analyze_with_llm(data, target_field)
214
+ st.session_state.analysis_result = analysis_result
215
+ st.session_state.data = data
216
+ st.success("✅ Analysis completed successfully!")
217
+ except Exception as e:
218
+ st.error(f"❌ Analysis failed: {e}")
219
+ st.error(f"Error type: {type(e).__name__}")
220
+ import traceback
221
+ st.code(traceback.format_exc())
222
 
223
  # Display results if available
224
  if st.session_state.analysis_result:
test_analysis.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Test script to verify the analysis functions work correctly without Streamlit.
4
+ """
5
+
6
+ import json
7
+ import sys
8
+ import os
9
+
10
+ # Add src directory to path
11
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
12
+
13
+ from structure_analysis import detect_summary_fields, classify_data_structure, get_hierarchy_summary
14
+
15
+ def analyze_with_llm(data, target_field="rotation_enabled"):
16
+ """
17
+ Analyze data and generate a prompt for LLM analysis.
18
+ Returns structured analysis without requiring Ollama.
19
+ """
20
+ print(f"DEBUG: Starting analysis with target_field: {target_field}")
21
+ print(f"DEBUG: Data type: {type(data)}")
22
+ print(f"DEBUG: Data keys: {list(data.keys()) if isinstance(data, dict) else 'Not a dict'}")
23
+
24
+ # Detect summary fields
25
+ print("DEBUG: Detecting summary fields...")
26
+ summary_fields = detect_summary_fields(data)
27
+ print(f"DEBUG: Found summary fields: {summary_fields}")
28
+
29
+ print("DEBUG: Classifying data structure...")
30
+ classification = classify_data_structure(data)
31
+ print(f"DEBUG: Classification result: {classification}")
32
+
33
+ print("DEBUG: Getting hierarchy summary...")
34
+ hierarchy_summary = get_hierarchy_summary(data)
35
+ print(f"DEBUG: Hierarchy summary: {hierarchy_summary}")
36
+
37
+ # Extract samples
38
+ print("DEBUG: Extracting samples...")
39
+ sample_object = {}
40
+ if 'results' in data:
41
+ print("DEBUG: Found 'results' key in data")
42
+ for section_name, section in data['results'].items():
43
+ print(f"DEBUG: Processing section '{section_name}': {type(section)}")
44
+ if isinstance(section, list) and len(section) > 0:
45
+ sample_object = section[0]
46
+ print(f"DEBUG: Found sample object from list: {sample_object}")
47
+ break
48
+ elif isinstance(section, dict):
49
+ for key, value in section.items():
50
+ if isinstance(value, list) and len(value) > 0:
51
+ sample_object = value[0] if isinstance(value[0], dict) else {}
52
+ print(f"DEBUG: Found sample object from dict list: {sample_object}")
53
+ break
54
+ else:
55
+ print("DEBUG: No 'results' key found in data")
56
+
57
+ summary_sample = data.get('results', {}).get('summary', {}) or data.get('summary', {})
58
+ print(f"DEBUG: Summary sample: {summary_sample}")
59
+
60
+ # Count objects with target field
61
+ def count_objects_with_field(obj, field_name):
62
+ count = 0
63
+ if isinstance(obj, dict):
64
+ if field_name in obj:
65
+ count += 1
66
+ for v in obj.values():
67
+ count += count_objects_with_field(v, field_name)
68
+ elif isinstance(obj, list):
69
+ for item in obj:
70
+ count += count_objects_with_field(item, field_name)
71
+ return count
72
+
73
+ print("DEBUG: Counting objects with target field...")
74
+ total_objects = count_objects_with_field(data, target_field)
75
+ print(f"DEBUG: Total objects with '{target_field}': {total_objects}")
76
+
77
+ # Generate analysis
78
+ print("DEBUG: Generating analysis...")
79
+ analysis = {
80
+ "summary_fields_detected": summary_fields[:10],
81
+ "classification": classification,
82
+ "hierarchy_summary": hierarchy_summary,
83
+ "total_objects": total_objects,
84
+ "sample_object": sample_object,
85
+ "summary_sample": summary_sample,
86
+ "recommended_fields": []
87
+ }
88
+ print(f"DEBUG: Initial analysis: {analysis}")
89
+
90
+ # Recommend fields based on priority
91
+ print("DEBUG: Generating field recommendations...")
92
+ if summary_fields:
93
+ analysis["recommended_fields"].extend(summary_fields[:3])
94
+ print(f"DEBUG: Added summary fields: {summary_fields[:3]}")
95
+ if classification.get('config_fields'):
96
+ analysis["recommended_fields"].extend(classification['config_fields'][:2])
97
+ print(f"DEBUG: Added config fields: {classification['config_fields'][:2]}")
98
+ if sample_object:
99
+ target_related = [k for k in sample_object.keys() if target_field in k.lower()]
100
+ analysis["recommended_fields"].extend(target_related)
101
+ print(f"DEBUG: Added target-related fields: {target_related}")
102
+
103
+ print(f"DEBUG: Final recommended fields: {analysis['recommended_fields']}")
104
+ print("DEBUG: Analysis completed successfully")
105
+ return analysis
106
+
107
+ def test_analysis_functions():
108
+ """Test the analysis functions with sample data."""
109
+
110
+ # Load test data
111
+ with open('test_data.json', 'r') as f:
112
+ data = json.load(f)
113
+
114
+ print("=== Testing Analysis Functions ===")
115
+ print(f"Data loaded: {type(data)}")
116
+ print(f"Data keys: {list(data.keys())}")
117
+ print()
118
+
119
+ # Test detect_summary_fields
120
+ print("Testing detect_summary_fields...")
121
+ summary_fields = detect_summary_fields(data)
122
+ print(f"Summary fields found: {summary_fields}")
123
+ print()
124
+
125
+ # Test classify_data_structure
126
+ print("Testing classify_data_structure...")
127
+ classification = classify_data_structure(data)
128
+ print(f"Classification result: {classification}")
129
+ print()
130
+
131
+ # Test get_hierarchy_summary
132
+ print("Testing get_hierarchy_summary...")
133
+ hierarchy_summary = get_hierarchy_summary(data)
134
+ print(f"Hierarchy summary: {hierarchy_summary}")
135
+ print()
136
+
137
+ # Test the full analysis function
138
+ print("Testing full analysis...")
139
+ try:
140
+ analysis_result = analyze_with_llm(data, "rotation_enabled")
141
+ print("✅ Analysis completed successfully!")
142
+ print(f"Analysis keys: {list(analysis_result.keys())}")
143
+ print(f"Recommended fields: {analysis_result.get('recommended_fields', [])}")
144
+ print(f"Total objects with 'rotation_enabled': {analysis_result.get('total_objects', 0)}")
145
+ except Exception as e:
146
+ print(f"❌ Analysis failed: {e}")
147
+ import traceback
148
+ traceback.print_exc()
149
+
150
+ if __name__ == "__main__":
151
+ test_analysis_functions()
test_data.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "results": {
3
+ "summary": {
4
+ "total_keys": 13,
5
+ "rotated_keys": 6,
6
+ "rotation_percentage": 46,
7
+ "compliance_rate": 85
8
+ },
9
+ "kms_keys": {
10
+ "object": [
11
+ {
12
+ "key_id": "12345",
13
+ "rotation_enabled": true,
14
+ "key_state": "Enabled",
15
+ "creation_date": "2023-01-15"
16
+ },
17
+ {
18
+ "key_id": "67890",
19
+ "rotation_enabled": false,
20
+ "key_state": "Enabled",
21
+ "creation_date": "2023-02-20"
22
+ }
23
+ ]
24
+ },
25
+ "config": {
26
+ "rotation_policy": "enabled",
27
+ "compliance_enforced": true,
28
+ "audit_enabled": true
29
+ }
30
+ }
31
+ }
test_upload.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Test script to verify the file upload functionality works correctly.
4
+ """
5
+
6
+ import json
7
+ import sys
8
+ import os
9
+
10
+ # Add src directory to path
11
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
12
+
13
+ from structure_analysis import detect_summary_fields, classify_data_structure, get_hierarchy_summary
14
+
15
+ def test_analysis_functions():
16
+ """Test the analysis functions with sample data."""
17
+
18
+ # Load test data
19
+ with open('test_data.json', 'r') as f:
20
+ data = json.load(f)
21
+
22
+ print("=== Testing Analysis Functions ===")
23
+ print(f"Data loaded: {type(data)}")
24
+ print(f"Data keys: {list(data.keys())}")
25
+ print()
26
+
27
+ # Test detect_summary_fields
28
+ print("Testing detect_summary_fields...")
29
+ summary_fields = detect_summary_fields(data)
30
+ print(f"Summary fields found: {summary_fields}")
31
+ print()
32
+
33
+ # Test classify_data_structure
34
+ print("Testing classify_data_structure...")
35
+ classification = classify_data_structure(data)
36
+ print(f"Classification result: {classification}")
37
+ print()
38
+
39
+ # Test get_hierarchy_summary
40
+ print("Testing get_hierarchy_summary...")
41
+ hierarchy_summary = get_hierarchy_summary(data)
42
+ print(f"Hierarchy summary: {hierarchy_summary}")
43
+ print()
44
+
45
+ # Test the full analysis function
46
+ print("Testing full analysis...")
47
+ try:
48
+ from streamlit_app import analyze_with_llm
49
+ analysis_result = analyze_with_llm(data, "rotation_enabled")
50
+ print("✅ Analysis completed successfully!")
51
+ print(f"Analysis keys: {list(analysis_result.keys())}")
52
+ print(f"Recommended fields: {analysis_result.get('recommended_fields', [])}")
53
+ except Exception as e:
54
+ print(f"❌ Analysis failed: {e}")
55
+ import traceback
56
+ traceback.print_exc()
57
+
58
+ if __name__ == "__main__":
59
+ test_analysis_functions()