Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,12 @@ import numpy as np
|
|
4 |
import pickle
|
5 |
import os
|
6 |
from datetime import datetime, timedelta
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
import
|
|
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
@@ -125,154 +127,43 @@ def predict_next_7_days(model, scaler, data):
|
|
125 |
return predictions_df, last_price[0], total_close_change, total_open_change
|
126 |
|
127 |
def create_prediction_chart(data, predictions_df, last_price):
|
128 |
-
"""Create
|
|
|
|
|
129 |
|
130 |
-
#
|
131 |
recent_data = data.tail(30)
|
|
|
|
|
132 |
|
133 |
-
#
|
134 |
-
|
135 |
-
|
136 |
-
subplot_titles=['Prediksi Harga Emas - 7 Hari Kedepan'],
|
137 |
-
x_title='Tanggal',
|
138 |
-
y_title='Harga (IDR)'
|
139 |
-
)
|
140 |
|
141 |
-
#
|
142 |
-
|
143 |
-
|
144 |
-
x=recent_data.index,
|
145 |
-
y=recent_data['close'],
|
146 |
-
mode='lines',
|
147 |
-
name='Harga Tutup Historis',
|
148 |
-
line=dict(color='#1f77b4', width=2),
|
149 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
150 |
-
'<b>Harga Tutup:</b> IDR %{y:,.0f}<br>' +
|
151 |
-
'<extra></extra>'
|
152 |
-
)
|
153 |
-
)
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
mode='lines',
|
161 |
-
name='Harga Buka Historis',
|
162 |
-
line=dict(color='#2ca02c', width=2),
|
163 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
164 |
-
'<b>Harga Buka:</b> IDR %{y:,.0f}<br>' +
|
165 |
-
'<extra></extra>'
|
166 |
-
)
|
167 |
-
)
|
168 |
|
169 |
-
#
|
170 |
-
|
171 |
-
go.Scatter(
|
172 |
-
x=[recent_data.index[-1]],
|
173 |
-
y=[last_price[0]],
|
174 |
-
mode='markers',
|
175 |
-
name='Harga Tutup Saat Ini',
|
176 |
-
marker=dict(color='red', size=10),
|
177 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
178 |
-
'<b>Harga Tutup Saat Ini:</b> IDR %{y:,.0f}<br>' +
|
179 |
-
'<extra></extra>'
|
180 |
-
)
|
181 |
-
)
|
182 |
|
183 |
-
|
184 |
-
|
185 |
-
x=[recent_data.index[-1]],
|
186 |
-
y=[last_price[1]],
|
187 |
-
mode='markers',
|
188 |
-
name='Harga Buka Saat Ini',
|
189 |
-
marker=dict(color='green', size=10),
|
190 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
191 |
-
'<b>Harga Buka Saat Ini:</b> IDR %{y:,.0f}<br>' +
|
192 |
-
'<extra></extra>'
|
193 |
-
)
|
194 |
-
)
|
195 |
|
196 |
-
#
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
name='Prediksi Harga Tutup',
|
203 |
-
line=dict(color='red', width=2, dash='dash'),
|
204 |
-
marker=dict(size=6),
|
205 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
206 |
-
'<b>Prediksi Harga Tutup:</b> IDR %{y:,.0f}<br>' +
|
207 |
-
'<extra></extra>'
|
208 |
-
)
|
209 |
-
)
|
210 |
|
211 |
-
|
212 |
-
fig.add_trace(
|
213 |
-
go.Scatter(
|
214 |
-
x=predictions_df.index,
|
215 |
-
y=predictions_df['open'],
|
216 |
-
mode='lines+markers',
|
217 |
-
name='Prediksi Harga Buka',
|
218 |
-
line=dict(color='orange', width=2, dash='dash'),
|
219 |
-
marker=dict(size=6, symbol='square'),
|
220 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
221 |
-
'<b>Prediksi Harga Buka:</b> IDR %{y:,.0f}<br>' +
|
222 |
-
'<extra></extra>'
|
223 |
-
)
|
224 |
-
)
|
225 |
-
|
226 |
-
# Update layout
|
227 |
-
fig.update_layout(
|
228 |
-
title={
|
229 |
-
'text': 'Prediksi Harga Emas - 7 Hari Kedepan',
|
230 |
-
'x': 0.5,
|
231 |
-
'xanchor': 'center',
|
232 |
-
'font': {'size': 20, 'family': 'Arial Black'}
|
233 |
-
},
|
234 |
-
xaxis_title='Tanggal',
|
235 |
-
yaxis_title='Harga (IDR)',
|
236 |
-
hovermode='x unified',
|
237 |
-
showlegend=True,
|
238 |
-
legend=dict(
|
239 |
-
orientation="h",
|
240 |
-
yanchor="bottom",
|
241 |
-
y=1.02,
|
242 |
-
xanchor="right",
|
243 |
-
x=1
|
244 |
-
),
|
245 |
-
plot_bgcolor='white',
|
246 |
-
paper_bgcolor='white',
|
247 |
-
font=dict(family="Arial", size=12),
|
248 |
-
height=500,
|
249 |
-
margin=dict(l=50, r=50, t=80, b=50)
|
250 |
-
)
|
251 |
-
|
252 |
-
# Update axes
|
253 |
-
fig.update_xaxes(
|
254 |
-
showgrid=True,
|
255 |
-
gridwidth=1,
|
256 |
-
gridcolor='lightgray',
|
257 |
-
showline=True,
|
258 |
-
linewidth=1,
|
259 |
-
linecolor='black'
|
260 |
-
)
|
261 |
-
|
262 |
-
fig.update_yaxes(
|
263 |
-
showgrid=True,
|
264 |
-
gridwidth=1,
|
265 |
-
gridcolor='lightgray',
|
266 |
-
showline=True,
|
267 |
-
linewidth=1,
|
268 |
-
linecolor='black',
|
269 |
-
tickformat=',.0f'
|
270 |
-
)
|
271 |
-
|
272 |
-
# Convert to HTML
|
273 |
-
html_string = pio.to_html(fig, include_plotlyjs='cdn', div_id="prediction-chart")
|
274 |
-
|
275 |
-
return html_string
|
276 |
|
277 |
@app.route('/')
|
278 |
def index():
|
@@ -306,7 +197,8 @@ def predict():
|
|
306 |
'open_price': round(row['open'], 2),
|
307 |
'close_change': round(row['close_change'], 2),
|
308 |
'open_change': round(row['open_change'], 2)
|
309 |
-
})
|
|
|
310 |
response = {
|
311 |
'success': True,
|
312 |
'current_prices': {
|
@@ -318,7 +210,7 @@ def predict():
|
|
318 |
'close': round(total_close_change, 2),
|
319 |
'open': round(total_open_change, 2)
|
320 |
},
|
321 |
-
'
|
322 |
}
|
323 |
|
324 |
return jsonify(response)
|
@@ -333,85 +225,25 @@ def data_analysis():
|
|
333 |
data = load_data()
|
334 |
if data is None:
|
335 |
return render_template('error.html', error='Data file not found.')
|
336 |
-
# Create historical price chart with Plotly
|
337 |
-
fig = go.Figure()
|
338 |
-
|
339 |
-
# Add close price line
|
340 |
-
fig.add_trace(
|
341 |
-
go.Scatter(
|
342 |
-
x=data.index,
|
343 |
-
y=data['close'],
|
344 |
-
mode='lines',
|
345 |
-
name='Harga Tutup',
|
346 |
-
line=dict(color='#1f77b4', width=1.5),
|
347 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
348 |
-
'<b>Harga Tutup:</b> IDR %{y:,.0f}<br>' +
|
349 |
-
'<extra></extra>'
|
350 |
-
)
|
351 |
-
)
|
352 |
-
|
353 |
-
# Add open price line
|
354 |
-
fig.add_trace(
|
355 |
-
go.Scatter(
|
356 |
-
x=data.index,
|
357 |
-
y=data['open'],
|
358 |
-
mode='lines',
|
359 |
-
name='Harga Buka',
|
360 |
-
line=dict(color='#2ca02c', width=1.5),
|
361 |
-
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
362 |
-
'<b>Harga Buka:</b> IDR %{y:,.0f}<br>' +
|
363 |
-
'<extra></extra>'
|
364 |
-
)
|
365 |
-
)
|
366 |
-
|
367 |
-
# Update layout
|
368 |
-
fig.update_layout(
|
369 |
-
title={
|
370 |
-
'text': 'Data Historis Harga Emas',
|
371 |
-
'x': 0.5,
|
372 |
-
'xanchor': 'center',
|
373 |
-
'font': {'size': 20, 'family': 'Arial Black'}
|
374 |
-
},
|
375 |
-
xaxis_title='Tanggal',
|
376 |
-
yaxis_title='Harga (IDR)',
|
377 |
-
hovermode='x unified',
|
378 |
-
showlegend=True,
|
379 |
-
legend=dict(
|
380 |
-
orientation="h",
|
381 |
-
yanchor="bottom",
|
382 |
-
y=1.02,
|
383 |
-
xanchor="right",
|
384 |
-
x=1
|
385 |
-
),
|
386 |
-
plot_bgcolor='white',
|
387 |
-
paper_bgcolor='white',
|
388 |
-
font=dict(family="Arial", size=12),
|
389 |
-
height=400,
|
390 |
-
margin=dict(l=50, r=50, t=80, b=50)
|
391 |
-
)
|
392 |
-
|
393 |
-
# Update axes
|
394 |
-
fig.update_xaxes(
|
395 |
-
showgrid=True,
|
396 |
-
gridwidth=1,
|
397 |
-
gridcolor='lightgray',
|
398 |
-
showline=True,
|
399 |
-
linewidth=1,
|
400 |
-
linecolor='black'
|
401 |
-
)
|
402 |
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
)
|
|
|
|
|
|
|
412 |
|
413 |
-
|
414 |
-
|
|
|
|
|
|
|
415 |
|
416 |
# Calculate statistics
|
417 |
stats = {
|
@@ -433,4 +265,4 @@ def data_analysis():
|
|
433 |
if __name__ == '__main__':
|
434 |
# For Hugging Face Spaces, use port 7860
|
435 |
port = int(os.environ.get('PORT', 7860))
|
436 |
-
app.run(host='0.0.0.0', port=port, debug=False)
|
|
|
4 |
import pickle
|
5 |
import os
|
6 |
from datetime import datetime, timedelta
|
7 |
+
import matplotlib
|
8 |
+
matplotlib.use('Agg') # Use non-interactive backend
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
import seaborn as sns
|
11 |
+
import base64
|
12 |
+
import io
|
13 |
|
14 |
app = Flask(__name__)
|
15 |
|
|
|
127 |
return predictions_df, last_price[0], total_close_change, total_open_change
|
128 |
|
129 |
def create_prediction_chart(data, predictions_df, last_price):
|
130 |
+
"""Create a chart showing historical and predicted prices"""
|
131 |
+
plt.style.use('default')
|
132 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
133 |
|
134 |
+
# Plot last 30 days of historical data
|
135 |
recent_data = data.tail(30)
|
136 |
+
ax.plot(recent_data.index, recent_data['close'], 'b-', label='Historical Close Price', linewidth=2)
|
137 |
+
ax.plot(recent_data.index, recent_data['open'], 'g-', label='Historical Open Price', linewidth=2)
|
138 |
|
139 |
+
# Add current day point
|
140 |
+
ax.plot(recent_data.index[-1], last_price[0], 'ro', markersize=8, label='Current Close Price')
|
141 |
+
ax.plot(recent_data.index[-1], last_price[1], 'go', markersize=8, label='Current Open Price')
|
|
|
|
|
|
|
|
|
142 |
|
143 |
+
# Plot predictions
|
144 |
+
ax.plot(predictions_df.index, predictions_df['close'], 'r--', label='Predicted Close Price', linewidth=2, marker='o')
|
145 |
+
ax.plot(predictions_df.index, predictions_df['open'], 'orange', linestyle='--', label='Predicted Open Price', linewidth=2, marker='s')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
+
ax.set_title('Gold Price Prediction - Next 7 Days', fontsize=16, fontweight='bold')
|
148 |
+
ax.set_xlabel('Date', fontsize=12)
|
149 |
+
ax.set_ylabel('Price (IDR)', fontsize=12)
|
150 |
+
ax.legend()
|
151 |
+
ax.grid(True, alpha=0.3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
+
# Format y-axis to show prices in millions
|
154 |
+
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f'{x/1000000:.1f}M'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
+
plt.xticks(rotation=45)
|
157 |
+
plt.tight_layout()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
+
# Convert plot to base64 string
|
160 |
+
img_buffer = io.BytesIO()
|
161 |
+
plt.savefig(img_buffer, format='png', dpi=150, bbox_inches='tight')
|
162 |
+
img_buffer.seek(0)
|
163 |
+
img_string = base64.b64encode(img_buffer.read()).decode()
|
164 |
+
plt.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
return img_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
@app.route('/')
|
169 |
def index():
|
|
|
197 |
'open_price': round(row['open'], 2),
|
198 |
'close_change': round(row['close_change'], 2),
|
199 |
'open_change': round(row['open_change'], 2)
|
200 |
+
})
|
201 |
+
|
202 |
response = {
|
203 |
'success': True,
|
204 |
'current_prices': {
|
|
|
210 |
'close': round(total_close_change, 2),
|
211 |
'open': round(total_open_change, 2)
|
212 |
},
|
213 |
+
'chart': chart_img
|
214 |
}
|
215 |
|
216 |
return jsonify(response)
|
|
|
225 |
data = load_data()
|
226 |
if data is None:
|
227 |
return render_template('error.html', error='Data file not found.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
|
229 |
+
# Create historical price chart
|
230 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
231 |
+
ax.plot(data.index, data['close'], 'b-', label='Close Price', linewidth=1.5)
|
232 |
+
ax.plot(data.index, data['open'], 'g-', label='Open Price', linewidth=1.5)
|
233 |
+
ax.set_title('Historical Gold Prices', fontsize=16, fontweight='bold')
|
234 |
+
ax.set_xlabel('Date', fontsize=12)
|
235 |
+
ax.set_ylabel('Price (IDR)', fontsize=12)
|
236 |
+
ax.legend()
|
237 |
+
ax.grid(True, alpha=0.3)
|
238 |
+
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: f'{x/1000000:.1f}M'))
|
239 |
+
plt.xticks(rotation=45)
|
240 |
+
plt.tight_layout()
|
241 |
|
242 |
+
img_buffer = io.BytesIO()
|
243 |
+
plt.savefig(img_buffer, format='png', dpi=150, bbox_inches='tight')
|
244 |
+
img_buffer.seek(0)
|
245 |
+
historical_chart = base64.b64encode(img_buffer.read()).decode()
|
246 |
+
plt.close()
|
247 |
|
248 |
# Calculate statistics
|
249 |
stats = {
|
|
|
265 |
if __name__ == '__main__':
|
266 |
# For Hugging Face Spaces, use port 7860
|
267 |
port = int(os.environ.get('PORT', 7860))
|
268 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|