Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,10 @@
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import streamlit as st
|
| 6 |
-
import seaborn as sns
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
from xgboost import XGBRegressor
|
| 9 |
from sklearn.model_selection import train_test_split
|
| 10 |
from sklearn.metrics import r2_score
|
| 11 |
from io import BytesIO
|
| 12 |
-
import json
|
| 13 |
|
| 14 |
# -------------------------
|
| 15 |
# π Load Dataset
|
|
@@ -62,74 +59,35 @@ st.metric(label="Predicted GDP", value=f"${gdp_prediction:,.2f} Billion USD")
|
|
| 62 |
st.caption(f"Model Accuracy (RΒ²): {score:.4f}")
|
| 63 |
|
| 64 |
# -------------------------
|
| 65 |
-
# π GDP Trend Chart
|
| 66 |
# -------------------------
|
| 67 |
st.subheader("π Historical GDP Trend (Nigeria)")
|
| 68 |
-
gdp_df = df[['Year', 'Real GDP']].dropna()
|
|
|
|
| 69 |
st.line_chart(gdp_df.set_index("Year"))
|
| 70 |
|
| 71 |
# -------------------------
|
| 72 |
-
#
|
| 73 |
# -------------------------
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
cols = st.columns(len(features))
|
| 77 |
-
for i, feat in enumerate(features):
|
| 78 |
-
with cols[i]:
|
| 79 |
-
fig, ax = plt.subplots()
|
| 80 |
-
sns.scatterplot(x=df[feat], y=df['Real GDP'], ax=ax)
|
| 81 |
-
ax.set_title(f"GDP vs {feat}")
|
| 82 |
-
st.pyplot(fig)
|
| 83 |
-
|
| 84 |
-
# -------------------------
|
| 85 |
-
# π₯ Correlation Heatmap
|
| 86 |
-
# -------------------------
|
| 87 |
-
st.subheader("π Correlation Heatmap")
|
| 88 |
-
corr = df[features + ['Real GDP']].corr()
|
| 89 |
-
fig, ax = plt.subplots()
|
| 90 |
-
sns.heatmap(corr, annot=True, cmap="coolwarm", fmt=".2f", ax=ax)
|
| 91 |
-
st.pyplot(fig)
|
| 92 |
-
|
| 93 |
-
# -------------------------
|
| 94 |
-
# π₯ Export Prediction
|
| 95 |
-
# -------------------------
|
| 96 |
-
st.subheader("β¬οΈ Export Predicted Data")
|
| 97 |
|
|
|
|
| 98 |
output_df = input_data.copy()
|
| 99 |
output_df['Predicted_GDP'] = gdp_prediction
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
| 108 |
-
output_df.to_excel(writer, index=False, sheet_name='Prediction')
|
| 109 |
-
st.download_button("Download as Excel", excel_buffer.getvalue(), "gdp_prediction.xlsx", "application/vnd.ms-excel")
|
| 110 |
-
|
| 111 |
-
# JSON
|
| 112 |
-
json_data = output_df.to_json(orient='records')
|
| 113 |
-
st.download_button("Download as JSON", json_data, "gdp_prediction.json", "application/json")
|
| 114 |
|
| 115 |
# -------------------------
|
| 116 |
-
# π Gradio Integration
|
| 117 |
# -------------------------
|
| 118 |
-
st.subheader("π€ Gradio
|
| 119 |
-
st.markdown(
|
| 120 |
-
"""
|
| 121 |
-
You can also create a separate Gradio interface and host it on [Hugging Face Spaces](https://huggingface.co/spaces).
|
| 122 |
-
|
| 123 |
-
Once deployed, embed your app here:
|
| 124 |
-
|
| 125 |
-
```python
|
| 126 |
-
<iframe src="https://your-huggingface-username.hf.space" width="100%" height="600" frameborder="0"></iframe>
|
| 127 |
-
```
|
| 128 |
-
|
| 129 |
-
Replace the `src` with your actual Hugging Face space link.
|
| 130 |
-
"""
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
st.markdown(
|
| 134 |
"""
|
| 135 |
<iframe src="https://your-huggingface-username.hf.space" width="100%" height="600" frameborder="0"></iframe>
|
|
|
|
| 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
|
|
|
|
| 59 |
st.caption(f"Model Accuracy (RΒ²): {score:.4f}")
|
| 60 |
|
| 61 |
# -------------------------
|
| 62 |
+
# π GDP Trend Line Chart
|
| 63 |
# -------------------------
|
| 64 |
st.subheader("π Historical GDP Trend (Nigeria)")
|
| 65 |
+
gdp_df = df[['Year', 'Real GDP']].dropna()
|
| 66 |
+
gdp_df = gdp_df.sort_values("Year")
|
| 67 |
st.line_chart(gdp_df.set_index("Year"))
|
| 68 |
|
| 69 |
# -------------------------
|
| 70 |
+
# π₯ Download Prediction
|
| 71 |
# -------------------------
|
| 72 |
+
def convert_df_to_csv(data):
|
| 73 |
+
return data.to_csv(index=False).encode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
st.subheader("β¬οΈ Export Prediction")
|
| 76 |
output_df = input_data.copy()
|
| 77 |
output_df['Predicted_GDP'] = gdp_prediction
|
| 78 |
+
csv_data = convert_df_to_csv(output_df)
|
| 79 |
|
| 80 |
+
st.download_button(
|
| 81 |
+
label="Download Prediction as CSV",
|
| 82 |
+
data=csv_data,
|
| 83 |
+
file_name="gdp_prediction.csv",
|
| 84 |
+
mime='text/csv'
|
| 85 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# -------------------------
|
| 88 |
+
# π Gradio Integration (optional)
|
| 89 |
# -------------------------
|
| 90 |
+
st.subheader("π€ Gradio AI Simulator (Embed)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
st.markdown(
|
| 92 |
"""
|
| 93 |
<iframe src="https://your-huggingface-username.hf.space" width="100%" height="600" frameborder="0"></iframe>
|