Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·770 lines (655 loc) · 33 KB
/
Copy pathapp.py
File metadata and controls
executable file
·770 lines (655 loc) · 33 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
"""
Streamlit Demo for Theory-Driven Multi-Agent Recommendation System
Interactive interface for urban AI recommendations
"""
"""
Streamlit Demo for Theory-Driven Multi-Agent Recommendation System
Interactive interface for urban AI recommendations
"""
import streamlit as st
import asyncio
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime
import time
import json
import os
import sys
import numpy as np
# Import the recommendation system components
try:
# Import directly from the same directory
import recommendation_system
# Import required classes from the module
UrbanAIRecommendationSystem = recommendation_system.UrbanAIRecommendationSystem
UrbanScenario = recommendation_system.UrbanScenario
UrbanTheory = recommendation_system.UrbanTheory
Algorithm = recommendation_system.Algorithm
DataSource = recommendation_system.DataSource
Recommendation = recommendation_system.Recommendation
display_recommendation = recommendation_system.display_recommendation
except ImportError as e:
st.error(f"⚠️ Could not import the recommendation system. Error: {str(e)}")
st.error("Please ensure recommendation_system.py is in the same directory as app.py")
st.error(f"Current working directory: {os.getcwd()}")
st.error(f"Files in directory: {os.listdir('.')}")
st.stop()
except Exception as e:
st.error(f"⚠️ Could not import the recommendation system. Error: {str(e)}")
st.error(f"Error type: {type(e).__name__}")
st.error(f"Current working directory: {os.getcwd()}")
import traceback
st.error(f"Traceback: {traceback.format_exc()}")
st.stop()
# Page configuration
st.set_page_config(
page_title="Urban AI Recommendation System",
page_icon="🏙️",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
font-size: 4.5rem;
font-weight: 700;
text-align: center;
background: linear-gradient(120deg, #1e3c72, #2a5298);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 2rem;
}
.sub-header {
text-align: center;
color: #666;
font-size: 1.6rem;
margin-bottom: 3rem;
font-weight: 500;
}
.agent-card {
background-color: #f0f2f6;
padding: 1.5rem;
border-radius: 10px;
margin-bottom: 1rem;
border-left: 4px solid #1e3c72;
}
.metric-card {
background-color: #ffffff;
padding: 1rem;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
text-align: center;
}
.recommendation-section {
background-color: #f8f9fa;
padding: 2rem;
border-radius: 10px;
margin-top: 2rem;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.stTabs [data-baseweb="tab-list"] button {
font-size: 1.1rem;
font-weight: 600;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
if 'system' not in st.session_state:
st.session_state.system = None
st.session_state.recommendation = None
st.session_state.processing_steps = []
st.session_state.current_challenge = ""
st.session_state.selected_case = ""
st.session_state.case_studies = {}
# Helper functions
def initialize_system():
"""Initialize the recommendation system"""
with st.spinner("🔧 Initializing Urban AI Recommendation System..."):
try:
system = UrbanAIRecommendationSystem()
st.session_state.system = system
return True
except Exception as e:
st.error(f"Failed to initialize system: {str(e)}")
return False
def create_agent_status_card(agent_name, status, description="", progress=0):
"""Create a status card for an agent"""
status_icons = {
"pending": "⏳",
"processing": "🔄",
"completed": "✅",
"error": "❌"
}
icon = status_icons.get(status, "❓")
col1, col2 = st.columns([1, 9])
with col1:
st.markdown(f"<h1>{icon}</h1>", unsafe_allow_html=True)
with col2:
st.markdown(f"**{agent_name}**")
if description:
st.caption(description)
if status == "processing" and progress > 0:
st.progress(progress)
async def process_challenge_async(system, challenge):
"""Process urban challenge asynchronously with status updates"""
steps = []
# Stage 1: Scenario Analysis
st.session_state.processing_steps.append(("Scenario Analyzer", "processing", "Analyzing urban challenge...", 20))
scenario = await system.scenario_analyzer.process(challenge)
steps.append({
"agent": "Scenario Analyzer",
"result": f"Domain: {scenario.domain}, Complexity: {scenario.complexity_score:.2f}",
"data": scenario
})
st.session_state.processing_steps[-1] = ("Scenario Analyzer", "completed", f"Domain: {scenario.domain}", 100)
# Stage 2: Theory Retrieval
st.session_state.processing_steps.append(("Theory Retriever", "processing", "Retrieving relevant theories...", 40))
theories = await system.theory_retriever.process(scenario)
steps.append({
"agent": "Theory Retriever",
"result": f"Retrieved {len(theories)} theories",
"data": theories
})
st.session_state.processing_steps[-1] = (
"Theory Retriever", "completed", f"Found {len(theories)} relevant theories", 100)
# Stage 3: Algorithm Matching
st.session_state.processing_steps.append(
("Algorithm Matcher", "processing", "Matching algorithms to theories...", 60))
algorithms = await system.algorithm_matcher.process(theories, scenario)
steps.append({
"agent": "Algorithm Matcher",
"result": f"Matched {len(algorithms)} algorithms",
"data": algorithms
})
st.session_state.processing_steps[-1] = (
"Algorithm Matcher", "completed", f"Matched {len(algorithms)} algorithms", 100)
# Stage 4: Data Source Selection
st.session_state.processing_steps.append(("Data Source Selector", "processing", "Selecting data sources...", 80))
data_sources = await system.data_selector.process(algorithms, scenario)
steps.append({
"agent": "Data Source Selector",
"result": f"Selected {len(data_sources)} data sources",
"data": data_sources
})
st.session_state.processing_steps[-1] = (
"Data Source Selector", "completed", f"Selected {len(data_sources)} data sources", 100)
# Stage 5: Integration Validation
st.session_state.processing_steps.append(
("Integration Validator", "processing", "Validating recommendations...", 90))
recommendation = await system.validator.process(theories, algorithms, data_sources, scenario)
steps.append({
"agent": "Integration Validator",
"result": f"Confidence: {recommendation.confidence_score:.2f}",
"data": recommendation
})
st.session_state.processing_steps[-1] = ("Integration Validator", "completed", f"Validation complete", 100)
return recommendation, steps
def visualize_theory_distribution(theories):
"""Create a pie chart of theory categories"""
categories = [t.category for t in theories]
df = pd.DataFrame({"Category": categories})
fig = px.pie(df, names="Category", title="Theory Distribution by Category",
color_discrete_map={"Safety": "#FF6B6B", "Design": "#4ECDC4",
"Spatial": "#45B7D1", "Perception": "#FFA07A"})
return fig
def visualize_algorithm_capabilities(algorithms):
"""Create a radar chart of algorithm capabilities"""
# Aggregate capabilities across all algorithms
all_capabilities = {}
for algo in algorithms:
for cap, score in algo.capabilities.items():
if cap not in all_capabilities:
all_capabilities[cap] = []
all_capabilities[cap].append(score)
# Calculate average scores
avg_capabilities = {cap: sum(scores) / len(scores) for cap, scores in all_capabilities.items()}
# Create radar chart
categories = list(avg_capabilities.keys())
values = list(avg_capabilities.values())
fig = go.Figure(data=go.Scatterpolar(
r=values,
theta=categories,
fill='toself',
name='Average Capability Score'
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 1]
)),
showlegend=False,
title="Algorithm Capabilities Overview"
)
return fig
def visualize_data_quality(data_sources):
"""Create a heatmap of data source quality scores"""
# Prepare data for heatmap
source_names = [ds.name for ds in data_sources]
quality_dimensions = list(data_sources[0].quality_scores.keys()) if data_sources else []
# Create matrix
quality_matrix = []
for ds in data_sources:
scores = [ds.quality_scores.get(dim, 0) for dim in quality_dimensions]
quality_matrix.append(scores)
fig = go.Figure(data=go.Heatmap(
z=quality_matrix,
x=quality_dimensions,
y=source_names,
colorscale='Viridis',
text=[[f"{score:.2f}" for score in row] for row in quality_matrix],
texttemplate="%{text}",
textfont={"size": 10}
))
fig.update_layout(
title="Data Source Quality Assessment",
xaxis_title="Quality Dimensions",
yaxis_title="Data Sources",
height=400
)
return fig
def visualize_case_transformation(case_data):
"""Create a visual representation of the transformation process"""
fig = go.Figure()
# Add traces for original vs transformed approach
categories = ['Problem Focus', 'Theory Integration', 'Algorithm Sophistication',
'Data Comprehensiveness', 'Outcome Impact']
# Original approach scores (lower)
original_scores = [3, 1, 4, 3, 4] # Simulated scores
# Theory-driven scores (higher)
theory_driven_scores = [9, 10, 9, 9, 10] # Simulated scores
fig.add_trace(go.Scatterpolar(
r=original_scores,
theta=categories,
fill='toself',
name='Original Approach',
line_color='#FF6B6B'
))
fig.add_trace(go.Scatterpolar(
r=theory_driven_scores,
theta=categories,
fill='toself',
name='Theory-Driven Approach',
line_color='#45B7D1'
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 10]
)),
title="Transformation Impact Analysis",
showlegend=True,
height=400
)
return fig
# Main App
def main():
# Header
st.markdown('<h1 class="main-header" style="font-size: 4.5rem !important;">🏙️ Urban AI Recommendation System</h1>',
unsafe_allow_html=True)
st.markdown(
'<p class="sub-header" style="font-size: 1.6rem !important;">Theory-Driven Multi-Agent System for Urban Challenge Solutions</p>',
unsafe_allow_html=True)
# Initialize selected_case and selected_example
selected_case = ""
selected_example = ""
# Sidebar
with st.sidebar:
st.header("📋 System Overview")
st.info("""
This system bridges the theory-practice gap in urban AI applications through:
• **5 Specialized Agents** working collaboratively
• **Theory-First Approach** grounding AI in urban planning principles
• **Comprehensive Validation** ensuring practical feasibility
""")
st.header("🔧 Configuration")
# Remove the data_path configuration since we're not using local paths anymore
st.write("System configured for cloud deployment")
if st.button("🚀 Initialize System"):
if initialize_system():
st.success("✅ System initialized successfully!")
else:
st.error("❌ Failed to initialize system")
# Example challenges and case studies
st.header("💡 Examples & Cases")
# Tab selection for examples vs case studies
example_type = st.radio("Choose type:", ["Quick Examples", "Research Case Studies"])
if example_type == "Quick Examples":
examples = [
"Reduce crime rates in downtown neighborhoods by improving street lighting and surveillance while maintaining resident privacy",
"Optimize public transportation routes to reduce traffic congestion during peak hours in the city center",
"Design a sustainable mixed-use development that promotes walkability and community interaction",
"Implement smart parking solutions to reduce urban congestion and improve air quality",
"Create safer pedestrian zones in high-traffic areas using AI-powered traffic management"
]
selected_example = st.selectbox("Select an example:", [""] + examples)
selected_case = "" # Clear case selection
else:
case_studies = {
"Case A: Problem-Driven (Food Waste Crisis)": {
"challenge": "A major US city discards 10,000 tons of edible food annually while significant populations experience food insecurity. We need to optimize food recovery and distribution to address both waste reduction and food access equity.",
"original_approach": "Continuous approximation methods for vehicle routing optimization focusing on minimizing transportation costs",
"complexity": 0.82,
"transformation": {
"theories": ["Urban Metabolism Theory", "Environmental Justice Theory"],
"algorithms": ["MOEA/D", "Graph Neural Networks", "Temporal LSTM"],
"data_sources": ["Food Bank Networks Database", "Social Vulnerability Index",
"Real-time Food Supply APIs", "Transportation Networks"],
"data_focus": "From routes to multi-domain: social vulnerability, food supply, environmental impact",
"outcome": "Integrated food security framework balancing efficiency and equity"
}
},
"Case B: Method-Driven (Urban Heat Island)": {
"challenge": "Traditional 3D morphological indices achieve only R²<0.8 in Urban Heat Island modeling. We need to develop enhanced stereoscopic urban morphology metrics with improved prediction accuracy while ensuring practical planning applications.",
"original_approach": "New stereoscopic metrics with XGBoost achieving R²=0.95, focused on technical performance",
"complexity": 0.76,
"transformation": {
"theories": ["Urban Climate Theory", "Compact City Theory", "Sustainable Urban Design Theory"],
"algorithms": ["Physics-Informed Neural Networks", "Spatial-GCN", "XGBoost", "SHAP"],
"data_sources": ["LiDAR Point Clouds", "Thermal Satellite Imagery", "Building Energy Database",
"Demographic Census Data"],
"data_focus": "From morphology to comprehensive: LiDAR, thermal, social vulnerability, infrastructure",
"outcome": "Vulnerability-aware prediction tool supporting planning decisions"
}
},
"Case C: Technology-Driven (GRU-CNN for Resilience)": {
"challenge": "We have developed a GRU-CNN architecture and need to find meaningful urban applications that leverage its real-time processing and predictive modeling capabilities for disaster resilience.",
"original_approach": "GRU-CNN technology demonstration without specific disaster events or theoretical frameworks",
"complexity": 0.88,
"transformation": {
"theories": ["Urban Resilience Theory", "Disaster Risk Reduction Framework",
"Socio-Ecological Systems Theory"],
"algorithms": ["GRU-CNN", "Agent-Based Modeling", "Network Analysis", "Reinforcement Learning"],
"data_sources": ["IoT Sensor Networks", "Emergency Response Systems", "Social Media Streams",
"Infrastructure Networks GIS"],
"data_focus": "From technology demo to comprehensive: hazard sensors, infrastructure networks, social patterns",
"outcome": "Community-centered resilience platform with participatory interfaces"
}
}
}
selected_case = st.selectbox("Select a case study:", [""] + list(case_studies.keys()))
if selected_case and selected_case in case_studies:
case = case_studies[selected_case]
selected_example = case["challenge"]
st.session_state.selected_case = selected_case
st.session_state.case_studies = case_studies
if selected_case and selected_case in case_studies:
case = case_studies[selected_case]
selected_example = case["challenge"]
# Show case study details
with st.expander("📋 Case Study Details", expanded=True):
st.markdown(f"**Original Approach:** {case['original_approach']}")
st.markdown(f"**Complexity Score:** {case['complexity']}")
st.markdown("**🔄 Expected Transformation:**")
trans = case['transformation']
st.markdown(f"- **Theories:** {', '.join(trans['theories'])}")
st.markdown(f"- **Algorithms:** {', '.join(trans['algorithms'])}")
st.markdown(f"- **Data Sources:** {', '.join(trans['data_sources'])}")
st.markdown(f"- **Data Evolution:** {trans['data_focus']}")
st.markdown(f"- **Outcome:** {trans['outcome']}")
st.info("💡 Run the analysis to see how our system transforms this challenge!")
else:
selected_example = ""
# Clear selection button
if selected_example or (example_type == "Research Case Studies" and selected_case):
if st.button("🗑️ Clear Selection", use_container_width=True):
st.session_state.selected_case = ""
st.rerun()
# Main content area
if st.session_state.system is None:
st.warning("⚠️ Please initialize the system using the sidebar button.")
return
# Input section
col1, col2 = st.columns([4, 1])
with col1:
challenge = st.text_area(
"🎯 Describe your urban challenge:",
value=selected_example if selected_example else st.session_state.current_challenge,
height=100,
placeholder="Example: We need to reduce crime rates in downtown neighborhoods..."
)
with col2:
st.write("") # Spacing
st.write("")
analyze_button = st.button("🔍 Analyze Challenge", type="primary", use_container_width=True)
# Processing section
if analyze_button and challenge:
st.session_state.current_challenge = challenge
st.session_state.processing_steps = []
# Create placeholders for dynamic updates
status_container = st.container()
# Run async processing
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
with st.spinner("Processing urban challenge..."):
recommendation, steps = loop.run_until_complete(
process_challenge_async(st.session_state.system, challenge)
)
st.session_state.recommendation = recommendation
except Exception as e:
st.error(f"Error processing challenge: {str(e)}")
return
finally:
loop.close()
# Display processing steps
with status_container:
st.header("🤖 Agent Processing Status")
for agent_name, status, desc, progress in st.session_state.processing_steps:
create_agent_status_card(agent_name, status, desc, progress)
st.success("✅ Analysis complete! See recommendations below.")
# Results section
if st.session_state.recommendation:
st.markdown('<div class="recommendation-section">', unsafe_allow_html=True)
st.header("📊 Recommendation Results")
# Confidence metrics
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Confidence Score", f"{st.session_state.recommendation.confidence_score:.2%}")
with col2:
st.metric("Theories Selected", len(st.session_state.recommendation.theories))
with col3:
human_val = "Yes" if st.session_state.recommendation.validation_results[
'requires_human_validation'] else "No"
st.metric("Human Validation Required", human_val)
# Show case study comparison if applicable
if st.session_state.selected_case and st.session_state.selected_case in st.session_state.case_studies:
st.markdown("---")
st.subheader("🔄 Case Study Transformation Analysis")
case = st.session_state.case_studies[st.session_state.selected_case]
# Transformation visualization
transformation_fig = visualize_case_transformation(case)
st.plotly_chart(transformation_fig, use_container_width=True)
col1, col2 = st.columns(2)
with col1:
st.markdown("##### 📌 Original Approach")
st.info(case['original_approach'])
st.markdown("##### ❌ Limitations")
if "Case A" in st.session_state.selected_case:
st.write("• Focus only on cost minimization")
st.write("• Ignores food insecurity patterns")
st.write("• No equity considerations")
elif "Case B" in st.session_state.selected_case:
st.write("• Pure technical performance focus")
st.write("• No planning integration")
st.write("• Limited actionability")
else: # Case C
st.write("• Technology without application")
st.write("• No theoretical grounding")
st.write("• Missing community context")
with col2:
st.markdown("##### 🎯 Theory-Driven Enhancement")
# Show expected vs actual theories
expected_theories = case['transformation']['theories']
actual_theories = [t.name for t in st.session_state.recommendation.theories]
st.write("**Expected Theories:**")
for theory in expected_theories:
if any(theory.lower() in t.lower() for t in actual_theories):
st.write(f"✅ {theory}")
else:
st.write(f"⚪ {theory}")
# Show expected data sources
st.write("\n**Expected Data Sources:**")
for ds in case['transformation']['data_sources']:
st.write(f"📊 {ds}")
st.write("\n**Key Improvements:**")
st.success(case['transformation']['outcome'])
# Additional metrics comparison
st.markdown("---")
st.markdown("##### 📊 Transformation Metrics")
metric_col1, metric_col2, metric_col3, metric_col4 = st.columns(4)
with metric_col1:
st.metric("Complexity", f"{case['complexity']:.2f}", "+0.3")
with metric_col2:
st.metric("Theory Integration", "High", "↑")
with metric_col3:
st.metric("Data Sources", f"{len(case['transformation']['data_sources'])}",
f"+{len(case['transformation']['data_sources'])}")
with metric_col4:
st.metric("Algorithm Types", f"{len(case['transformation']['algorithms'])}", "↑")
# Lessons learned section
st.markdown("---")
st.markdown("##### 💡 Key Lessons from Transformation")
lessons_col1, lessons_col2 = st.columns(2)
with lessons_col1:
st.markdown("**🎯 Strategic Insights:**")
if "Case A" in st.session_state.selected_case:
st.write("• Theory grounds technical solutions in social context")
st.write("• Multi-objective optimization enables equity integration")
st.write("• Comprehensive data reveals hidden vulnerabilities")
elif "Case B" in st.session_state.selected_case:
st.write("• Physical principles enhance ML model reliability")
st.write("• Interpretability enables planning applications")
st.write("• Social data transforms accuracy into actionability")
else: # Case C
st.write("• Theory provides purpose for advanced technology")
st.write("• Community needs guide technical capabilities")
st.write("• Integration creates resilience beyond prediction")
with lessons_col2:
st.markdown("**🔧 Implementation Guidance:**")
if "Case A" in st.session_state.selected_case:
st.write("• Start with stakeholder mapping")
st.write("• Balance efficiency with equity metrics")
st.write("• Design for iterative improvement")
elif "Case B" in st.session_state.selected_case:
st.write("• Embed domain knowledge in models")
st.write("• Prioritize actionable outputs")
st.write("• Validate with planning practitioners")
else: # Case C
st.write("• Co-design with communities")
st.write("• Build adaptive feedback loops")
st.write("• Ensure local knowledge integration")
# Tabbed results
tab1, tab2, tab3, tab4, tab5 = st.tabs(
["📚 Theories", "🤖 Algorithms", "📊 Data Sources", "📈 Visualizations", "⚠️ Validation"])
with tab1:
st.subheader("Recommended Urban Theories")
for theory in st.session_state.recommendation.theories:
with st.expander(f"**{theory.name}** ({theory.year}) - {theory.category}", expanded=True):
st.write("**Key Principles:**")
for principle in theory.principles:
st.write(f"• {principle}")
st.write("**Computational Requirements:**")
st.write(", ".join(theory.computational_requirements))
with tab2:
st.subheader("Recommended AI/ML Algorithms")
for algo in st.session_state.recommendation.algorithms:
with st.expander(f"**{algo.name}** (Group {algo.group})", expanded=True):
st.write("**Key Capabilities:**")
capability_df = pd.DataFrame(
[(cap, score) for cap, score in algo.capabilities.items()],
columns=["Capability", "Score"]
)
st.dataframe(capability_df, use_container_width=True)
st.write(f"**Computational Cost:** {algo.computational_cost:.2f}")
st.write(f"**Data Requirements:** {', '.join(algo.data_requirements)}")
with tab3:
st.subheader("Recommended Data Sources")
for source in st.session_state.recommendation.data_sources:
with st.expander(f"**{source.name}** ({source.type})", expanded=True):
col1, col2 = st.columns(2)
with col1:
st.metric("Accessibility", f"{source.accessibility:.1%}")
with col2:
st.metric("Update Frequency", source.update_frequency)
st.write("**Quality Scores:**")
quality_df = pd.DataFrame(
[(dim, score) for dim, score in source.quality_scores.items()],
columns=["Dimension", "Score"]
)
st.bar_chart(quality_df.set_index("Dimension"))
with tab4:
st.subheader("System Analysis Visualizations")
col1, col2 = st.columns(2)
with col1:
if st.session_state.recommendation.theories:
fig1 = visualize_theory_distribution(st.session_state.recommendation.theories)
st.plotly_chart(fig1, use_container_width=True)
with col2:
if st.session_state.recommendation.algorithms:
fig2 = visualize_algorithm_capabilities(st.session_state.recommendation.algorithms)
st.plotly_chart(fig2, use_container_width=True)
if st.session_state.recommendation.data_sources:
fig3 = visualize_data_quality(st.session_state.recommendation.data_sources)
st.plotly_chart(fig3, use_container_width=True)
with tab5:
st.subheader("Integration Validation Results")
val_results = st.session_state.recommendation.validation_results
# Robustness score
st.metric("Robustness Score", f"{val_results['robustness']:.2%}")
# Compatibility issues
if val_results['compatibility_issues']:
st.warning("⚠️ Compatibility Issues Detected:")
for issue in val_results['compatibility_issues']:
st.write(f"• {issue}")
else:
st.success("✅ No compatibility issues detected")
# Human validation recommendation
if val_results['requires_human_validation']:
st.info("""
🧑💼 **Human Validation Recommended**
Based on the confidence score and compatibility analysis, we recommend
human expert review before implementation. Key areas to review:
- Theory-algorithm alignment
- Data source sufficiency
- Practical implementation constraints
""")
st.markdown('</div>', unsafe_allow_html=True)
# Export functionality
st.header("📥 Export Recommendation")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("📄 Export as JSON"):
# Create exportable format
export_data = {
"timestamp": datetime.now().isoformat(),
"challenge": st.session_state.current_challenge,
"confidence_score": st.session_state.recommendation.confidence_score,
"theories": [{"name": t.name, "category": t.category, "year": t.year}
for t in st.session_state.recommendation.theories],
"algorithms": [{"name": a.name, "group": a.group, "capabilities": a.capabilities}
for a in st.session_state.recommendation.algorithms],
"data_sources": [{"name": d.name, "type": d.type, "accessibility": d.accessibility}
for d in st.session_state.recommendation.data_sources],
"validation": st.session_state.recommendation.validation_results
}
st.download_button(
label="Download JSON",
data=json.dumps(export_data, indent=2),
file_name=f"urban_ai_recommendation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
mime="application/json"
)
with col2:
if st.button("📊 Generate Report"):
st.info("Report generation feature coming soon!")
with col3:
if st.button("🔄 New Analysis"):
st.session_state.recommendation = None
st.session_state.current_challenge = ""
st.session_state.selected_case = ""
st.rerun()
if __name__ == "__main__":
main()