omniverse1 commited on
Commit
6d4f83e
·
verified ·
1 Parent(s): c699178

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -56
app.py CHANGED
@@ -9,6 +9,7 @@ from model_handler import ModelHandler
9
  from trading_logic import TradingLogic
10
  import io
11
  import base64
 
12
 
13
  # Global instances
14
  data_processor = DataProcessor()
@@ -164,62 +165,45 @@ def analyze_sentiment(asset_name):
164
  """Analyze market sentiment for selected asset"""
165
  try:
166
  ticker = asset_map[asset_name]
167
- sentiment_score, news_summary = sentiment_analyzer.analyze_sentiment(ticker)
168
-
169
- # --- Modifikasi untuk tampilan Gauge yang lebih baik ---
170
-
171
- # Create sentiment gauge using matplotlib
172
- fig, ax = plt.subplots(figsize=(6, 4), facecolor='white')
173
- fig.patch.set_facecolor('white')
174
-
175
- # Setup Gauge limits
176
- ax.set_xlim(-1.1, 1.1) # Diperluas sedikit untuk label
177
- ax.set_ylim(-0.2, 1.1) # Diperluas sedikit ke bawah untuk skor
178
- ax.set_aspect('equal')
179
-
180
- # Draw gauge background segments
181
- theta = np.linspace(np.pi, 0, 100)
182
- x, y = np.cos(theta), np.sin(theta)
183
-
184
- # Segments: Red (-1.0 to -0.5), Gray/Yellow (-0.5 to 0.5), Green (0.5 to 1.0)
185
- ax.fill_between(x[75:], y[75:], 0, where=x[75:]<=-0.5, color='#F08080', alpha=0.9, linewidth=0) # Red (Bearish)
186
- ax.fill_between(x[25:75], y[25:75], 0, color='#D3D3D3', alpha=0.9, linewidth=0) # Gray (Neutral)
187
- ax.fill_between(x[:25], y[:25], 0, where=x[:25]>=0.5, color='#90EE90', alpha=0.9, linewidth=0) # Green (Bullish)
188
-
189
- # Draw main arc line
190
- ax.plot(x, y, color='#D3D3D3', linewidth=10, solid_capstyle='round', zorder=1)
191
-
192
- # Draw needle (using a small triangle/arrow)
193
- needle_angle = np.pi * (1 - (sentiment_score + 1) / 2) # Angle from pi (180 deg) to 0 (0 deg)
194
- needle_x = 0.8 * np.cos(needle_angle)
195
- needle_y = 0.8 * np.sin(needle_angle)
196
-
197
- # Draw a line/needle for the pointer
198
- ax.plot([0, needle_x], [0, needle_y], color='black', linewidth=3, zorder=3)
199
- ax.plot([0], [0], marker='o', color='black', markersize=6, zorder=4)
200
-
201
- # Draw score number (similar to the image)
202
- score_color = 'red' if sentiment_score < 0 else 'green' if sentiment_score > 0 else 'black'
203
- ax.text(0, -0.15, f"{sentiment_score:+.3f}", ha='center', va='center',
204
- fontsize=24, color=score_color, weight='bold', zorder=5)
205
-
206
- # Add labels for the scale
207
- ax.text(-1, 0, "-1", ha='center', va='top', fontsize=10, color='black')
208
- ax.text(-0.5, 0.5, "-0.5", ha='center', va='center', fontsize=10, color='black')
209
- ax.text(0, 1.05, "0", ha='center', va='bottom', fontsize=10, color='black')
210
- ax.text(0.5, 0.5, "0.5", ha='center', va='center', fontsize=10, color='black')
211
- ax.text(1, 0, "1", ha='center', va='top', fontsize=10, color='black')
212
-
213
- # Add Title
214
- ax.set_title(f'{ticker} Market Sentiment (Simulated)', color='black', pad=20)
215
-
216
- # Remove axes
217
- ax.axis('off')
218
 
219
  return fig, news_summary
220
 
221
  except Exception as e:
222
- # Return error plot
223
  fig, ax = plt.subplots(figsize=(6, 4), facecolor='white')
224
  fig.patch.set_facecolor('white')
225
  ax.text(0.5, 0.5, f'Sentiment Error: {str(e)}', ha='center', va='center',
@@ -328,10 +312,8 @@ with gr.Blocks(
328
 
329
  with gr.TabItem("Sentiment Analysis"):
330
  with gr.Row():
331
- with gr.Column(scale=1):
332
- sentiment_gauge = gr.Plot(label="Sentiment Score")
333
- with gr.Column(scale=1):
334
- news_display = gr.HTML(label="Market News")
335
 
336
  with gr.TabItem("Fundamentals"):
337
  with gr.Row():
 
9
  from trading_logic import TradingLogic
10
  import io
11
  import base64
12
+ import plotly.graph_objects as go
13
 
14
  # Global instances
15
  data_processor = DataProcessor()
 
165
  """Analyze market sentiment for selected asset"""
166
  try:
167
  ticker = asset_map[asset_name]
168
+ # FIX: Menggunakan fungsi yang benar dari sentiment_analyzer.py
169
+ sentiment_score, news_summary = sentiment_analyzer.analyze_market_sentiment(ticker)
170
+
171
+ # --- Implementasi Plotly Gauge (sesuai referensi pengguna) ---
172
+
173
+ fig = go.Figure(go.Indicator(
174
+ mode="gauge+number+delta",
175
+ value=sentiment_score,
176
+ domain={'x': [0, 1], 'y': [0, 1]},
177
+ title={'text': f"{ticker} Market Sentiment (Simulated)"},
178
+ delta={'reference': 0},
179
+ gauge={
180
+ 'axis': {'range': [-1, 1]},
181
+ 'bar': {'color': "#FFD700"},
182
+ 'steps': [
183
+ {'range': [-1, -0.5], 'color': "rgba(255,0,0,0.5)"}, # Merah (Bearish)
184
+ {'range': [-0.5, 0.5], 'color': "rgba(100,100,100,0.3)"}, # Abu-abu (Neutral)
185
+ {'range': [0.5, 1], 'color': "rgba(0,255,0,0.5)"} # Hijau (Bullish)
186
+ ],
187
+ 'threshold': {
188
+ 'line': {'color': "black", 'width': 4},
189
+ 'thickness': 0.75,
190
+ 'value': 0
191
+ }
192
+ }
193
+ ))
194
+
195
+ fig.update_layout(
196
+ template='plotly_white',
197
+ height=300,
198
+ paper_bgcolor='white',
199
+ plot_bgcolor='white',
200
+ font=dict(color='black')
201
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
  return fig, news_summary
204
 
205
  except Exception as e:
206
+ # Return error plot (menggunakan Matplotlib untuk error fallback)
207
  fig, ax = plt.subplots(figsize=(6, 4), facecolor='white')
208
  fig.patch.set_facecolor('white')
209
  ax.text(0.5, 0.5, f'Sentiment Error: {str(e)}', ha='center', va='center',
 
312
 
313
  with gr.TabItem("Sentiment Analysis"):
314
  with gr.Row():
315
+ sentiment_gauge = gr.Plot(label="Sentiment Score")
316
+ news_display = gr.HTML(label="Market News")
 
 
317
 
318
  with gr.TabItem("Fundamentals"):
319
  with gr.Row():