Frederick001 commited on
Commit
cf0054c
Β·
verified Β·
1 Parent(s): ff6e105

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -16
app.py CHANGED
@@ -3,10 +3,13 @@
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,35 +62,74 @@ st.metric(label="Predicted GDP", value=f"${gdp_prediction:,.2f} Billion USD")
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>
 
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
  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().sort_values("Year")
 
69
  st.line_chart(gdp_df.set_index("Year"))
70
 
71
  # -------------------------
72
+ # 🧾 Extra Visualizations
73
  # -------------------------
74
+ st.subheader("πŸ“Š GDP vs Economic Indicators")
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
+ # CSV
102
+ csv_data = output_df.to_csv(index=False).encode('utf-8')
103
+ st.download_button("Download as CSV", csv_data, "gdp_prediction.csv", "text/csv")
104
+
105
+ # Excel
106
+ excel_buffer = BytesIO()
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 Integration (Hugging Face)")
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>