Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,9 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 5 |
from data_processor import DataProcessor
|
| 6 |
from sentiment_analyzer import SentimentAnalyzer
|
| 7 |
from model_handler import ModelHandler
|
|
@@ -69,6 +72,7 @@ def update_analysis(interval):
|
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
error_msg = f"Error: {str(e)}"
|
|
|
|
| 72 |
return [gr.update(value=error_msg)] * 7
|
| 73 |
|
| 74 |
def create_candlestick_chart(df, interval):
|
|
@@ -199,8 +203,109 @@ def create_sentiment_gauge(score):
|
|
| 199 |
|
| 200 |
return fig
|
| 201 |
|
|
|
|
| 202 |
def create_fundamentals_gauge(fundamentals):
|
| 203 |
"""Create fundamentals gauge"""
|
| 204 |
value = fundamentals.get('Gold Strength Index', 50)
|
| 205 |
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import numpy as np
|
| 5 |
+
|
| 6 |
+
# ASUMSI: Komponen-komponen berikut sudah ada dan berfungsi
|
| 7 |
+
# dari file data_processor.py, sentiment_analyzer.py, model_handler.py, dan trading_logic.py
|
| 8 |
from data_processor import DataProcessor
|
| 9 |
from sentiment_analyzer import SentimentAnalyzer
|
| 10 |
from model_handler import ModelHandler
|
|
|
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
error_msg = f"Error: {str(e)}"
|
| 75 |
+
# Mengembalikan nilai yang dapat di-update untuk semua 7 output
|
| 76 |
return [gr.update(value=error_msg)] * 7
|
| 77 |
|
| 78 |
def create_candlestick_chart(df, interval):
|
|
|
|
| 203 |
|
| 204 |
return fig
|
| 205 |
|
| 206 |
+
# --- FUNGSI YANG DIPERBAIKI ---
|
| 207 |
def create_fundamentals_gauge(fundamentals):
|
| 208 |
"""Create fundamentals gauge"""
|
| 209 |
value = fundamentals.get('Gold Strength Index', 50)
|
| 210 |
|
| 211 |
+
# PERBAIKAN: Melengkapi go.Figure(go.Indicator(...)) dan menutup tanda kurung
|
| 212 |
+
fig = go.Figure(go.Indicator(
|
| 213 |
+
mode="gauge+number",
|
| 214 |
+
value=value,
|
| 215 |
+
domain={'x': [0, 1], 'y': [0, 1]},
|
| 216 |
+
title={'text': "Fundamentals Strength"},
|
| 217 |
+
gauge={
|
| 218 |
+
'axis': {'range': [0, 100]},
|
| 219 |
+
'bar': {'color': "#FFD700"},
|
| 220 |
+
'steps': [
|
| 221 |
+
{'range': [0, 30], 'color': "rgba(255,0,0,0.5)"},
|
| 222 |
+
{'range': [30, 70], 'color': "rgba(255,255,255,0.3)"},
|
| 223 |
+
{'range': [70, 100], 'color': "rgba(0,255,0,0.5)"}
|
| 224 |
+
]
|
| 225 |
+
}
|
| 226 |
+
))
|
| 227 |
+
|
| 228 |
+
fig.update_layout(
|
| 229 |
+
template='plotly_dark',
|
| 230 |
+
height=300,
|
| 231 |
+
paper_bgcolor='black',
|
| 232 |
+
plot_bgcolor='black',
|
| 233 |
+
font=dict(color='white')
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
return fig
|
| 237 |
+
# --- AKHIR FUNGSI YANG DIPERBAIKI ---
|
| 238 |
+
|
| 239 |
+
# --- Gradio UI definition (Tambahan untuk kode lengkap) ---
|
| 240 |
+
|
| 241 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 242 |
+
gr.Markdown("<h1><center>📈 Gold Trading Dashboard</center></h1>")
|
| 243 |
+
|
| 244 |
+
with gr.Row():
|
| 245 |
+
interval_selector = gr.Dropdown(
|
| 246 |
+
['1h', '4h', '1d'],
|
| 247 |
+
label="Data Interval",
|
| 248 |
+
value='1d',
|
| 249 |
+
interactive=True
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
with gr.Row():
|
| 253 |
+
chart_output = gr.Plot(label="Candlestick Chart", scale=3)
|
| 254 |
+
|
| 255 |
+
with gr.Column(scale=1):
|
| 256 |
+
metrics_output = gr.Dataframe(
|
| 257 |
+
headers=["Metric", "Value"],
|
| 258 |
+
row_count=(8, "fixed"),
|
| 259 |
+
col_count=(2, "fixed"),
|
| 260 |
+
label="Key Metrics",
|
| 261 |
+
interactive=False
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
fundamentals_fig_output = gr.Plot(label="Fundamentals Gauge")
|
| 265 |
+
fundamentals_df_output = gr.Dataframe(
|
| 266 |
+
headers=["Metric", "Value"],
|
| 267 |
+
label="Fundamental Data",
|
| 268 |
+
interactive=False,
|
| 269 |
+
visible=False
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
with gr.Row():
|
| 273 |
+
pred_output = gr.Plot(label="Price Prediction")
|
| 274 |
+
sentiment_fig_output = gr.Plot(label="Market Sentiment Gauge")
|
| 275 |
+
|
| 276 |
+
news_output = gr.HTML(label="Latest Sentiment News")
|
| 277 |
+
|
| 278 |
+
interval_selector.change(
|
| 279 |
+
fn=update_analysis,
|
| 280 |
+
inputs=[interval_selector],
|
| 281 |
+
outputs=[
|
| 282 |
+
chart_output,
|
| 283 |
+
metrics_output,
|
| 284 |
+
pred_output,
|
| 285 |
+
sentiment_fig_output,
|
| 286 |
+
news_output,
|
| 287 |
+
fundamentals_fig_output,
|
| 288 |
+
fundamentals_df_output
|
| 289 |
+
]
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
# Initial load
|
| 293 |
+
demo.load(
|
| 294 |
+
fn=update_analysis,
|
| 295 |
+
inputs=[interval_selector],
|
| 296 |
+
outputs=[
|
| 297 |
+
chart_output,
|
| 298 |
+
metrics_output,
|
| 299 |
+
pred_output,
|
| 300 |
+
sentiment_fig_output,
|
| 301 |
+
news_output,
|
| 302 |
+
fundamentals_fig_output,
|
| 303 |
+
fundamentals_df_output
|
| 304 |
+
]
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
if __name__ == "__main__":
|
| 308 |
+
# Catatan: Anda perlu memastikan file data_processor.py, sentiment_analyzer.py,
|
| 309 |
+
# model_handler.py, dan trading_logic.py tersedia di direktori yang sama
|
| 310 |
+
# dan memiliki implementasi kelas yang diperlukan.
|
| 311 |
+
demo.launch()
|