Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -395,6 +395,14 @@ Provide specific numerical estimates and actionable recommendations for economic
|
|
| 395 |
return state
|
| 396 |
|
| 397 |
# Helper methods for analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
def _stability_risk_level(self, total_threat: float) -> str:
|
| 399 |
"""Calculate stability risk level from total threat"""
|
| 400 |
if total_threat >= 0.8: return "CRITICAL"
|
|
@@ -402,11 +410,6 @@ Provide specific numerical estimates and actionable recommendations for economic
|
|
| 402 |
elif total_threat >= 0.4: return "MODERATE"
|
| 403 |
elif total_threat >= 0.2: return "LOW"
|
| 404 |
else: return "MINIMAL"
|
| 405 |
-
if level >= 0.8: return "CRITICAL"
|
| 406 |
-
elif level >= 0.6: return "HIGH"
|
| 407 |
-
elif level >= 0.4: return "MODERATE"
|
| 408 |
-
elif level >= 0.2: return "LOW"
|
| 409 |
-
else: return "MINIMAL"
|
| 410 |
|
| 411 |
def _ai_threat_analysis(self, score: float) -> str:
|
| 412 |
if score >= 0.8: return "AGI/Singularity risk, massive economic disruption"
|
|
@@ -444,18 +447,102 @@ Provide specific numerical estimates and actionable recommendations for economic
|
|
| 444 |
elif score >= 0.4: return "Space security concerns, increased space militarization"
|
| 445 |
else: return "Stable space environment, continued commercial growth"
|
| 446 |
|
| 447 |
-
def
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
if scores.
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
| 460 |
def _calculate_gdp_impact(self, scores: TechScores) -> float:
|
| 461 |
"""Calculate projected GDP impact"""
|
|
@@ -789,7 +876,7 @@ def load_model():
|
|
| 789 |
logger.error(f"Failed to load model: {str(e)}")
|
| 790 |
return False
|
| 791 |
|
| 792 |
-
def generate_response(prompt,
|
| 793 |
"""Generate response using the loaded model pipeline"""
|
| 794 |
try:
|
| 795 |
if not chat_pipeline:
|
|
@@ -801,7 +888,7 @@ def generate_response(prompt, max_length=512, temperature=0.7):
|
|
| 801 |
# Generate response
|
| 802 |
response = chat_pipeline(
|
| 803 |
formatted_prompt,
|
| 804 |
-
|
| 805 |
temperature=temperature,
|
| 806 |
do_sample=True,
|
| 807 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 395 |
return state
|
| 396 |
|
| 397 |
# Helper methods for analysis
|
| 398 |
+
def _threat_level_description(self, total_threat: float) -> str:
|
| 399 |
+
"""Get threat level description from total threat score"""
|
| 400 |
+
if total_threat >= 0.8: return "CRITICAL"
|
| 401 |
+
elif total_threat >= 0.6: return "HIGH"
|
| 402 |
+
elif total_threat >= 0.4: return "MODERATE"
|
| 403 |
+
elif total_threat >= 0.2: return "LOW"
|
| 404 |
+
else: return "MINIMAL"
|
| 405 |
+
|
| 406 |
def _stability_risk_level(self, total_threat: float) -> str:
|
| 407 |
"""Calculate stability risk level from total threat"""
|
| 408 |
if total_threat >= 0.8: return "CRITICAL"
|
|
|
|
| 410 |
elif total_threat >= 0.4: return "MODERATE"
|
| 411 |
elif total_threat >= 0.2: return "LOW"
|
| 412 |
else: return "MINIMAL"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
def _ai_threat_analysis(self, score: float) -> str:
|
| 415 |
if score >= 0.8: return "AGI/Singularity risk, massive economic disruption"
|
|
|
|
| 447 |
elif score >= 0.4: return "Space security concerns, increased space militarization"
|
| 448 |
else: return "Stable space environment, continued commercial growth"
|
| 449 |
|
| 450 |
+
def _calculate_gdp_impact(self, scores: TechScores) -> float:
|
| 451 |
+
"""Calculate projected GDP impact"""
|
| 452 |
+
base_impact = scores.get_total_threat_level() * -15 # Base negative impact
|
| 453 |
+
|
| 454 |
+
# Amplification factors
|
| 455 |
+
if scores.ai >= 0.8: base_impact *= 1.5 # AI singularity amplification
|
| 456 |
+
if scores.climate >= 0.8: base_impact *= 1.3 # Climate catastrophe
|
| 457 |
+
if scores.nuclear >= 0.6: base_impact *= 2.0 # Nuclear conflict
|
| 458 |
+
|
| 459 |
+
return max(base_impact, -50) # Cap at -50% GDP impact
|
| 460 |
+
|
| 461 |
+
def _assess_regional_vulnerabilities(self, scores: TechScores) -> str:
|
| 462 |
+
"""Assess regional vulnerabilities"""
|
| 463 |
+
regions = {
|
| 464 |
+
'North America': (scores.ai * 0.8 + scores.cyber * 0.9 + scores.climate * 0.6) / 3,
|
| 465 |
+
'Europe': (scores.cyber * 0.7 + scores.climate * 0.8 + scores.nuclear * 0.6) / 3,
|
| 466 |
+
'Asia Pacific': (scores.ai * 0.9 + scores.cyber * 0.8 + scores.climate * 0.7) / 3,
|
| 467 |
+
'China': (scores.ai * 0.9 + scores.cyber * 0.8 + scores.space * 0.7) / 3,
|
| 468 |
+
'Russia': (scores.nuclear * 0.9 + scores.cyber * 0.7 + scores.space * 0.6) / 3,
|
| 469 |
+
'Middle East': (scores.nuclear * 0.8 + scores.climate * 0.9 + scores.cyber * 0.5) / 3,
|
| 470 |
+
'Africa': (scores.climate * 0.9 + scores.bio * 0.7 + scores.cyber * 0.4) / 3,
|
| 471 |
+
'South America': (scores.climate * 0.8 + scores.bio * 0.6 + scores.cyber * 0.4) / 3
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
return "\n".join([f"- {region}: {vuln:.3f} vulnerability index" for region, vuln in regions.items()])
|
| 475 |
+
|
| 476 |
+
def _calculate_cascade_probability(self, scores: TechScores) -> float:
|
| 477 |
+
"""Calculate cascade failure probability"""
|
| 478 |
+
base_prob = scores.get_total_threat_level() * 0.8
|
| 479 |
+
|
| 480 |
+
# Cross-domain amplification
|
| 481 |
+
if scores.ai >= 0.7 and scores.cyber >= 0.7: base_prob += 0.2
|
| 482 |
+
if scores.climate >= 0.8: base_prob += 0.15
|
| 483 |
+
if scores.nuclear >= 0.5: base_prob += 0.25
|
| 484 |
+
|
| 485 |
+
return min(base_prob, 1.0)
|
| 486 |
+
|
| 487 |
+
def _assess_systemic_risk(self, scores: TechScores) -> str:
|
| 488 |
+
"""Assess systemic risk level"""
|
| 489 |
+
risk_score = (scores.ai * 0.25 + scores.cyber * 0.25 + scores.nuclear * 0.2 +
|
| 490 |
+
scores.climate * 0.15 + scores.bio * 0.1 + scores.space * 0.05)
|
| 491 |
+
|
| 492 |
+
if risk_score >= 0.8: return "EXISTENTIAL - Civilization-level threats"
|
| 493 |
+
elif risk_score >= 0.6: return "CRITICAL - System-wide failure risk"
|
| 494 |
+
elif risk_score >= 0.4: return "HIGH - Major disruption likely"
|
| 495 |
+
elif risk_score >= 0.2: return "MODERATE - Manageable with preparation"
|
| 496 |
+
else: return "LOW - Standard risk management sufficient"
|
| 497 |
+
|
| 498 |
+
def _generate_risk_scenarios(self, scores: TechScores) -> str:
|
| 499 |
+
"""Generate risk scenarios"""
|
| 500 |
+
scenarios = []
|
| 501 |
+
|
| 502 |
+
if scores.ai >= 0.8:
|
| 503 |
+
scenarios.append("AI Singularity Scenario: Rapid economic obsolescence, mass unemployment")
|
| 504 |
+
if scores.cyber >= 0.7 and scores.ai >= 0.5:
|
| 505 |
+
scenarios.append("Cyber-AI Warfare: Autonomous attacks on critical infrastructure")
|
| 506 |
+
if scores.climate >= 0.8:
|
| 507 |
+
scenarios.append("Climate Collapse: Supply chain breakdown, mass migration")
|
| 508 |
+
if scores.nuclear >= 0.6:
|
| 509 |
+
scenarios.append("Nuclear Exchange: Regional economic devastation, global recession")
|
| 510 |
+
if scores.bio >= 0.7:
|
| 511 |
+
scenarios.append("Pandemic Scenario: Healthcare collapse, economic shutdown")
|
| 512 |
+
|
| 513 |
+
return "\n".join([f"- {scenario}" for scenario in scenarios]) if scenarios else "- No critical scenarios identified"
|
| 514 |
+
|
| 515 |
+
def _immediate_policy_actions(self, scores: TechScores) -> str:
|
| 516 |
+
"""Generate immediate policy actions"""
|
| 517 |
+
actions = []
|
| 518 |
+
|
| 519 |
+
if scores.ai >= 0.7:
|
| 520 |
+
actions.append("Emergency AI safety protocols, regulatory frameworks")
|
| 521 |
+
if scores.cyber >= 0.6:
|
| 522 |
+
actions.append("Critical infrastructure protection, cyber emergency response")
|
| 523 |
+
if scores.nuclear >= 0.5:
|
| 524 |
+
actions.append("Nuclear security enhancement, crisis communication protocols")
|
| 525 |
+
if scores.climate >= 0.7:
|
| 526 |
+
actions.append("Climate emergency declarations, adaptation funding")
|
| 527 |
+
if scores.bio >= 0.6:
|
| 528 |
+
actions.append("Public health emergency preparedness, medical stockpiling")
|
| 529 |
+
|
| 530 |
+
return "\n".join([f"- {action}" for action in actions]) if actions else "- Continue standard monitoring and preparedness"
|
| 531 |
+
|
| 532 |
+
def _resource_allocation_priorities(self, scores: TechScores) -> str:
|
| 533 |
+
"""Calculate resource allocation priorities"""
|
| 534 |
+
priorities = []
|
| 535 |
+
threat_scores = scores.to_dict()
|
| 536 |
+
|
| 537 |
+
# Calculate resource allocation based on threat levels
|
| 538 |
+
total_threat = sum(threat_scores.values())
|
| 539 |
+
if total_threat > 0:
|
| 540 |
+
for threat, score in threat_scores.items():
|
| 541 |
+
percentage = (score / total_threat) * 100
|
| 542 |
+
if percentage >= 15: # Significant allocation threshold
|
| 543 |
+
priorities.append(f"{threat} defense/mitigation: {percentage:.1f}% of emergency resources")
|
| 544 |
+
|
| 545 |
+
return "\n".join([f"- {priority}" for priority in priorities]) if priorities else "- Balanced resource allocation across all domains"
|
| 546 |
|
| 547 |
def _calculate_gdp_impact(self, scores: TechScores) -> float:
|
| 548 |
"""Calculate projected GDP impact"""
|
|
|
|
| 876 |
logger.error(f"Failed to load model: {str(e)}")
|
| 877 |
return False
|
| 878 |
|
| 879 |
+
def generate_response(prompt, temperature=0.7):
|
| 880 |
"""Generate response using the loaded model pipeline"""
|
| 881 |
try:
|
| 882 |
if not chat_pipeline:
|
|
|
|
| 888 |
# Generate response
|
| 889 |
response = chat_pipeline(
|
| 890 |
formatted_prompt,
|
| 891 |
+
max_new_tokens=256, # Use only max_new_tokens to avoid conflict
|
| 892 |
temperature=temperature,
|
| 893 |
do_sample=True,
|
| 894 |
pad_token_id=tokenizer.eos_token_id,
|