Spaces:
Sleeping
Sleeping
SeaLevel Predictor
Browse files- app.py +129 -0
- requirements.txt +5 -3
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py (Versi贸n 2.0 - Biling眉e)
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from scipy.stats import linregress
|
| 7 |
+
from datetime import date
|
| 8 |
+
|
| 9 |
+
# --- 1. DICCIONARIO DE TEXTOS (ESPA脩OL E INGL脡S) ---
|
| 10 |
+
TEXTS = {
|
| 11 |
+
'es': {
|
| 12 |
+
'page_title': "Predicci贸n del Nivel del Mar", 'page_icon': "馃寠",
|
| 13 |
+
'title': "Simulador de Aumento del Nivel del Mar 馃寠",
|
| 14 |
+
'description': """
|
| 15 |
+
Esta herramienta visualiza datos hist贸ricos del aumento del nivel del mar y proyecta las tendencias a futuro.
|
| 16 |
+
Se muestran dos modelos:
|
| 17 |
+
1. **Tendencia a Largo Plazo:** Basada en datos desde 1880.
|
| 18 |
+
2. **Tendencia Acelerada:** Basada en datos m谩s recientes (desde el a帽o 2000), que reflejan un ritmo de aumento m谩s r谩pido.
|
| 19 |
+
""",
|
| 20 |
+
'subheader_viz': "Visualizaci贸n Hist贸rica y Proyecciones",
|
| 21 |
+
'plot_title': "Aumento del Nivel del Mar: Comparaci贸n de Tendencias",
|
| 22 |
+
'plot_xlabel': "A帽o", 'plot_ylabel': "Nivel del Mar Ajustado (en pulgadas)",
|
| 23 |
+
'legend_hist': "Datos Hist贸ricos", 'legend_full': "Predicci贸n (Tendencia 1880-2014)",
|
| 24 |
+
'legend_recent': "Predicci贸n (Tendencia Acelerada 2000-2014)",
|
| 25 |
+
'subheader_sim': "Viaja al Futuro: Simulador Interactivo",
|
| 26 |
+
'slider_label': "Selecciona un a帽o futuro para ver la predicci贸n:",
|
| 27 |
+
'prediction_header': "Predicciones para el a帽o",
|
| 28 |
+
'metric1_label': "Predicci贸n (Tendencia Hist贸rica)",
|
| 29 |
+
'metric2_label': "Predicci贸n (Tendencia Acelerada)",
|
| 30 |
+
'unit_inches': "pulgadas",
|
| 31 |
+
'metric_delta_text': "m谩s alto",
|
| 32 |
+
'analysis_warning': "**An谩lisis:** Basado en la tendencia de las 煤ltimas d茅cadas, el nivel del mar en"
|
| 33 |
+
},
|
| 34 |
+
'en': {
|
| 35 |
+
'page_title': "Sea Level Predictor", 'page_icon': "馃寠",
|
| 36 |
+
'title': "Sea Level Rise Simulator 馃寠",
|
| 37 |
+
'description': """
|
| 38 |
+
This tool visualizes historical sea level rise data and projects future trends.
|
| 39 |
+
Two prediction models are shown:
|
| 40 |
+
1. **Long-Term Trend:** Based on all data since 1880.
|
| 41 |
+
2. **Accelerated Trend:** Based on more recent data (since the year 2000), reflecting a faster rate of increase.
|
| 42 |
+
""",
|
| 43 |
+
'subheader_viz': "Historical Visualization and Projections",
|
| 44 |
+
'plot_title': "Sea Level Rise: Comparing Trends",
|
| 45 |
+
'plot_xlabel': "Year", 'plot_ylabel': "CSIRO Adjusted Sea Level (inches)",
|
| 46 |
+
'legend_hist': "Historical Data", 'legend_full': "Prediction (1880-2014 Trend)",
|
| 47 |
+
'legend_recent': "Prediction (Accelerated 2000-2014 Trend)",
|
| 48 |
+
'subheader_sim': "Travel to the Future: Interactive Simulator",
|
| 49 |
+
'slider_label': "Select a future year to see the prediction:",
|
| 50 |
+
'prediction_header': "Predictions for the year",
|
| 51 |
+
'metric1_label': "Prediction (Historical Trend)",
|
| 52 |
+
'metric2_label': "Prediction (Accelerated Trend)",
|
| 53 |
+
'unit_inches': "inches",
|
| 54 |
+
'metric_delta_text': "higher",
|
| 55 |
+
'analysis_warning': "**Analysis:** Based on the trend of recent decades, the sea level in"
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# --- L脫GICA DE LA APP ---
|
| 60 |
+
if 'lang' not in st.session_state:
|
| 61 |
+
st.session_state.lang = 'en'
|
| 62 |
+
def toggle_language():
|
| 63 |
+
st.session_state.lang = 'es' if st.session_state.lang == 'en' else 'en'
|
| 64 |
+
|
| 65 |
+
# La llamada a st.set_page_config() debe ocurrir antes de que se use st.session_state para obtener los textos
|
| 66 |
+
st.set_page_config(
|
| 67 |
+
page_title=TEXTS[st.session_state.get('lang', 'en')]['page_title'],
|
| 68 |
+
page_icon=TEXTS[st.session_state.get('lang', 'en')]['page_icon'],
|
| 69 |
+
layout="wide"
|
| 70 |
+
)
|
| 71 |
+
texts = TEXTS[st.session_state.lang]
|
| 72 |
+
|
| 73 |
+
@st.cache_data
|
| 74 |
+
def load_and_process_data():
|
| 75 |
+
url = 'https://raw.githubusercontent.com/freeCodeCamp/boilerplate-sea-level-predictor/master/epa-sea-level.csv'
|
| 76 |
+
df = pd.read_csv(url)
|
| 77 |
+
res_full = linregress(df['Year'], df['CSIRO Adjusted Sea Level'])
|
| 78 |
+
df_recent = df[df['Year'] >= 2000]
|
| 79 |
+
res_recent = linregress(df_recent['Year'], df_recent['CSIRO Adjusted Sea Level'])
|
| 80 |
+
return df, res_full, res_recent
|
| 81 |
+
|
| 82 |
+
df_sea, model_full, model_recent = load_and_process_data()
|
| 83 |
+
|
| 84 |
+
# --- INTERFAZ ---
|
| 85 |
+
st.button('Espa帽ol / English', on_click=toggle_language)
|
| 86 |
+
st.title(texts['title'])
|
| 87 |
+
st.markdown(texts['description'])
|
| 88 |
+
|
| 89 |
+
st.subheader(texts['subheader_viz'])
|
| 90 |
+
years_full_extended = pd.Series(range(1880, 2101))
|
| 91 |
+
line_full = model_full.intercept + model_full.slope * years_full_extended
|
| 92 |
+
years_recent_extended = pd.Series(range(2000, 2101))
|
| 93 |
+
line_recent = model_recent.intercept + model_recent.slope * years_recent_extended
|
| 94 |
+
|
| 95 |
+
fig, ax = plt.subplots(figsize=(16, 9))
|
| 96 |
+
ax.scatter(df_sea['Year'], df_sea['CSIRO Adjusted Sea Level'], label=texts['legend_hist'], alpha=0.7)
|
| 97 |
+
ax.plot(years_full_extended, line_full, 'r', label=texts['legend_full'], linestyle='--')
|
| 98 |
+
ax.plot(years_recent_extended, line_recent, 'orange', label=texts['legend_recent'], linewidth=3)
|
| 99 |
+
ax.set_title(texts['plot_title'], fontsize=18)
|
| 100 |
+
ax.set_xlabel(texts['plot_xlabel'], fontsize=14)
|
| 101 |
+
ax.set_ylabel(texts['plot_ylabel'], fontsize=14)
|
| 102 |
+
ax.legend(fontsize=12)
|
| 103 |
+
ax.grid(True)
|
| 104 |
+
st.pyplot(fig)
|
| 105 |
+
|
| 106 |
+
st.subheader(texts['subheader_sim'])
|
| 107 |
+
future_year = st.slider(
|
| 108 |
+
texts['slider_label'],
|
| 109 |
+
min_value=date.today().year, max_value=2100, value=2050, step=1
|
| 110 |
+
)
|
| 111 |
+
prediction_full = model_full.intercept + model_full.slope * future_year
|
| 112 |
+
prediction_recent = model_recent.intercept + model_recent.slope * future_year
|
| 113 |
+
difference = prediction_recent - prediction_full
|
| 114 |
+
|
| 115 |
+
st.write(f"#### {texts['prediction_header']} {future_year}:")
|
| 116 |
+
col1, col2 = st.columns(2)
|
| 117 |
+
with col1:
|
| 118 |
+
st.metric(
|
| 119 |
+
label=texts['metric1_label'],
|
| 120 |
+
value=f"{prediction_full:.2f} {texts['unit_inches']}"
|
| 121 |
+
)
|
| 122 |
+
with col2:
|
| 123 |
+
st.metric(
|
| 124 |
+
label=texts['metric2_label'],
|
| 125 |
+
value=f"{prediction_recent:.2f} {texts['unit_inches']}",
|
| 126 |
+
delta=f"{difference:.2f} {texts['unit_inches']} {texts['metric_delta_text']}",
|
| 127 |
+
delta_color="inverse"
|
| 128 |
+
)
|
| 129 |
+
st.warning(f"{texts['analysis_warning']} **{future_year}** podr铆a ser **{difference:.2f} {texts['unit_inches']} m谩s alto** de lo que predecir铆a el modelo a largo plazo.")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
pandas
|
| 3 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
matplotlib
|
| 4 |
+
seaborn
|
| 5 |
+
scipy
|