















Code Block
Python
1
model = DiscreteBayesianNetwork([
('Ecological_Security', 'EthicalRisks'),
('natural_environment', 'Ecological_Security'),
('ecosystem_balance', 'Ecological_Security'),
('genetic_abnormality', 'Ecological_Security'),
('natural_gene_pool', 'Ecological_Security')])
Code Block
Python
1
cpd_ecosecurity = TabularCPD(
'Ecological_Security', 2,
[
[1, 0.766, 0.739, 0.505, 0.752, 0.5184, 0.491, 0.2572,
0.7428, 0.509, 0.4816, 0.248, 0.495, 0.261, 0.234, 0],
[0, 0.234, 0.261, 0.495, 0.248, 0.4816, 0.509, 0.7428,
0.2572, 0.491, 0.5184, 0.752, 0.505, 0.739, 0.766, 1]
],
evidence=['natural_environment',
'genetic_abnormality', 'ecosystem_balance', 'natural_gene_pool'],
evidence_card=[2, 2, 2, 2])
Code Block
Python
1
model.add_cpds(cpd_eco_balance, ...) assert model.check_model()
Code Block
Python
1
def predict_ethical_risks(evidence_dict):
2
Example: {'natural_environment': 1, 'genetic_abnormality': 0, ...}
3
// 0 False, 1 True
4
model = build_model()
5
inference = VariableElimination(model)
6
result = inference.query(variables=['EthicalRisks'], evidence=evidence_dict)
7
return result
Code Block
Python
1
if __name__ == "__main__":
2
evidence = {
3
'natural_environment': 1,
4
'genetic_abnormality': 0,
5
'biological_warfare': 1,
6
'biological_weapon': 0
7
}
8
res = predict_ethical_risks(evidence)
9
print(res)
Code Block
Python
1
def score_to_soft_evidence(score):
2
p1 = score / 100.0
3
return {0: 1 - p1, 1: p1}
Code Block
Python
1
def predict_ethical_risks_sampling(model, soft_evidence_dict, num_samples=5000):
2
sampler = BayesianModelSampling(model)
3
evidence_states = []
4
for node, dist in soft_evidence_dict.items():
5
chosen_state = np.random.choice(list(dist.keys()), p=list(dist.values()))
6
evidence_states.append(State(node, chosen_state))
7
samples = sampler.likelihood_weighted_sample(evidence=evidence_states, size=num_samples)
8
unique, counts = np.unique(samples['EthicalRisks'], return_counts=True)
9
probs = {state: count / num_samples for state, count in zip(unique, counts)}
10
return probs
Code Block
Python
1
if __name__ == "__main__":
2
model = build_model()
3
score_inputs = {
4
'natural_environment': 75.2,
5
'genetic_abnormality': 50.6,
6
'biological_warfare': 90,
7
'democratic_review': 64,
8
}
9
soft_evidence = {node: score_to_soft_evidence(score)
10
for node, score in score_inputs.items()}
11
result = predict_ethical_risks_sampling(model, soft_evidence, num_samples=10000)
12
print("EthicalRisks :", result)
Code Block
JSON
1
{
2
"evidence": {
3
"natural_environment": 1,
4
"genetic_abnormality": 0,
5
"natural_gene_pool": 1,
6
"ecosystem_balance": 1,
7
"biological_warfare": 0,
8
"biological_weapon": 0,
9
"technology_leakage": 1,
10
"technological_equity": 0,
11
"intergenerational_equity": 1,
12
"technology_limitation": 0,
13
"ethical_preassessment": 1,
14
"Minimization_of_harm_to_the_public": 1,
15
"public_acceptance": 0,
16
"democratic_review": 1,
17
"legal_regulation": 1,
18
"academic_freedom": 0,
19
"ip_rights": 1
20
}
21
}
Code Block
JSON
1
{
2
"results": {
3
"EthicalRisks": 1,
4
"low_risk": 0.72,
5
"high_risk": 0.28
6
},
7
"Ecological_Security": {
8
"low_risk": 0.65,
9
"high_risk": 0.35
10
},
11
"Biological_weapon_Risk": {
12
"low_risk": 0.88,
13
"high_risk": 0.12
14
},
15
"Technology_Governance": {
16
"low_risk": 0.56,
17
"high_risk": 0.44
18
},
19
"Public_Acceptance": {
20
"low_risk": 0.81,
21
"high_risk": 0.19
22
},
23
"Regulatory_Framework": {
24
"low_risk": 0.74,
25
"high_risk": 0.26
26
}
27
},
28
"evidence": {
29
"natural_environment": 1,
30
"genetic_abnormality": 0,
31
"...": "..."
32
},
33
"success": true
34
}
Code Block
JSON
POST /api/predict
Content-Type: application/json
1
{
2
"evidence": {
3
"natural_environment": 1,
4
"genetic_abnormality": 0,
5
"natural_gene_pool": 1,
6
"ecosystem_balance": 1,
7
"biological_warfare": 0,
8
"biological_weapon": 0,
9
"technology_leakage": 1,
10
"technological_equity": 0,
11
"intergenerational_equity": 1,
12
"technology_limitation": 0,
13
"ethical_preassessment": 1,
14
"Minimization_of_harm_to_the_public": 1,
15
"public_acceptance": 0,
16
"democratic_review": 1,
17
"legal_regulation": 1,
18
"academic_freedom": 0,
19
"ip_rights": 1
20
}
21
}
Code Block
JSON
1
{
2
"results": {
3
"EthicalRisks": {"low_risk": 0.68, "high_risk": 0.32},
4
"Ecological_Security": {"low_risk": 0.59, "high_risk": 0.41},
5
"Biological_weapon_Risk": {"low_risk": 0.92, "high_risk": 0.08},
6
"Technology_Governance": {"low_risk": 0.61, "high_risk": 0.39},
7
"Public_Acceptance": {"low_risk": 0.79, "high_risk": 0.21},
8
"Regulatory_Framework": {"low_risk": 0.71, "high_risk": 0.29}
9
},
10
"evidence": {
11
"natural_environment": 1,
12
"genetic_abnormality": 0,
13
"...": "..."
14
},
15
"success": true
16
}
Code Block
JSON
1
{
2
"scores": {
3
"natural_environment": 75,
4
"genetic_abnormality": 40,
5
"biological_warfare": 60,
6
"technology_leakage": 30,
7
"public_acceptance": 85,
8
"democratic_review": 50
9
}
10
}
Code Block
JSON
1
{
2
"results": {
3
"EthicalRisks": {
4
"low_risk": 0.31,
5
"high_risk": 0.69
6
},
7
"Ecological_Security": {
8
"low_risk": 0.42,
9
"high_risk": 0.58
10
},
11
"Biological_weapon_Risk": {
12
"low_risk": 0.77,
13
"high_risk": 0.23
14
},
15
"Technology_Governance": {
16
"low_risk": 0.49,
17
"high_risk": 0.51
18
},
19
"Public_Acceptance": {
20
"low_risk": 0.15,
21
"high_risk": 0.85
22
},
23
"Regulatory_Framework": {
24
"low_risk": 0.62,
25
"high_risk": 0.38
26
}
27
},
28
"scores": {
29
"natural_environment": 75,
30
"genetic_abnormality": 40,
31
"biological_warfare": 60,
32
"technology_leakage": 30,
33
"public_acceptance": 85,
34
"democratic_review": 50
35
},
36
"success": true
37
}
Code Block
JSON
POST /api/predict_pro
Content-Type: application/json
1
{
2
"scores": {
3
"natural_environment": 75,
4
"genetic_abnormality": 40,
5
"biological_warfare": 60,
6
"technology_leakage": 30,
7
"public_acceptance": 85,
8
"democratic_review": 50
9
}
10
}
Code Block
JSON
1
{
2
"results": {
3
"EthicalRisks": {"low_risk": 0.31, "high_risk": 0.69},
4
"EthicalRisks": {"low_risk": 0.31, "high_risk": 0.69},
5
"Biological_weapon_Risk": {"low_risk": 0.77, "high_risk": 0.23},
6
"Technology_Governance": {"low_risk": 0.49, "high_risk": 0.51},
7
"Public_Acceptance": {"low_risk": 0.15, "high_risk": 0.85},
8
"Regulatory_Framework": {"low_risk": 0.62, "high_risk": 0.38}
9
},
10
"scores": {
11
"natural_environment": 75,
12
"genetic_abnormality": 40,
13
"biological_warfare": 60,
14
"technology_leakage": 30,
15
"public_acceptance": 85,
16
"democratic_review": 50
17
},
18
"success": true
19
}








