Spaces:
Sleeping
Sleeping
| # app.py | |
| import pandas as pd | |
| import numpy as np | |
| import streamlit as st | |
| from xgboost import XGBRegressor | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import r2_score | |
| from io import BytesIO | |
| # ------------------------- | |
| # ๐ Load Dataset | |
| # ------------------------- | |
| def load_data(): | |
| df = pd.read_csv("Nigeria Economy Dataset_1990-2022.csv") | |
| return df | |
| df = load_data() | |
| # ------------------------- | |
| # โ๏ธ Model Training | |
| # ------------------------- | |
| features = ['Agriculture to GDP', 'Industry to GDP', 'Services to GDP', 'Inflation rate', 'Government debt'] | |
| target = 'Real GDP' | |
| X = df[features] | |
| y = df[target] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| model = XGBRegressor() | |
| model.fit(X_train, y_train) | |
| y_pred = model.predict(X_test) | |
| score = r2_score(y_test, y_pred) | |
| # ------------------------- | |
| # ๐จ Streamlit UI | |
| # ------------------------- | |
| st.set_page_config(page_title="Nigeria GDP Predictor", layout="wide") | |
| st.title("๐ณ๐ฌ Nigeria GDP Forecasting App") | |
| st.markdown("Use the controls to simulate policy changes and forecast **GDP** using AI.") | |
| st.sidebar.header("๐ Economic Inputs") | |
| agri = st.sidebar.slider("Agriculture Contribution (%)", 0.0, 100.0, 25.0) | |
| indus = st.sidebar.slider("Industry Contribution (%)", 0.0, 100.0, 30.0) | |
| service = st.sidebar.slider("Services Contribution (%)", 0.0, 100.0, 40.0) | |
| inflation = st.sidebar.slider("Inflation Rate (%)", 0.0, 100.0, 15.0) | |
| debt = st.sidebar.slider("Govt Debt (Billion USD)", 0.0, 200.0, 60.0) | |
| # ------------------------- | |
| # ๐งฎ Make Prediction | |
| # ------------------------- | |
| input_data = pd.DataFrame([[agri, indus, service, inflation, debt]], columns=features) | |
| gdp_prediction = model.predict(input_data)[0] | |
| st.subheader("๐ง Predicted GDP Result") | |
| st.metric(label="Predicted GDP", value=f"${gdp_prediction:,.2f} Billion USD") | |
| st.caption(f"Model Accuracy (Rยฒ): {score:.4f}") | |
| # ------------------------- | |
| # ๐ GDP Trend Line Chart | |
| # ------------------------- | |
| st.subheader("๐ Historical GDP Trend (Nigeria)") | |
| gdp_df = df[['Year', 'Real GDP']].dropna() | |
| gdp_df = gdp_df.sort_values("Year") | |
| st.line_chart(gdp_df.set_index("Year")) | |
| # ------------------------- | |
| # ๐ฅ Download Prediction | |
| # ------------------------- | |
| def convert_df_to_csv(data): | |
| return data.to_csv(index=False).encode('utf-8') | |
| st.subheader("โฌ๏ธ Export Prediction") | |
| output_df = input_data.copy() | |
| output_df['Predicted_GDP'] = gdp_prediction | |
| csv_data = convert_df_to_csv(output_df) | |
| st.download_button( | |
| label="Download Prediction as CSV", | |
| data=csv_data, | |
| file_name="gdp_prediction.csv", | |
| mime='text/csv' | |
| ) | |
| # ------------------------- | |
| # ๐ Gradio Integration (optional) | |
| # ------------------------- | |
| st.subheader("๐ค Gradio AI Simulator (Embed)") | |
| st.markdown( | |
| """ | |
| <iframe src="https://your-huggingface-username.hf.space" width="100%" height="600" frameborder="0"></iframe> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |