thegeekybeng's picture
πŸš€ Deploy TIPM v1.5 - Professional Economic Intelligence Platform
8986ff6
"""
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"<div style='color: red;'>Error: {analysis_results['error']}</div>"
)
# Create subplot
fig = make_subplots(
rows=2,
cols=2,
subplot_titles=(
"Economic Profile",
"Trade Analysis",
"Global Position",
"Export Capabilities",
),
specs=[
[{"type": "indicator"}, {"type": "bar"}],
[{"type": "pie"}, {"type": "bar"}],
],
)
# Economic Profile (Gauge)
fig.add_trace(
go.Indicator(
mode="gauge+number+delta",
value=analysis_results["gdp_billions"],
title={"text": f"GDP (${analysis_results['gdp_billions']:.1f}B)"},
delta={"reference": 2000},
gauge={
"axis": {"range": [None, 30000]},
"bar": {"color": "darkblue"},
"steps": [
{"range": [0, 1000], "color": "lightgray"},
{"range": [1000, 5000], "color": "gray"},
{"range": [5000, 30000], "color": "darkgray"},
],
"threshold": {
"line": {"color": "red", "width": 4},
"thickness": 0.75,
"value": 25000,
},
},
),
row=1,
col=1,
)
# Trade Volume Bar
fig.add_trace(
go.Bar(
x=["Trade Volume"],
y=[analysis_results["trade_volume_millions"]],
name="Bilateral Trade",
marker_color="green",
),
row=1,
col=2,
)
# Global Groups Pie
groups = (
analysis_results["global_groups"].split(", ")
if analysis_results["global_groups"] != "None"
else []
)
if groups:
fig.add_trace(
go.Pie(
labels=groups, values=[1] * len(groups), name="Global Groups"
),
row=2,
col=1,
)
# Export Capabilities Bar
capabilities = analysis_results["export_capabilities"].split(", ")
fig.add_trace(
go.Bar(
x=capabilities,
y=[1] * len(capabilities),
name="Export Capabilities",
marker_color="orange",
),
row=2,
col=2,
)
# Update layout
fig.update_layout(
title=f"TIPM Analysis: {analysis_results['country_name']}",
height=600,
showlegend=False,
)
return fig.to_html(include_plotlyjs="cdn")
except Exception as e:
logger.error(f"Error creating visualization: {e}")
return f"<div style='color: red;'>Visualization error: {str(e)}</div>"
def create_tipm_interface():
"""Create the TIPM Gradio interface"""
interface = TIPMInterface()
with gr.Blocks(
title="TIPM - Tariff Impact Propagation Model",
theme=gr.themes.Soft(),
css="""
.gradio-container {
max-width: 1200px !important;
margin: 0 auto !important;
}
.header {
text-align: center;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 10px;
margin-bottom: 20px;
}
""",
) as demo:
# Header
gr.Markdown(
"""
<div class="header">
<h1>πŸš€ TIPM - Tariff Impact Propagation Model</h1>
<p>Professional Economic Intelligence Platform | Advanced ML Pipeline | 185 Countries</p>
</div>
"""
)
with gr.Row():
with gr.Column(scale=1):
# Country Selection
gr.Markdown("### 🌍 Country Selection")
sort_method = gr.Dropdown(
choices=[
"Alphabetical",
"By Tariff Rate (High to Low)",
"By Tariff Rate (Low to High)",
"By GDP (Largest First)",
],
value="Alphabetical",
label="Sort Countries By",
)
country_dropdown = gr.Dropdown(
choices=[c.display_name for c in interface.countries_data],
value=(
interface.countries_data[0].display_name
if interface.countries_data
else None
),
label="Select Country for Analysis",
)
# Custom Tariff Rate
gr.Markdown("### βš™οΈ Analysis Parameters")
custom_tariff = gr.Number(
value=None,
label="Custom Tariff Rate (%) (Optional)",
info="Override default tariff rate for scenario testing",
)
# Analysis Button
analyze_btn = gr.Button(
"πŸš€ Run TIPM Analysis", variant="primary", size="lg"
)
with gr.Column(scale=2):
# Results Display
gr.Markdown("### πŸ“Š Analysis Results")
results_markdown = gr.Markdown(
"Select a country and click 'Run TIPM Analysis' to begin...",
value="Select a country and click 'Run TIPM Analysis' to begin...",
)
# Visualization
gr.Markdown("### πŸ“ˆ Interactive Visualizations")
viz_html = gr.HTML(
value="<div style='text-align: center; color: #666;'>Analysis results will appear here...</div>"
)
# Status
status = gr.Markdown("Ready for analysis", value="Ready for analysis")
# Event Handlers
def update_country_list(sort_method):
"""Update country list based on sort method"""
sorted_countries = interface.get_sorted_countries(sort_method)
return gr.Dropdown(choices=[c.display_name for c in sorted_countries])
def run_analysis(country_display_name, custom_tariff_rate):
"""Run TIPM analysis for selected country"""
try:
# Extract country name from display name
country_name = (
country_display_name.split(" (")[0]
if country_display_name
else None
)
if not country_name:
return (
"Please select a country",
"<div style='color: red;'>No country selected</div>",
"Error: No country selected",
)
# Run analysis
results = interface.run_tipm_analysis(country_name, custom_tariff_rate)
if "error" in results:
return (
f"Error: {results['error']}",
"<div style='color: red;'>Analysis failed</div>",
f"Error: {results['error']}",
)
# Format results
results_text = f"""
## 🌍 **{results['country_name']} - TIPM Analysis Results**
### πŸ“Š **Economic Profile**
- **GDP**: ${results['gdp_billions']:.1f} billion
- **Income Group**: {results['income_group']}
- **Data Confidence**: {results['data_confidence']}
### πŸ›οΈ **Trade Policy**
- **Tariff Rate**: {results['tariff_rate']:.1f}%
- **Trade Volume**: ${results['trade_volume_millions']:.1f} million
- **Trade Agreements**: {results['trade_agreements']}
### 🌐 **Global Position**
- **Continent**: {results['continent']}
- **Global Groups**: {results['global_groups']}
- **Emerging Market**: {results['emerging_market']}
### 🏭 **Economic Specialization**
- **Tech Manufacturing Rank**: {results['tech_rank']}
- **Resource Category**: {results['resource_category']}
- **Export Capabilities**: {results['export_capabilities']}
- **Strategic Commodities**: {results['strategic_commodities']}
---
*Analysis completed at {results['analysis_timestamp']}*
"""
# Create visualization
viz = interface.create_visualization(results)
return (
results_text,
viz,
f"βœ… Analysis completed successfully for {country_name}",
)
except Exception as e:
error_msg = f"Analysis failed: {str(e)}"
return (
error_msg,
f"<div style='color: red;'>{error_msg}</div>",
error_msg,
)
# Connect event handlers
sort_method.change(
fn=update_country_list, inputs=[sort_method], outputs=[country_dropdown]
)
analyze_btn.click(
fn=run_analysis,
inputs=[country_dropdown, custom_tariff],
outputs=[results_markdown, viz_html, status],
)
# Footer
gr.Markdown(
"""
---
**Tariff Impact Propagation Model** | Professional Economic Intelligence Platform |
Advanced ML Pipeline β€’ 185 Countries β€’ Interactive Analysis |
**Data Sources**: World Bank β€’ US Census β€’ USTR β€’ MSCI β€’ FTSE Russell β€’ OECD β€’ UN Statistics |
**AI Architecture**: 6-layer machine learning pipeline with confidence scoring |
[πŸ“ GitHub Repository](https://github.com/thegeekybeng/TIPM) |
*Professional-grade economic intelligence platform with advanced ML capabilities.*
**Feedback to developer**: [[email protected]](mailto:[email protected])
<div style="text-align: right; font-size: 11px; color: #666; margin-top: 10px;">v1.5 - HF Spaces</div>
"""
)
return demo
# Launch the interface
if __name__ == "__main__":
interface = create_tipm_interface()
interface.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
debug=False,
show_error=True,
quiet=False,
)