-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1265 lines (1071 loc) · 50.1 KB
/
Copy pathapp.py
File metadata and controls
1265 lines (1071 loc) · 50.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
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import random
import json
import time
import sys
import os
from pathlib import Path
from datetime import datetime
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
# Import simulation components
from agents.agent import Agent, AgentFactory, Message
from agents.llm_service import LLMService
from config import AgentStance
from simulation.manager import SimulationManager
from simulation.network import NetworkGenerator, NetworkType, SocialNetwork
from tasks.tasks import (
TaskType, TaskFactory, InfoDiffusionTask, RumorDetectionTask,
StanceEvolutionTask, MultiRoleCollabTask, GroupPolarizationTask
)
from visualization.visualizer import NetworkVisualizer, ChartVisualizer, ConversationVisualizer
from evaluation.metrics import MetricsCalculator, ReportGenerator
from data.loader import DataLoader, SyntheticDataGenerator, create_sample_data
# Page configuration
st.set_page_config(
page_title="Social Simulation Platform",
page_icon="🌐",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
font-size: 2.5rem;
font-weight: 700;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem;
}
.sub-header {
color: #666;
font-size: 1.1rem;
margin-bottom: 2rem;
}
.metric-card {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
padding: 1rem;
border-radius: 10px;
text-align: center;
}
.metric-value {
font-size: 2rem;
font-weight: bold;
color: #333;
}
.metric-label {
color: #666;
font-size: 0.9rem;
}
.status-running {
color: #27ae60;
font-weight: bold;
}
.status-completed {
color: #3498db;
font-weight: bold;
}
.agent-card {
background: white;
border: 1px solid #ddd;
border-radius: 8px;
padding: 10px;
margin: 5px 0;
}
.log-entry {
font-family: monospace;
font-size: 0.85rem;
padding: 5px;
border-left: 3px solid #3498db;
margin: 5px 0;
background: #f8f9fa;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
def init_session_state():
"""Initialize Streamlit session state variables"""
if 'simulation_manager' not in st.session_state:
st.session_state.simulation_manager = None
if 'llm_service' not in st.session_state:
st.session_state.llm_service = LLMService(preferred_client="mock")
if 'simulation_results' not in st.session_state:
st.session_state.simulation_results = None
if 'current_task' not in st.session_state:
st.session_state.current_task = None
if 'logs' not in st.session_state:
st.session_state.logs = []
if 'is_running' not in st.session_state:
st.session_state.is_running = False
init_session_state()
# Sidebar Configuration
def render_sidebar():
"""Render sidebar with configuration options"""
st.sidebar.markdown("## ⚙️ Configuration")
# Platform Mode Selection
st.sidebar.markdown("### 📱 Platform Mode")
platform_mode = st.sidebar.selectbox(
"Simulation Style",
["Twitter", "Reddit", "Weibo", "Generic"],
help="Choose the social platform style for simulation"
)
# Task Selection
st.sidebar.markdown("### 🎯 Task Type")
task_options = {
"Information Diffusion": TaskType.INFO_DIFFUSION,
"Rumor Detection": TaskType.RUMOR_DETECTION,
"Stance Evolution": TaskType.STANCE_EVOLUTION,
"Multi-Role Collaboration": TaskType.MULTI_ROLE_COLLAB,
"Group Polarization": TaskType.GROUP_POLARIZATION,
}
task_name = st.sidebar.selectbox(
"Select Task",
list(task_options.keys()),
help="Choose the type of social simulation task"
)
task_type = task_options[task_name]
# Network Configuration
st.sidebar.markdown("### 🕸️ Network Settings")
# Data Source Selection
data_source = st.sidebar.radio(
"Data Source",
["Generated Network", "Kaggle Dataset", "Upload File"],
help="Choose where to get network/content data"
)
# Initialize all variables with defaults
kaggle_dataset = None
kaggle_username = None
kaggle_key = None
sample_size = 5000
max_edges = 50000
uploaded_file = None
if data_source == "Kaggle Dataset":
with st.sidebar.expander("🔑 Kaggle API Configuration", expanded=True):
kaggle_username = st.text_input("Kaggle Username", key="kaggle_user")
kaggle_key = st.text_input("Kaggle API Key", type="password", key="kaggle_key")
if kaggle_username and kaggle_key:
st.success("✅ Credentials provided")
# Popular datasets for social network analysis (verified on Kaggle)
st.markdown("**Popular Datasets:**")
popular_datasets = {
"Twitter Sentiment (1.6M tweets)": "kazanova/sentiment140",
"Fake News Dataset": "clmentbisaillon/fake-and-real-news-dataset",
"Twitter User Gender": "crowdflower/twitter-user-gender-classification",
"Facebook Social Network": "ashwinpathak/facebook-social-network",
"Social Network Edges": "mathurinache/twitter-edge-nodes",
"Custom...": "custom"
}
dataset_choice = st.selectbox("Select Dataset", list(popular_datasets.keys()))
if dataset_choice == "Custom...":
kaggle_dataset = st.text_input(
"Dataset Reference",
placeholder="owner/dataset-name",
help="Enter Kaggle dataset reference (e.g., 'kazanova/sentiment140')"
)
else:
kaggle_dataset = popular_datasets[dataset_choice]
sample_size = st.number_input("Sample Size (0=all)", 0, 100000, 5000)
max_edges = st.number_input("Max Edges (prevents large dataset issues)", 1000, 100000, 50000)
else:
st.info("Enter Kaggle credentials to load datasets")
elif data_source == "Upload File":
uploaded_file = st.sidebar.file_uploader(
"Upload Network/Content CSV",
type=['csv', 'json'],
help="Upload CSV with 'source,target' columns for network, or 'text,user' for content"
)
# Network type (for generated networks)
if data_source == "Generated Network":
network_options = {
"Barabási-Albert (Scale-Free)": NetworkType.BARABASI_ALBERT,
"Watts-Strogatz (Small-World)": NetworkType.WATTS_STROGATZ,
"Erdős-Rényi (Random)": NetworkType.ERDOS_RENYI,
}
network_name = st.sidebar.selectbox(
"Network Type",
list(network_options.keys())
)
network_type = network_options[network_name]
else:
network_type = NetworkType.CUSTOM
num_agents = st.sidebar.slider("Number of Agents", 5, 100, 20, 5)
# Simulation Parameters
st.sidebar.markdown("### 🔄 Simulation Parameters")
num_rounds = st.sidebar.slider("Number of Rounds", 1, 30, 10)
infection_prob = st.sidebar.slider("Infection Probability", 0.1, 1.0, 0.3, 0.1)
random_seed = st.sidebar.number_input("Random Seed", 0, 9999, 42)
# Advanced Options
with st.sidebar.expander("🔧 Advanced Options"):
enable_persona = st.checkbox("Enable Agent Personas", True)
enable_topic_drift = st.checkbox("Enable Topic Drift", False)
trust_weight = st.slider("Trust Weight", 0.0, 1.0, 0.5, 0.1)
# LLM Configuration
st.sidebar.markdown("### 🤖 LLM Settings")
llm_client = st.sidebar.selectbox(
"LLM Client",
["Mock (No API)", "OpenAI", "DeepSeek", "Anthropic"],
help="Select LLM backend (Mock for testing without API)"
)
# API Key input for real LLM clients
api_key = None
api_base_url = None
llm_model = None
if llm_client != "Mock (No API)":
with st.sidebar.expander("🔑 API Configuration", expanded=True):
api_key = st.text_input(
f"{llm_client} API Key",
type="password",
help=f"Enter your {llm_client} API key"
)
if llm_client == "DeepSeek":
api_base_url = st.text_input(
"API Base URL",
value="https://api.deepseek.com/v1",
help="DeepSeek API endpoint"
)
llm_model = st.selectbox(
"Model",
["deepseek-chat", "deepseek-coder"],
help="Select DeepSeek model"
)
elif llm_client == "OpenAI":
# Support OpenAI-compatible APIs
use_custom_endpoint = st.checkbox("Use Custom Endpoint", False)
if use_custom_endpoint:
api_base_url = st.text_input(
"API Base URL",
placeholder="https://api.openai.com/v1",
help="Custom OpenAI-compatible API endpoint"
)
llm_model = st.selectbox(
"Model",
["gpt-4o-mini", "gpt-4o", "gpt-4-turbo", "gpt-3.5-turbo"],
help="Select OpenAI model"
)
elif llm_client == "Anthropic":
llm_model = st.selectbox(
"Model",
["claude-3-5-sonnet-20241022", "claude-3-haiku-20240307", "claude-3-opus-20240229"],
help="Select Anthropic model"
)
if api_key:
st.success("✅ API Key provided")
else:
st.warning("⚠️ Enter API Key to use this LLM")
return {
"platform_mode": platform_mode.lower(),
"task_type": task_type,
"network_type": network_type,
"num_agents": num_agents,
"num_rounds": num_rounds,
"infection_prob": infection_prob,
"random_seed": random_seed,
"enable_persona": enable_persona,
"enable_topic_drift": enable_topic_drift,
"trust_weight": trust_weight,
"llm_client": llm_client.lower().split()[0],
"api_key": api_key,
"api_base_url": api_base_url,
"llm_model": llm_model,
# Data source options
"data_source": data_source,
"kaggle_dataset": kaggle_dataset if data_source == "Kaggle Dataset" else None,
"kaggle_username": kaggle_username if data_source == "Kaggle Dataset" else None,
"kaggle_key": kaggle_key if data_source == "Kaggle Dataset" else None,
"kaggle_sample_size": sample_size if data_source == "Kaggle Dataset" else None,
"kaggle_max_edges": max_edges if data_source == "Kaggle Dataset" else None,
"uploaded_file": uploaded_file if data_source == "Upload File" else None,
}
def render_task_config(task_type: TaskType):
"""Render task-specific configuration options"""
st.markdown("### 📋 Task Configuration")
config = {}
if task_type == TaskType.INFO_DIFFUSION:
col1, col2 = st.columns(2)
with col1:
config["message_content"] = st.text_area(
"Initial Message",
"Breaking news: Major announcement expected today!",
height=100
)
config["source_count"] = st.number_input("Number of Sources", 1, 5, 1)
with col2:
config["enable_intervention"] = st.checkbox("Enable Intervention", False)
if config["enable_intervention"]:
config["intervention_round"] = st.number_input("Intervention Round", 1, 20, 5)
config["intervention_type"] = st.selectbox(
"Intervention Type",
["counter_message", "reduce_spread"]
)
elif task_type == TaskType.RUMOR_DETECTION:
col1, col2 = st.columns(2)
with col1:
config["rumor_content"] = st.text_area(
"Rumor Message",
"ALERT: Unverified reports suggest company bankruptcy is imminent!",
height=100
)
with col2:
config["truth_content"] = st.text_area(
"Truth/Counter Message",
"FACT CHECK: The bankruptcy rumors are FALSE. Official statements confirm company stability. ✅",
height=100
)
config["fact_checker_ratio"] = st.slider("Fact-Checker Ratio", 0.0, 0.3, 0.1, 0.05)
config["rumor_delay_rounds"] = st.number_input("Rounds Before Counter-Message", 1, 10, 3)
elif task_type == TaskType.STANCE_EVOLUTION:
config["topic"] = st.text_input(
"Debate Topic",
"AI regulation should be significantly stricter"
)
col1, col2 = st.columns(2)
with col1:
config["initial_support_ratio"] = st.slider("Initial Support Ratio", 0.0, 1.0, 0.4, 0.1)
with col2:
config["initial_oppose_ratio"] = st.slider("Initial Oppose Ratio", 0.0, 1.0, 0.4, 0.1)
elif task_type == TaskType.MULTI_ROLE_COLLAB:
st.info("💡 Each agent will receive partial information. They must collaborate to solve the puzzle.")
puzzle = SyntheticDataGenerator.generate_collaboration_puzzle()
config["puzzle_type"] = "mystery"
config["clues"] = puzzle["clues"]
config["answer"] = puzzle["answer"]
with st.expander("View Puzzle Details"):
st.write(f"**Puzzle:** {puzzle['title']}")
st.write("**Clues:**")
for i, clue in enumerate(puzzle['clues'], 1):
st.write(f" {i}. {clue}")
st.write(f"**Answer:** {puzzle['answer']}")
elif task_type == TaskType.GROUP_POLARIZATION:
config["n_communities"] = st.number_input("Number of Communities", 2, 5, 2)
config["topic"] = st.text_input(
"Polarizing Topic",
"Climate change requires immediate drastic action"
)
col1, col2 = st.columns(2)
with col1:
config["intra_connection_prob"] = st.slider("Intra-Community Connection", 0.1, 0.8, 0.4, 0.1)
with col2:
config["inter_connection_prob"] = st.slider("Inter-Community Connection", 0.0, 0.3, 0.05, 0.01)
return config
def run_simulation(config: dict, task_config: dict):
"""Run the simulation with given configuration"""
with st.spinner("🔄 Initializing simulation..."):
# Update LLM service with API key and configuration
st.session_state.llm_service = LLMService(
preferred_client=config["llm_client"],
api_key=config.get("api_key"),
api_base_url=config.get("api_base_url"),
model=config.get("llm_model")
)
# Show LLM status
available = st.session_state.llm_service.available_clients()
if config["llm_client"] != "mock" and config["llm_client"] in available:
st.success(f"✅ Using {config['llm_client'].upper()} LLM")
elif config["llm_client"] != "mock":
st.warning(f"⚠️ {config['llm_client'].upper()} not available, using Mock LLM")
# Create simulation manager
manager = SimulationManager(llm_service=st.session_state.llm_service)
# Check data source
data_source = config.get("data_source", "Generated Network")
if data_source == "Kaggle Dataset" and config.get("kaggle_dataset"):
# Load from Kaggle
try:
from data.loader import KaggleLoader
loader = KaggleLoader()
# Configure credentials
if not loader.configure_api(
username=config["kaggle_username"],
key=config["kaggle_key"]
):
st.error("Failed to configure Kaggle API credentials")
raise Exception("Kaggle auth failed")
st.info(f"📥 Downloading dataset: {config['kaggle_dataset']}...")
# Download dataset
data_path = loader.download_dataset(config["kaggle_dataset"])
if not data_path:
raise Exception("Download failed")
# Convert dataset_id to folder name (kaggle uses owner-dataset format)
dataset_folder = config["kaggle_dataset"].replace("/", "-")
# Import to simulation format
dataset_info = loader.import_to_simulation(
dataset_name=dataset_folder,
max_nodes=config["num_agents"],
max_edges=config.get("kaggle_max_edges", 50000),
max_content=1000
)
if dataset_info and dataset_info.get("nodes"):
# Network dataset - create agents from nodes
from agents.agent import AgentFactory
from simulation.network import SocialNetwork
nodes = dataset_info["nodes"][:config["num_agents"]]
edges = dataset_info.get("edges", [])
manager.agents = AgentFactory.create_population(
n_agents=len(nodes),
llm_service=manager.llm_service
)
# Build network
manager.network = SocialNetwork()
node_map = {old: agent.agent_id for old, agent in zip(nodes, manager.agents)}
for agent in manager.agents:
manager.network.add_node(agent.agent_id)
for src, dst in edges:
if src in node_map and dst in node_map:
manager.network.add_edge(node_map[src], node_map[dst])
manager._index_agents()
manager.platform_mode = config.get("platform_mode", "generic")
manager.trust_weight = config.get("trust_weight", 0.5)
# Setup trust networks
import random
for agent in manager.agents:
following = manager.network.get_following(agent.agent_id)
for fid in following:
agent.trust_network[fid] = 0.5 + 0.3 * random.random()
st.success(f"✅ Loaded {len(manager.agents)} agents, {len(edges)} edges from Kaggle")
else:
# Content dataset or failed - fallback to generated network
st.warning("Dataset doesn't contain network structure, using generated network")
manager.setup(
n_agents=config["num_agents"],
network_type=config["network_type"],
seed=config["random_seed"],
platform_mode=config.get("platform_mode", "generic"),
trust_weight=config.get("trust_weight", 0.5)
)
# Inject content from dataset as seed messages
if dataset_info and dataset_info.get("content"):
import random
sample_content = random.sample(
dataset_info["content"],
min(3, len(dataset_info["content"]))
)
for item in sample_content:
text = item.get("text", item.get("content", ""))[:500]
if text and manager.agents:
src = random.choice(manager.agents)
manager.inject_message(text, [src.agent_id])
except ImportError:
st.error("Kaggle package not installed. Run: pip install kaggle")
return None
except Exception as e:
st.error(f"Failed to load Kaggle dataset: {e}")
# Fallback to generated network
manager.setup(
n_agents=config["num_agents"],
network_type=config["network_type"],
seed=config["random_seed"],
platform_mode=config.get("platform_mode", "generic"),
trust_weight=config.get("trust_weight", 0.5)
)
elif data_source == "Upload File" and config.get("uploaded_file"):
# Load from uploaded file
try:
import tempfile
import pandas as pd
# Save uploaded file temporarily
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
tmp.write(config["uploaded_file"].getvalue())
tmp_path = tmp.name
# Try to parse the file
df = pd.read_csv(tmp_path)
cols_lower = [c.lower() for c in df.columns]
# Check if network or content
if 'source' in cols_lower and 'target' in cols_lower:
# Network format
from agents.agent import AgentFactory
from simulation.network import SocialNetwork
src_col = df.columns[cols_lower.index('source')]
tgt_col = df.columns[cols_lower.index('target')]
edges = list(zip(df[src_col].astype(str), df[tgt_col].astype(str)))
nodes = list(set(df[src_col].astype(str)) | set(df[tgt_col].astype(str)))
nodes = nodes[:config["num_agents"]]
manager.agents = AgentFactory.create_population(
n_agents=len(nodes),
llm_service=manager.llm_service
)
manager.network = SocialNetwork()
node_map = {old: agent.agent_id for old, agent in zip(nodes, manager.agents)}
for agent in manager.agents:
manager.network.add_node(agent.agent_id)
for src, dst in edges:
if src in node_map and dst in node_map:
manager.network.add_edge(node_map[src], node_map[dst])
manager._index_agents()
manager.platform_mode = config.get("platform_mode", "generic")
manager.trust_weight = config.get("trust_weight", 0.5)
import random
for agent in manager.agents:
following = manager.network.get_following(agent.agent_id)
for fid in following:
agent.trust_network[fid] = 0.5 + 0.3 * random.random()
st.success(f"✅ Loaded {len(manager.agents)} agents from uploaded network")
else:
# Content format - use generated network but inject content
manager.setup(
n_agents=config["num_agents"],
network_type=config["network_type"],
seed=config["random_seed"],
platform_mode=config.get("platform_mode", "generic"),
trust_weight=config.get("trust_weight", 0.5)
)
# Find text column
text_col = None
for c in df.columns:
if any(x in c.lower() for x in ['text', 'content', 'body', 'message']):
text_col = c
break
if text_col:
import random
sample_texts = df[text_col].dropna().sample(min(3, len(df))).tolist()
for text in sample_texts:
if text and manager.agents:
src = random.choice(manager.agents)
manager.inject_message(str(text)[:500], [src.agent_id])
st.success(f"✅ Created network and injected {len(sample_texts)} seed messages")
else:
st.success(f"✅ Created network with {len(manager.agents)} agents")
# Cleanup temp file
os.unlink(tmp_path)
except Exception as e:
st.error(f"Failed to load uploaded file: {e}")
# Fallback
manager.setup(
n_agents=config["num_agents"],
network_type=config["network_type"],
seed=config["random_seed"],
platform_mode=config.get("platform_mode", "generic"),
trust_weight=config.get("trust_weight", 0.5)
)
else:
# Generated network (default)
manager.setup(
n_agents=config["num_agents"],
network_type=config["network_type"],
seed=config["random_seed"],
platform_mode=config.get("platform_mode", "generic"),
enable_persona=config.get("enable_persona", True),
enable_topic_drift=config.get("enable_topic_drift", False),
trust_weight=config.get("trust_weight", 0.5)
)
st.session_state.simulation_manager = manager
# Create and setup task
task = TaskFactory.create(config["task_type"], manager)
task.setup(**task_config)
st.session_state.current_task = task
# Progress tracking
progress_bar = st.progress(0)
status_text = st.empty()
# Run simulation via task.run() - this ensures task-specific logic is executed
st.session_state.is_running = True
status_text.text("Running simulation...")
# Use task.run() to execute the simulation with task-specific logic
task_result = task.run(
n_rounds=config["num_rounds"],
infection_probability=config["infection_prob"]
)
# Update progress
progress_bar.progress(1.0)
# Get results
eval_result = task.evaluate()
st.session_state.simulation_results = {
"task_result": eval_result,
"manager_results": manager.get_results(),
"round_data": manager.round_data.copy()
}
st.session_state.is_running = False
status_text.text("✅ Simulation completed!")
return st.session_state.simulation_results
def render_results():
"""Render simulation results and visualizations"""
if not st.session_state.simulation_results:
st.info("Run a simulation to see results here.")
return
results = st.session_state.simulation_results
manager = st.session_state.simulation_manager
st.markdown("## 📊 Results")
# Key Metrics
col1, col2, col3, col4 = st.columns(4)
final_state = results["manager_results"]["final_state"]
with col1:
st.metric("Rounds", final_state["rounds_completed"])
with col2:
st.metric("Coverage", f"{final_state['coverage']:.1%}")
with col3:
st.metric("Messages", final_state["total_messages"])
with col4:
st.metric("Shares", final_state["total_shares"])
# Tabs for different visualizations
tab1, tab2, tab3, tab4 = st.tabs(["📈 Charts", "🕸️ Network", "👥 Agents", "📝 Logs"])
with tab1:
render_charts(results)
with tab2:
render_network(manager)
with tab3:
render_agents(manager)
with tab4:
render_logs(manager)
def render_charts(results):
"""Render result charts"""
col1, col2 = st.columns(2)
round_data = results.get("round_data", [])
with col1:
# Coverage over time
if round_data:
coverage_data = [r.get("coverage", 0) for r in round_data]
fig = ChartVisualizer.create_coverage_chart(coverage_data)
if fig:
st.plotly_chart(fig, use_container_width=True)
else:
st.line_chart(coverage_data)
with col2:
# Stance distribution
final_state = results["manager_results"]["final_state"]
stance_dist = final_state.get("stance_distribution", {})
if stance_dist:
fig = ChartVisualizer.create_stance_distribution_chart(stance_dist)
if fig:
st.plotly_chart(fig, use_container_width=True)
else:
st.bar_chart(stance_dist)
# Additional metrics based on task type
task_result = results.get("task_result", {})
if "polarization_history" in task_result:
st.markdown("### Polarization Trend")
fig = ChartVisualizer.create_polarization_chart(task_result["polarization_history"])
if fig:
st.plotly_chart(fig, use_container_width=True)
if "stance_history" in task_result:
st.markdown("### Stance Evolution")
fig = ChartVisualizer.create_stance_evolution_chart(task_result["stance_history"])
if fig:
st.plotly_chart(fig, use_container_width=True)
def render_network(manager: SimulationManager):
"""Render network visualization"""
if not manager or not manager.network:
st.warning("No network data available.")
return
network = manager.network
# Prepare node data
node_colors = {}
node_sizes = {}
node_labels = {}
for agent in manager.agents:
infected = agent.agent_id in manager.state.infected_nodes
node_colors[agent.agent_id] = '#e74c3c' if infected else '#2ecc71'
node_sizes[agent.agent_id] = 15 + agent.influence_score * 20
node_labels[agent.agent_id] = agent.name
# Try NetworkX-based visualization first
fig = NetworkVisualizer.create_network_figure(
nodes=network.nodes,
edges=network.edges,
node_colors=node_colors,
node_sizes=node_sizes,
node_labels=node_labels,
title="Social Network (Red = Infected, Green = Not Infected)"
)
if fig:
st.plotly_chart(fig, use_container_width=True)
else:
# Fallback: Pure Plotly visualization without networkx
try:
import plotly.graph_objects as go
import math
# Simple circular layout
nodes = list(network.nodes)
n = len(nodes)
# Calculate positions in a circle
pos = {}
for i, node in enumerate(nodes):
angle = 2 * math.pi * i / n
pos[node] = (math.cos(angle), math.sin(angle))
# Create edge traces
edge_x = []
edge_y = []
for src, tgt in network.edges:
if src in pos and tgt in pos:
x0, y0 = pos[src]
x1, y1 = pos[tgt]
edge_x.extend([x0, x1, None])
edge_y.extend([y0, y1, None])
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines'
)
# Create node traces
node_x = [pos[n][0] for n in nodes]
node_y = [pos[n][1] for n in nodes]
colors = [node_colors.get(n, '#2ecc71') for n in nodes]
sizes = [node_sizes.get(n, 15) for n in nodes]
labels = [node_labels.get(n, n) for n in nodes]
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers+text',
hoverinfo='text',
text=labels,
textposition="top center",
textfont=dict(size=8),
marker=dict(
color=colors,
size=sizes,
line=dict(width=1, color='white')
)
)
fig = go.Figure(
data=[edge_trace, node_trace],
layout=go.Layout(
title='Social Network (Red = Infected, Green = Not Infected)',
showlegend=False,
hovermode='closest',
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
height=500
)
)
st.plotly_chart(fig, use_container_width=True)
except ImportError:
st.info("Network visualization requires plotly library. Install with: pip install plotly")
except Exception as e:
st.warning(f"Network visualization error: {e}")
# Always show network stats
st.write(f"**Nodes:** {len(network.nodes)}")
st.write(f"**Edges:** {len(network.edges)}")
st.write(f"**Network Type:** {network.network_type.value}")
def render_agents(manager: SimulationManager):
"""Render agent information"""
if not manager or not manager.agents:
st.warning("No agent data available.")
return
agents_data = manager.get_agent_states()
# Filters
col1, col2 = st.columns(2)
with col1:
filter_infected = st.selectbox("Filter by Status", ["All", "Infected", "Not Infected"])
with col2:
filter_stance = st.selectbox("Filter by Stance", ["All"] + [s.name for s in AgentStance])
# Apply filters
filtered_agents = agents_data
if filter_infected == "Infected":
filtered_agents = [a for a in filtered_agents if a["infected"]]
elif filter_infected == "Not Infected":
filtered_agents = [a for a in filtered_agents if not a["infected"]]
if filter_stance != "All":
filtered_agents = [a for a in filtered_agents if a["stance"] == filter_stance]
st.write(f"Showing {len(filtered_agents)} of {len(agents_data)} agents")
# Display agents in grid
cols = st.columns(3)
for idx, agent in enumerate(filtered_agents):
with cols[idx % 3]:
status_color = "🔴" if agent["infected"] else "🟢"
stance_emoji = {"SUPPORT": "👍", "OPPOSE": "👎", "NEUTRAL": "😐",
"STRONG_SUPPORT": "💪👍", "STRONG_OPPOSE": "💪👎"}.get(agent["stance"], "❓")
st.markdown(f"""
<div class="agent-card">
<strong>{agent['name']}</strong> {status_color}<br>
Stance: {stance_emoji} {agent['stance']}<br>
Followers: {agent['followers']}<br>
Influence: {agent['influence']:.2f}
</div>
""", unsafe_allow_html=True)
def render_logs(manager: SimulationManager):
"""Render simulation logs"""
if not manager or not manager.logs:
st.info("No logs available.")
return
# Filter options
log_types = list(set(log.event_type for log in manager.logs))
selected_types = st.multiselect("Filter by Event Type", log_types, default=log_types)
max_logs = st.slider("Max Logs to Display", 10, 200, 50)
# Filter and display logs
filtered_logs = [log for log in manager.logs if log.event_type in selected_types]
for log in filtered_logs[-max_logs:]:
type_emoji = {
"setup": "⚙️",
"inject": "💉",
"reply": "💬",
"share": "🔄",
"stance_change": "🔀",
"round_start": "▶️",
"round_end": "⏹️",
}.get(log.event_type, "📝")
st.markdown(f"""
<div class="log-entry">
{type_emoji} <strong>Round {log.round_num}</strong> [{log.event_type}]: {log.content[:150]}
</div>
""", unsafe_allow_html=True)
def render_data_management():
"""Render data management section with Kaggle API support"""
st.markdown("## 📁 Data Management")
loader = DataLoader()
create_sample_data() # Ensure sample data exists
# Create tabs for different data sources
data_tab1, data_tab2, data_tab3 = st.tabs(["📂 Local Data", "🏆 Kaggle API", "🔧 Generate Data"])
with data_tab1:
st.markdown("### Available Local Datasets")
datasets = loader.list_available_datasets()
if datasets:
for ds in datasets:
st.write(f"📄 {ds}")
else:
st.info("No datasets found. Upload data or generate synthetic data.")
# File upload
st.markdown("### Upload Dataset")
uploaded_file = st.file_uploader(
"Upload network or content file",
type=['csv', 'json', 'tsv'],
help="Upload a CSV/TSV edge list or JSON dataset"
)
if uploaded_file:
file_type = st.radio("File type:", ["Network (edges)", "Content (messages)"])
if st.button("Import File"):
try:
if file_type == "Network (edges)":
save_path = os.path.join(loader.networks_path, uploaded_file.name)
else:
save_path = os.path.join(loader.content_path, uploaded_file.name)
with open(save_path, 'wb') as f:
f.write(uploaded_file.getbuffer())
st.success(f"Saved to {save_path}")
st.rerun()
except Exception as e:
st.error(f"Error: {e}")
with data_tab2:
render_kaggle_section()
with data_tab3:
st.markdown("### Generate Synthetic Data")
col1, col2 = st.columns(2)
with col1:
if st.button("🔗 Generate Sample Network"):
nodes = [f"user_{i}" for i in range(20)]
network = NetworkGenerator.generate_barabasi_albert(20, 2, nodes, seed=42)
loader.save_network(network.nodes, network.edges, "generated_network.json")
st.success("Generated network saved!")
st.rerun()
with col2:
if st.button("💬 Generate Sample Messages"):
messages = SyntheticDataGenerator.generate_messages(20)
loader.save_content(messages, "generated_messages.json")
st.success("Generated messages saved!")
st.rerun()
st.markdown("---")
st.markdown("### Custom Generation")
gen_col1, gen_col2 = st.columns(2)