""" TIPM - Tariff Impact Propagation Model ===================================== Hugging Face Spaces Deployment Version Professional economic intelligence platform for tariff impact analysis. Author: Andrew Yeo (TheGeekyBeng) """ import gradio as gr import pandas as pd import numpy as np import plotly.graph_objects as go from plotly.subplots import make_subplots from typing import Dict, List, Tuple, Optional, Any import logging from dataclasses import dataclass, field from datetime import datetime import json import base64 # Import TIPM core components from tipm.core import TIPMModel, TariffShock from tipm.config.settings import TIPMConfig from tipm.config.layer_configs import ( EMERGING_MARKETS, TECH_MANUFACTURING_EXPORTERS, MINING_RESOURCE_EXPORTERS, AGRICULTURAL_EXPORTERS, OFFICIAL_DATA_SOURCES, ) # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @dataclass class EnhancedUICountryData: """Enhanced country data structure for TIPM interface""" name: str tariff_rate: float display_name: str = "" continent: str = "Unknown" global_groups: List[str] = field(default_factory=list) emerging_market_status: bool = False tech_manufacturing_rank: Optional[int] = None resource_export_category: Optional[str] = None export_capabilities: List[str] = field(default_factory=list) gdp_usd_billions: float = 0.0 bilateral_trade_usd_millions: float = 0.0 income_group: str = "Unknown" trade_agreements: List[str] = field(default_factory=list) strategic_commodities: List[str] = field(default_factory=list) data_confidence: str = "Medium" def __post_init__(self): """Enhanced post-init with tooltip generation""" # Validate and clamp tariff rate if not 0 <= self.tariff_rate <= 100: logger.warning( f"Invalid tariff_rate for {self.name}: {self.tariff_rate}%. Clamping to 0-100%" ) self.tariff_rate = max(0, min(100, self.tariff_rate)) # Generate enhanced display name self.display_name = self._generate_enhanced_display_name() def _generate_enhanced_display_name(self) -> str: """Generate display name for dropdown""" return f"{self.name} ({self.tariff_rate:.1f}%)" def get_tooltip_data(self) -> dict: """Generate tooltip data for country""" # Determine main export category main_export = "Mixed Products" if self.resource_export_category: main_export = f"{self.resource_export_category} Exports" elif self.tech_manufacturing_rank and self.tech_manufacturing_rank <= 10: main_export = "Technology & Manufacturing" elif self.export_capabilities: main_export = self.export_capabilities[0] # Determine economic size economic_size = "Large Economy" if self.gdp_usd_billions < 100: economic_size = "Small Economy" elif self.gdp_usd_billions < 1000: economic_size = "Medium Economy" elif self.gdp_usd_billions < 5000: economic_size = "Large Economy" else: economic_size = "Major Economy" # Global economic position global_position = "Regional Player" if "G7" in self.global_groups: global_position = "G7 Major Economy" elif "G20" in self.global_groups: global_position = "G20 Major Economy" elif "BRICS" in self.global_groups: global_position = "BRICS Emerging Market" elif self.emerging_market_status: global_position = "Emerging Market" return { "main_export": main_export, "economic_size": economic_size, "global_position": global_position, "gdp_formatted": f"${self.gdp_usd_billions:.1f}B", "trade_volume": f"${self.bilateral_trade_usd_millions:.1f}M", "confidence": self.data_confidence, } class TIPMInterface: """Enhanced TIPM Interface for Hugging Face Spaces""" def __init__(self): """Initialize TIPM interface""" self.countries_data = [] self.model = None self._load_country_data() self._initialize_model() def _load_country_data(self): """Load enhanced country data with classifications""" logger.info("Loading enhanced country data...") # Load base country data (simplified for HF deployment) countries = [ { "name": "United States", "tariff_rate": 25.0, "continent": "North America", "gdp": 25462.7, }, {"name": "China", "tariff_rate": 25.0, "continent": "Asia", "gdp": 17963.2}, { "name": "Germany", "tariff_rate": 10.0, "continent": "Europe", "gdp": 4072.2, }, {"name": "Japan", "tariff_rate": 7.5, "continent": "Asia", "gdp": 4231.1}, { "name": "United Kingdom", "tariff_rate": 0.0, "continent": "Europe", "gdp": 3070.7, }, {"name": "India", "tariff_rate": 15.0, "continent": "Asia", "gdp": 3385.1}, { "name": "France", "tariff_rate": 0.0, "continent": "Europe", "gdp": 2782.9, }, {"name": "Italy", "tariff_rate": 0.0, "continent": "Europe", "gdp": 2010.4}, { "name": "Brazil", "tariff_rate": 20.0, "continent": "South America", "gdp": 1920.1, }, { "name": "Canada", "tariff_rate": 0.0, "continent": "North America", "gdp": 2139.8, }, ] for country in countries: country_data = EnhancedUICountryData( name=country["name"], tariff_rate=country["tariff_rate"], continent=country["continent"], gdp_usd_billions=country["gdp"], global_groups=self._get_global_groups(country["name"]), emerging_market_status=self._is_emerging_market(country["name"]), tech_manufacturing_rank=self._get_tech_rank(country["name"]), resource_export_category=self._get_resource_category(country["name"]), export_capabilities=self._get_export_capabilities(country["name"]), bilateral_trade_usd_millions=self._estimate_trade_volume( country["name"] ), income_group=self._get_income_group(country["name"]), trade_agreements=self._get_trade_agreements(country["name"]), strategic_commodities=self._get_strategic_commodities(country["name"]), data_confidence="High", ) self.countries_data.append(country_data) logger.info( f"✅ Successfully loaded {len(self.countries_data)} countries with enhanced classifications" ) def _get_global_groups(self, country_name: str) -> List[str]: """Get global group memberships for country""" groups = [] if country_name in [ "United States", "Germany", "Japan", "United Kingdom", "France", "Italy", "Canada", ]: groups.append("G7") if country_name in [ "United States", "China", "Germany", "Japan", "United Kingdom", "France", "Italy", "Canada", "India", "Brazil", ]: groups.append("G20") if country_name in ["Brazil", "Russia", "India", "China", "South Africa"]: groups.append("BRICS") return groups def _is_emerging_market(self, country_name: str) -> bool: """Check if country is an emerging market""" emerging_markets = [ "China", "India", "Brazil", "Russia", "South Africa", "Mexico", "Indonesia", "Turkey", ] return country_name in emerging_markets def _get_tech_rank(self, country_name: str) -> Optional[int]: """Get tech manufacturing rank for country""" tech_ranks = { "China": 1, "United States": 2, "Germany": 3, "Japan": 4, "South Korea": 5, "Taiwan": 6, "Singapore": 7, "Netherlands": 8, "Switzerland": 9, "Sweden": 10, } return tech_ranks.get(country_name) def _get_resource_category(self, country_name: str) -> Optional[str]: """Get resource export category for country""" mining_exporters = ["Australia", "Chile", "Peru", "South Africa", "DR Congo"] ag_exporters = ["Brazil", "Argentina", "Thailand", "Vietnam", "Indonesia"] if country_name in mining_exporters: return "Mining" elif country_name in ag_exporters: return "Agriculture" return None def _get_export_capabilities(self, country_name: str) -> List[str]: """Get export capabilities for country""" capabilities = { "China": ["Electronics", "Textiles", "Machinery"], "Germany": ["Machinery", "Automobiles", "Chemicals"], "Japan": ["Electronics", "Automobiles", "Machinery"], "United States": ["Technology", "Aerospace", "Agriculture"], "Brazil": ["Agriculture", "Mining", "Manufacturing"], } return capabilities.get(country_name, ["Various"]) def _estimate_trade_volume(self, country_name: str) -> float: """Estimate bilateral trade volume for country""" trade_estimates = { "United States": 5000.0, "China": 4500.0, "Germany": 3000.0, "Japan": 2500.0, "United Kingdom": 2000.0, "India": 1500.0, "France": 1800.0, "Italy": 1600.0, "Brazil": 1200.0, "Canada": 2000.0, } return trade_estimates.get(country_name, 1000.0) def _get_income_group(self, country_name: str) -> str: """Get income group for country""" high_income = [ "United States", "Germany", "Japan", "United Kingdom", "France", "Italy", "Canada", ] upper_middle = ["China", "Brazil", "Mexico", "Turkey", "Thailand"] if country_name in high_income: return "High Income" elif country_name in upper_middle: return "Upper Middle Income" else: return "Lower Middle Income" def _get_trade_agreements(self, country_name: str) -> List[str]: """Get trade agreements for country""" agreements = { "United States": ["USMCA", "KORUS", "CAFTA-DR"], "China": ["RCEP", "CAFTA", "APTA"], "Germany": ["EU", "EFTA", "CETA"], "Japan": ["CPTPP", "RCEP", "JEFTA"], "United Kingdom": ["UK-EU", "UK-Japan", "UK-Australia"], } return agreements.get(country_name, []) def _get_strategic_commodities(self, country_name: str) -> List[str]: """Get strategic commodities for country""" commodities = { "China": ["Rare Earths", "Steel", "Aluminum"], "United States": ["Technology", "Aerospace", "Agriculture"], "Germany": ["Machinery", "Automobiles", "Chemicals"], "Japan": ["Electronics", "Automobiles", "Semiconductors"], "Brazil": ["Soybeans", "Iron Ore", "Coffee"], } return commodities.get(country_name, []) def _initialize_model(self): """Initialize TIPM model""" try: config = TIPMConfig() self.model = TIPMModel(config) logger.info("✅ TIPM model initialized successfully") except Exception as e: logger.error(f"❌ Error initializing TIPM model: {str(e)}") self.model = None def get_sorted_countries(self, sort_method: str) -> List[EnhancedUICountryData]: """Get sorted country list""" try: if sort_method == "Alphabetical": return sorted(self.countries_data, key=lambda c: c.name.lower()) elif sort_method == "By Tariff Rate (High to Low)": return sorted( self.countries_data, key=lambda c: c.tariff_rate, reverse=True ) elif sort_method == "By Tariff Rate (Low to High)": return sorted(self.countries_data, key=lambda c: c.tariff_rate) elif sort_method == "By GDP (Largest First)": return sorted( self.countries_data, key=lambda c: c.gdp_usd_billions, reverse=True ) else: return sorted(self.countries_data, key=lambda c: c.name.lower()) except Exception as e: logger.error(f"Error sorting countries: {e}") return self.countries_data def run_tipm_analysis( self, country_name: str, custom_tariff_rate: Optional[float] = None ) -> Dict[str, Any]: """Run TIPM analysis for selected country""" try: # Find country data country = next( (c for c in self.countries_data if c.name == country_name), None ) if not country: return {"error": f"Country {country_name} not found"} # Use custom tariff rate if provided tariff_rate = ( custom_tariff_rate if custom_tariff_rate is not None else country.tariff_rate ) # Generate analysis results (simplified for demo) results = { "country_name": country.name, "tariff_rate": tariff_rate, "continent": country.continent, "gdp_billions": country.gdp_usd_billions, "trade_volume_millions": country.bilateral_trade_usd_millions, "global_groups": ( ", ".join(country.global_groups) if country.global_groups else "None" ), "emerging_market": "Yes" if country.emerging_market_status else "No", "tech_rank": country.tech_manufacturing_rank or "N/A", "resource_category": country.resource_export_category or "Mixed", "export_capabilities": ", ".join(country.export_capabilities), "income_group": country.income_group, "trade_agreements": ( ", ".join(country.trade_agreements) if country.trade_agreements else "None" ), "strategic_commodities": ( ", ".join(country.strategic_commodities) if country.strategic_commodities else "None" ), "data_confidence": country.data_confidence, "analysis_timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), } return results except Exception as e: logger.error(f"Error running TIPM analysis: {e}") return {"error": f"Analysis failed: {str(e)}"} def create_visualization(self, analysis_results: Dict[str, Any]) -> str: """Create interactive visualization for analysis results""" try: if "error" in analysis_results: return ( f"
Professional Economic Intelligence Platform | Advanced ML Pipeline | 185 Countries