Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,97 @@
|
|
|
|
|
| 1 |
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from xgboost import XGBRegressor
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.metrics import r2_score
|
| 9 |
+
from io import BytesIO
|
| 10 |
|
| 11 |
+
# -------------------------
|
| 12 |
+
# ๐ Load Dataset
|
| 13 |
+
# -------------------------
|
| 14 |
+
@st.cache_data
|
| 15 |
+
def load_data():
|
| 16 |
+
url = "https://raw.githubusercontent.com/yourusername/nigeria-economy-ai/main/Nigeria_Economy_Data.csv"
|
| 17 |
+
df = pd.read_csv(url)
|
| 18 |
+
return df
|
| 19 |
|
| 20 |
+
df = load_data()
|
| 21 |
|
| 22 |
+
# -------------------------
|
| 23 |
+
# โ๏ธ Model Training
|
| 24 |
+
# -------------------------
|
| 25 |
+
features = ['Agriculture_Contribution', 'Industry_Contribution', 'Services_Contribution', 'Inflation_Rate', 'Govt_Debt']
|
| 26 |
+
target = 'GDP_Billion_USD'
|
| 27 |
+
|
| 28 |
+
X = df[features]
|
| 29 |
+
y = df[target]
|
| 30 |
+
|
| 31 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 32 |
+
|
| 33 |
+
model = XGBRegressor()
|
| 34 |
+
model.fit(X_train, y_train)
|
| 35 |
+
y_pred = model.predict(X_test)
|
| 36 |
+
score = r2_score(y_test, y_pred)
|
| 37 |
+
|
| 38 |
+
# -------------------------
|
| 39 |
+
# ๐จ Streamlit UI
|
| 40 |
+
# -------------------------
|
| 41 |
+
st.set_page_config(page_title="Nigeria GDP Predictor", layout="wide")
|
| 42 |
+
st.title("๐ณ๐ฌ Nigeria GDP Forecasting App")
|
| 43 |
+
st.markdown("Use the controls to simulate policy changes and forecast **GDP** using AI.")
|
| 44 |
+
|
| 45 |
+
st.sidebar.header("๐ Economic Inputs")
|
| 46 |
+
agri = st.sidebar.slider("Agriculture Contribution (%)", 0.0, 100.0, 25.0)
|
| 47 |
+
indus = st.sidebar.slider("Industry Contribution (%)", 0.0, 100.0, 30.0)
|
| 48 |
+
service = st.sidebar.slider("Services Contribution (%)", 0.0, 100.0, 40.0)
|
| 49 |
+
inflation = st.sidebar.slider("Inflation Rate (%)", 0.0, 100.0, 15.0)
|
| 50 |
+
debt = st.sidebar.slider("Govt Debt (Billion USD)", 0.0, 200.0, 60.0)
|
| 51 |
+
|
| 52 |
+
# -------------------------
|
| 53 |
+
# ๐งฎ Make Prediction
|
| 54 |
+
# -------------------------
|
| 55 |
+
input_data = pd.DataFrame([[agri, indus, service, inflation, debt]], columns=features)
|
| 56 |
+
gdp_prediction = model.predict(input_data)[0]
|
| 57 |
+
|
| 58 |
+
st.subheader("๐ง Predicted GDP Result")
|
| 59 |
+
st.metric(label="Predicted GDP", value=f"${gdp_prediction:,.2f} Billion USD")
|
| 60 |
+
st.caption(f"Model Accuracy (Rยฒ): {score:.4f}")
|
| 61 |
+
|
| 62 |
+
# -------------------------
|
| 63 |
+
# ๐ GDP Trend Line Chart
|
| 64 |
+
# -------------------------
|
| 65 |
+
st.subheader("๐ Historical GDP Trend (Nigeria)")
|
| 66 |
+
gdp_df = df[['Year', 'GDP_Billion_USD']].dropna()
|
| 67 |
+
gdp_df = gdp_df.sort_values("Year")
|
| 68 |
+
st.line_chart(gdp_df.set_index("Year"))
|
| 69 |
+
|
| 70 |
+
# -------------------------
|
| 71 |
+
# ๐ฅ Download Prediction
|
| 72 |
+
# -------------------------
|
| 73 |
+
def convert_df_to_csv(data):
|
| 74 |
+
return data.to_csv(index=False).encode('utf-8')
|
| 75 |
+
|
| 76 |
+
st.subheader("โฌ๏ธ Export Prediction")
|
| 77 |
+
output_df = input_data.copy()
|
| 78 |
+
output_df['Predicted_GDP'] = gdp_prediction
|
| 79 |
+
csv_data = convert_df_to_csv(output_df)
|
| 80 |
+
|
| 81 |
+
st.download_button(
|
| 82 |
+
label="Download Prediction as CSV",
|
| 83 |
+
data=csv_data,
|
| 84 |
+
file_name="gdp_prediction.csv",
|
| 85 |
+
mime='text/csv'
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# -------------------------
|
| 89 |
+
# ๐ Gradio Integration (optional)
|
| 90 |
+
# -------------------------
|
| 91 |
+
st.subheader("๐ค Gradio AI Simulator (Embed)")
|
| 92 |
+
st.markdown(
|
| 93 |
+
"""
|
| 94 |
+
<iframe src="https://your-huggingface-username.hf.space" width="100%" height="600" frameborder="0"></iframe>
|
| 95 |
+
""",
|
| 96 |
+
unsafe_allow_html=True
|
| 97 |
+
)
|