omniverse1 commited on
Commit
dc6c2a4
·
verified ·
1 Parent(s): b8086d5

Update Gradio app with multiple files

Browse files
Files changed (2) hide show
  1. app.py +155 -263
  2. requirements.txt +27 -8
app.py CHANGED
@@ -1,314 +1,206 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import plotly.graph_objects as go
 
4
  from data_processor import DataProcessor
5
  from sentiment_analyzer import SentimentAnalyzer
6
  from model_handler import ModelHandler
7
  from trading_logic import TradingLogic
8
- import numpy as np
9
 
10
- # Global instances
11
  data_processor = DataProcessor()
12
  sentiment_analyzer = SentimentAnalyzer()
13
  model_handler = ModelHandler()
14
  trading_logic = TradingLogic()
15
 
16
- def create_chart_analysis(interval):
17
- """Create chart with technical indicators"""
18
  try:
 
19
  df = data_processor.get_gold_data(interval)
20
  if df.empty:
21
- return "No data available", None, None
22
 
23
- # Calculate indicators
24
  df = data_processor.calculate_indicators(df)
25
 
26
  # Create candlestick chart
27
- fig = go.Figure(data=[
28
- go.Candlestick(
29
- x=df.index,
30
- open=df['Open'],
31
- high=df['High'],
32
- low=df['Low'],
33
- close=df['Close'],
34
- name='Gold Price'
35
- )
36
- ])
37
-
38
- # Add Bollinger Bands
39
- fig.add_trace(go.Scatter(
40
- x=df.index, y=df['BB_upper'],
41
- line=dict(color='rgba(255,255,255,0.3)', width=1),
42
- name='BB Upper', showlegend=False
43
- ))
44
- fig.add_trace(go.Scatter(
45
- x=df.index, y=df['BB_lower'],
46
- line=dict(color='rgba(255,255,255,0.3)', width=1),
47
- fill='tonexty', fillcolor='rgba(255,255,255,0.1)',
48
- name='BB Lower', showlegend=False
49
- ))
50
-
51
- # Add moving averages
52
- fig.add_trace(go.Scatter(
53
- x=df.index, y=df['SMA_20'],
54
- line=dict(color='#FFD700', width=2),
55
- name='SMA 20'
56
- ))
57
- fig.add_trace(go.Scatter(
58
- x=df.index, y=df['SMA_50'],
59
- line=dict(color='#FFA500', width=2),
60
- name='SMA 50'
61
- ))
62
-
63
- fig.update_layout(
64
- title=f'Gold Futures (GC=F) - {interval}',
65
- yaxis_title='Price (USD)',
66
- xaxis_title='Date',
67
- template='plotly_dark',
68
- height=500,
69
- margin=dict(l=50, r=50, t=50, b=50),
70
- xaxis_rangeslider_visible=False,
71
- paper_bgcolor='rgba(0,0,0,0)',
72
- plot_bgcolor='rgba(0,0,0,0)',
73
- font=dict(color='white')
74
- )
75
 
76
  # Generate predictions
77
- predictions = model_handler.predict(df, horizon=10)
 
 
 
78
  current_price = df['Close'].iloc[-1]
 
 
79
 
80
- # Get signal
81
- signal, confidence = trading_logic.generate_signal(
82
- predictions, current_price, df
83
- )
84
 
85
- # Calculate TP/SL
86
- tp, sl = trading_logic.calculate_tp_sl(
87
- current_price, df['ATR'].iloc[-1], signal
88
- )
89
 
90
  # Create metrics display
91
  metrics = {
92
- "Current Price": f"${current_price:.2f}",
93
- "Signal": signal.upper(),
94
  "Confidence": f"{confidence:.1%}",
95
- "Take Profit": f"${tp:.2f}" if tp else "N/A",
96
- "Stop Loss": f"${sl:.2f}" if sl else "N/A",
97
  "RSI": f"{df['RSI'].iloc[-1]:.1f}",
98
  "MACD": f"{df['MACD'].iloc[-1]:.4f}",
99
- "Volume": f"{df['Volume'].iloc[-1]:,.0f}"
100
  }
101
 
102
- # Create prediction chart
103
- pred_fig = go.Figure()
104
- future_dates = pd.date_range(
105
- start=df.index[-1], periods=len(predictions), freq='D'
 
 
 
 
106
  )
107
 
108
- pred_fig.add_trace(go.Scatter(
109
- x=future_dates, y=predictions,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  mode='lines+markers',
111
  line=dict(color='#FFD700', width=3),
112
  marker=dict(size=6),
113
- name='Predictions'
114
  ))
115
 
116
- pred_fig.add_trace(go.Scatter(
117
- x=[df.index[-1], future_dates[0]],
118
- y=[current_price, predictions[0]],
 
119
  mode='lines',
120
- line=dict(color='rgba(255,215,0,0.5)', width=2, dash='dash'),
121
  showlegend=False
122
  ))
123
 
124
- pred_fig.update_layout(
125
- title='Price Prediction (Next 10 Periods)',
126
- yaxis_title='Price (USD)',
127
- xaxis_title='Date',
128
- template='plotly_dark',
129
- height=300,
130
- paper_bgcolor='rgba(0,0,0,0)',
131
- plot_bgcolor='rgba(0,0,0,0)',
132
- font=dict(color='white')
133
- )
134
-
135
- return fig, metrics, pred_fig
136
-
137
- except Exception as e:
138
- return str(e), None, None
139
-
140
- def analyze_sentiment():
141
- """Analyze gold market sentiment"""
142
- try:
143
- sentiment_score, news_summary = sentiment_analyzer.analyze_gold_sentiment()
144
-
145
- # Create sentiment gauge
146
- fig = go.Figure(go.Indicator(
147
- mode="gauge+number+delta",
148
- value=sentiment_score,
149
- domain={'x': [0, 1], 'y': [0, 1]},
150
- title={'text': "Gold Market Sentiment"},
151
- delta={'reference': 0},
152
- gauge={
153
- 'axis': {'range': [-1, 1]},
154
- 'bar': {'color': "#FFD700"},
155
- 'steps': [
156
- {'range': [-1, -0.5], 'color': "rgba(255,0,0,0.5)"},
157
- {'range': [-0.5, 0.5], 'color': "rgba(255,255,255,0.3)"},
158
- {'range': [0.5, 1], 'color': "rgba(0,255,0,0.5)"}
159
- ],
160
- 'threshold': {
161
- 'line': {'color': "white", 'width': 4},
162
- 'thickness': 0.75,
163
- 'value': 0
164
- }
165
- }
166
- ))
167
-
168
- fig.update_layout(
169
- template='plotly_dark',
170
- height=300,
171
- paper_bgcolor='rgba(0,0,0,0)',
172
- plot_bgcolor='rgba(0,0,0,0)',
173
- font=dict(color='white')
174
- )
175
-
176
- return fig, news_summary
177
-
178
- except Exception as e:
179
- return str(e), None
180
-
181
- def get_fundamentals():
182
- """Get fundamental analysis data"""
183
- try:
184
- fundamentals = data_processor.get_fundamental_data()
185
-
186
- # Create fundamentals table
187
- table_data = []
188
- for key, value in fundamentals.items():
189
- table_data.append([key, value])
190
-
191
- df = pd.DataFrame(table_data, columns=['Metric', 'Value'])
192
-
193
- # Create fundamentals gauge chart
194
- fig = go.Figure(go.Indicator(
195
- mode="gauge+number",
196
- value=fundamentals.get('Gold Strength Index', 50),
197
- title={'text': "Gold Strength Index"},
198
- gauge={
199
- 'axis': {'range': [0, 100]},
200
- 'bar': {'color': "#FFD700"},
201
- 'steps': [
202
- {'range': [0, 30], 'color': "rgba(255,0,0,0.5)"},
203
- {'range': [30, 70], 'color': "rgba(255,255,255,0.3)"},
204
- {'range': [70, 100], 'color': "rgba(0,255,0,0.5)"}
205
- ]
206
- }
207
  ))
208
-
209
- fig.update_layout(
210
- template='plotly_dark',
211
- height=300,
212
- paper_bgcolor='rgba(0,0,0,0)',
213
- plot_bgcolor='rgba(0,0,0,0)',
214
- font=dict(color='white')
215
- )
216
-
217
- return fig, df
218
-
219
- except Exception as e:
220
- return str(e), None
221
-
222
- # Create Gradio interface
223
- with gr.Blocks(
224
- theme=gr.themes.Default(primary_hue="yellow", secondary_hue="yellow"),
225
- title="Gold Trading Analysis & Prediction",
226
- css="""
227
- .gradio-container {background-color: #000000; color: #FFFFFF}
228
- .gr-button-primary {background-color: #FFD700 !important; color: #000000 !important}
229
- .gr-button-secondary {border-color: #FFD700 !important; color: #FFD700 !important}
230
- .gr-tab button {color: #FFFFFF !important}
231
- .gr-tab button.selected {background-color: #FFD700 !important; color: #000000 !important}
232
- .gr-highlighted {background-color: #1a1a1a !important}
233
- .anycoder-link {color: #FFD700 !important; text-decoration: none; font-weight: bold}
234
- """
235
- ) as demo:
236
-
237
- # Header with anycoder link
238
- gr.HTML("""
239
- <div style="text-align: center; padding: 20px;">
240
- <h1 style="color: #FFD700;">Gold Trading Analysis & Prediction</h1>
241
- <p>Advanced AI-powered analysis for Gold Futures (GC=F)</p>
242
- <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="anycoder-link">Built with anycoder</a>
243
- </div>
244
- """)
245
-
246
- with gr.Row():
247
- interval_dropdown = gr.Dropdown(
248
- choices=[
249
- "5m", "15m", "30m", "1h", "4h", "1d", "1wk", "1mo", "3mo"
250
- ],
251
- value="1d",
252
- label="Time Interval",
253
- info="Select analysis timeframe"
254
- )
255
- refresh_btn = gr.Button("🔄 Refresh Data", variant="primary")
256
 
257
- with gr.Tabs():
258
- with gr.TabItem("📊 Chart Analysis"):
259
- with gr.Row():
260
- chart_plot = gr.Plot(label="Price Chart")
261
- pred_plot = gr.Plot(label="Predictions")
262
-
263
- with gr.Row():
264
- metrics_output = gr.JSON(label="Trading Metrics")
265
-
266
- with gr.TabItem("📰 Sentiment Analysis"):
267
- with gr.Row():
268
- sentiment_gauge = gr.Plot(label="Sentiment Score")
269
- news_display = gr.HTML(label="Market News")
270
-
271
- with gr.TabItem("📈 Fundamentals"):
272
- with gr.Row():
273
- fundamentals_gauge = gr.Plot(label="Strength Index")
274
- fundamentals_table = gr.Dataframe(
275
- headers=["Metric", "Value"],
276
- label="Key Fundamentals",
277
- interactive=False
278
- )
279
 
280
- # Event handlers
281
- def update_all(interval):
282
- chart, metrics, pred = create_chart_analysis(interval)
283
- sentiment, news = analyze_sentiment()
284
- fund_gauge, fund_table = get_fundamentals()
285
-
286
- return chart, metrics, pred, sentiment, news, fund_gauge, fund_table
 
 
 
 
 
 
 
 
 
 
 
 
287
 
288
- refresh_btn.click(
289
- fn=update_all,
290
- inputs=interval_dropdown,
291
- outputs=[
292
- chart_plot, metrics_output, pred_plot,
293
- sentiment_gauge, news_display,
294
- fundamentals_gauge, fundamentals_table
295
- ]
296
  )
297
 
298
- demo.load(
299
- fn=update_all,
300
- inputs=interval_dropdown,
301
- outputs=[
302
- chart_plot, metrics_output, pred_plot,
303
- sentiment_gauge, news_display,
304
- fundamentals_gauge, fundamentals_table
305
- ]
306
- )
307
 
308
- if __name__ == "__main__":
309
- demo.launch(
310
- server_name="0.0.0.0",
311
- server_port=7860,
312
- share=False,
313
- show_api=True
314
- )
 
1
  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
8
  from trading_logic import TradingLogic
 
9
 
10
+ # Initialize components
11
  data_processor = DataProcessor()
12
  sentiment_analyzer = SentimentAnalyzer()
13
  model_handler = ModelHandler()
14
  trading_logic = TradingLogic()
15
 
16
+ def update_analysis(interval):
17
+ """Main analysis function that updates all components"""
18
  try:
19
+ # Get market data
20
  df = data_processor.get_gold_data(interval)
21
  if df.empty:
22
+ return [gr.update(value="No data available")] * 7
23
 
24
+ # Calculate technical indicators
25
  df = data_processor.calculate_indicators(df)
26
 
27
  # Create candlestick chart
28
+ chart_fig = create_candlestick_chart(df, interval)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Generate predictions
31
+ pred_df = model_handler.predict(df)
32
+ pred_fig = create_prediction_chart(df, pred_df)
33
+
34
+ # Get trading signal
35
  current_price = df['Close'].iloc[-1]
36
+ signal, confidence = trading_logic.generate_signal(pred_df, current_price, df)
37
+ tp, sl = trading_logic.calculate_tp_sl(current_price, df['ATR'].iloc[-1], signal)
38
 
39
+ # Analyze sentiment
40
+ sentiment_score, news_html = sentiment_analyzer.analyze_gold_sentiment()
41
+ sentiment_fig = create_sentiment_gauge(sentiment_score)
 
42
 
43
+ # Get fundamentals
44
+ fundamentals = data_processor.get_fundamental_data()
45
+ fundamentals_fig = create_fundamentals_gauge(fundamentals)
46
+ fundamentals_df = pd.DataFrame(list(fundamentals.items()), columns=['Metric', 'Value'])
47
 
48
  # Create metrics display
49
  metrics = {
50
+ "Current Price": f"${current_price:,.2f}",
51
+ "Trading Signal": signal.upper(),
52
  "Confidence": f"{confidence:.1%}",
53
+ "Take Profit": f"${tp:,.2f}" if tp else "N/A",
54
+ "Stop Loss": f"${sl:,.2f}" if sl else "N/A",
55
  "RSI": f"{df['RSI'].iloc[-1]:.1f}",
56
  "MACD": f"{df['MACD'].iloc[-1]:.4f}",
57
+ "Volume Ratio": f"{df['Volume_ratio'].iloc[-1]:.2f}x"
58
  }
59
 
60
+ return (
61
+ chart_fig,
62
+ metrics,
63
+ pred_fig,
64
+ sentiment_fig,
65
+ news_html,
66
+ fundamentals_fig,
67
+ fundamentals_df
68
  )
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):
75
+ """Create interactive candlestick chart with indicators"""
76
+ fig = go.Figure()
77
+
78
+ # Candlestick
79
+ fig.add_trace(go.Candlestick(
80
+ x=df.index,
81
+ open=df['Open'],
82
+ high=df['High'],
83
+ low=df['Low'],
84
+ close=df['Close'],
85
+ name='Gold Price'
86
+ ))
87
+
88
+ # Moving averages
89
+ fig.add_trace(go.Scatter(
90
+ x=df.index,
91
+ y=df['SMA_20'],
92
+ line=dict(color='#FFD700', width=2),
93
+ name='SMA 20'
94
+ ))
95
+
96
+ fig.add_trace(go.Scatter(
97
+ x=df.index,
98
+ y=df['SMA_50'],
99
+ line=dict(color='#FFA500', width=2),
100
+ name='SMA 50'
101
+ ))
102
+
103
+ fig.update_layout(
104
+ title=f'Gold Futures (GC=F) - {interval}',
105
+ yaxis_title='Price (USD)',
106
+ xaxis_title='Date/Time',
107
+ template='plotly_dark',
108
+ height=500,
109
+ margin=dict(l=50, r=50, t=50, b=50),
110
+ xaxis_rangeslider_visible=False,
111
+ paper_bgcolor='black',
112
+ plot_bgcolor='black',
113
+ font=dict(color='white')
114
+ )
115
+
116
+ return fig
117
+
118
+ def create_prediction_chart(df, pred_df):
119
+ """Create prediction chart"""
120
+ fig = go.Figure()
121
+
122
+ # Historical price
123
+ fig.add_trace(go.Scatter(
124
+ x=df.index,
125
+ y=df['Close'],
126
+ mode='lines',
127
+ line=dict(color='#FFFFFF', width=2),
128
+ name='Historical'
129
+ ))
130
+
131
+ # Predictions
132
+ if pred_df is not None and not pred_df.empty:
133
+ fig.add_trace(go.Scatter(
134
+ x=pred_df['date'],
135
+ y=pred_df['prediction'],
136
  mode='lines+markers',
137
  line=dict(color='#FFD700', width=3),
138
  marker=dict(size=6),
139
+ name='Prediction'
140
  ))
141
 
142
+ # Confidence interval
143
+ fig.add_trace(go.Scatter(
144
+ x=pred_df['date'],
145
+ y=pred_df['upper_bound'],
146
  mode='lines',
147
+ line=dict(color='rgba(255,215,0,0.2)', width=0),
148
  showlegend=False
149
  ))
150
 
151
+ fig.add_trace(go.Scatter(
152
+ x=pred_df['date'],
153
+ y=pred_df['lower_bound'],
154
+ mode='lines',
155
+ line=dict(color='rgba(255,215,0,0.2)', width=0),
156
+ fill='tonexty',
157
+ fillcolor='rgba(255,215,0,0.1)',
158
+ name='Confidence Interval'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
+ fig.update_layout(
162
+ title='Price Prediction',
163
+ yaxis_title='Price (USD)',
164
+ xaxis_title='Date/Time',
165
+ template='plotly_dark',
166
+ height=300,
167
+ paper_bgcolor='black',
168
+ plot_bgcolor='black',
169
+ font=dict(color='white')
170
+ )
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
+ return fig
173
+
174
+ def create_sentiment_gauge(score):
175
+ """Create sentiment gauge chart"""
176
+ fig = go.Figure(go.Indicator(
177
+ mode="gauge+number",
178
+ value=score,
179
+ domain={'x': [0, 1], 'y': [0, 1]},
180
+ title={'text': "Market Sentiment"},
181
+ gauge={
182
+ 'axis': {'range': [-1, 1]},
183
+ 'bar': {'color': "#FFD700"},
184
+ 'steps': [
185
+ {'range': [-1, -0.5], 'color': "rgba(255,0,0,0.5)"},
186
+ {'range': [-0.5, 0.5], 'color': "rgba(255,255,255,0.3)"},
187
+ {'range': [0.5, 1], 'color': "rgba(0,255,0,0.5)"}
188
+ ]
189
+ }
190
+ ))
191
 
192
+ fig.update_layout(
193
+ template='plotly_dark',
194
+ height=300,
195
+ paper_bgcolor='black',
196
+ plot_bgcolor='black',
197
+ font=dict(color='white')
 
 
198
  )
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
+ fig = go.Figure(go
 
 
requirements.txt CHANGED
@@ -1,11 +1,30 @@
 
 
1
  gradio
2
- yfinance
3
- torch
4
- transformers
5
  pandas
6
- numpy
7
- plotly
8
- scipy
 
 
 
 
 
9
  scikit-learn
10
- safetensors
11
- huggingface-hub
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ plotly
2
+ numpy
3
  gradio
 
 
 
4
  pandas
5
+ kaleido
6
+ requests
7
+ Pillow
8
+ matplotlib
9
+ openpyxl
10
+ xlrd
11
+ python-docx
12
+ PyPDF2
13
  scikit-learn
14
+ scipy
15
+ joblib
16
+ git+https://github.com/huggingface/transformers
17
+ sentencepiece
18
+ torch
19
+ accelerate
20
+ tokenizers
21
+ datasets
22
+ yfinance
23
+ ta-lib
24
+ seaborn
25
+ scipy
26
+ joblib
27
+ tqdm
28
+ requests
29
+ urllib3
30
+ certifi