sithuWiki commited on
Commit
ed26a7b
·
verified ·
1 Parent(s): a8a9175

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -44
app.py CHANGED
@@ -52,7 +52,7 @@ def init_app():
52
  init_predictor()
53
 
54
 
55
- def predict_roi(miner_name, region, prediction_date):
56
  """Make prediction for a specific date"""
57
  try:
58
  window_size = 30
@@ -60,6 +60,19 @@ def predict_roi(miner_name, region, prediction_date):
60
  # Convert prediction_date to datetime
61
  if isinstance(prediction_date, str):
62
  prediction_date = datetime.strptime(prediction_date, '%Y-%m-%d')
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  print(f"\n{'='*80}")
65
  print(f"PREDICTION REQUEST")
@@ -88,31 +101,22 @@ def predict_roi(miner_name, region, prediction_date):
88
  print(f"✅ Got {len(blockchain_df)} days of data")
89
  print(f" Date range: {blockchain_df['date'].min().date()} to {blockchain_df['date'].max().date()}")
90
 
91
- # Fetch ASIC price for the selected date
92
- print(f"\n💰 Fetching ASIC price for {prediction_date.date()}...")
93
- miner_prices, data_available = fetch_asic_price_for_date(prediction_date)
94
-
95
- if not data_available:
96
- warning_html = f"""
97
- <div style='background: #f39c12; color: white; padding: 15px; border-radius: 10px; margin-bottom: 20px;'>
98
- <h3 style='margin: 0;'>⚠️ Warning: Price Data Unavailable</h3>
99
- <p style='margin: 10px 0 0 0;'>
100
- No ASIC price data available for {prediction_date.date()}.
101
- Using fallback prices (approximate market values).
102
- </p>
103
- </div>
104
- """
105
- else:
106
- warning_html = ""
107
-
108
- miner_price = miner_prices.get(miner_name, FALLBACK_PRICES.get(miner_name, 2500))
109
- price_source = "API" if data_available else "Fallback"
110
-
111
- print(f" Price for {miner_name}: ${miner_price:,.2f} ({price_source})")
112
 
113
  # Get sequence (now uses blockchain_df which is date-specific)
114
  print(f"\n🔧 Preparing features...")
115
- sequence, _, pred_date = get_latest_sequence(blockchain_df, miner_name, miner_price, region, window_size)
 
 
 
 
 
 
 
 
 
 
116
  print(f"✅ Sequence prepared: {sequence.shape}")
117
 
118
  # Predict
@@ -121,7 +125,17 @@ def predict_roi(miner_name, region, prediction_date):
121
  print(f"✅ Prediction: {result['predicted_label']} ({result['confidence']:.1%})")
122
 
123
  # Create displays
124
- miner_info = create_miner_info(miner_name, miner_price, region, price_source, prediction_date)
 
 
 
 
 
 
 
 
 
 
125
  prediction_html = warning_html + create_prediction_html(result, pred_date, window_size)
126
  confidence_chart = create_confidence_chart(result['probabilities'])
127
  price_chart = create_price_chart(blockchain_df, window_size)
@@ -145,42 +159,61 @@ def predict_roi(miner_name, region, prediction_date):
145
  return error, error, None, None
146
 
147
 
148
- def create_miner_info(miner_name, price, region, source, prediction_date):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  specs = MINER_SPECS[miner_name]
150
- # Daily electricity rate for the prediction date (with fallback beyond CSV range)
151
- elec_rate = get_electricity_rate(region, prediction_date.date())
152
- daily_cost = (specs['power'] * 24 / 1000) * elec_rate
153
-
154
- # Color coding for source
 
155
  if source == "API":
156
  badge_color = "#27ae60" # Green
 
 
157
  else:
158
- badge_color = "#e74c3c" # Red for fallback
159
-
160
  return f"""
161
  <div style="background: #1e1e1e; padding: 20px; border-radius: 10px; border: 1px solid #333; color: #ffffff;">
162
- <h3 style="color: #F7931A; margin-top: 0;">{specs['full_name']}</h3>
163
  <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
164
  <div>
165
- <p style="color: #ffffff;"><strong style="color: #ffffff;">Hashrate:</strong> {specs['hashrate']} TH/s</p>
166
- <p style="color: #ffffff;"><strong style="color: #ffffff;">Power:</strong> {specs['power']} W</p>
167
- <p style="color: #ffffff;"><strong style="color: #ffffff;">Efficiency:</strong> {specs['efficiency']} W/TH</p>
168
  </div>
169
  <div>
170
- <p style="color: #ffffff;"><strong style="color: #ffffff;">Price ({prediction_date.date()}):</strong> ${price:,.2f}
171
- <span style="background: {badge_color}; color: white; padding: 2px 8px; border-radius: 4px; font-size: 0.8em;">{source}</span>
 
 
 
172
  </p>
173
- <p style="color: #ffffff;"><strong style="color: #ffffff;">Region:</strong> {region.title()}</p>
174
- <p style="color: #ffffff;"><strong style="color: #ffffff;">Electricity:</strong> ${elec_rate:.4f}/kWh</p>
 
175
  </div>
176
  </div>
177
- <div style="margin-top: 15px; padding-top: 15px; border-top: 1px solid #333;">
178
- <p style="color: #ffffff;"><strong style="color: #ffffff;">Daily Electricity Cost:</strong> ${daily_cost:.2f}</p>
179
- </div>
180
  </div>
181
  """
182
 
183
 
 
184
  def create_prediction_html(result, date, window):
185
  label = result['predicted_label']
186
  conf = result['confidence']
@@ -261,6 +294,31 @@ def create_interface():
261
  placeholder="2024-12-08",
262
  elem_classes="date-input"
263
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
  btn = gr.Button("🔮 Predict ROI", variant="primary", size="lg")
266
 
@@ -291,7 +349,20 @@ def create_interface():
291
  conf_plot = gr.Plot()
292
  price_plot = gr.Plot()
293
 
294
- btn.click(fn=predict_roi, inputs=[miner, region, prediction_date], outputs=[miner_info, prediction, conf_plot, price_plot])
 
 
 
 
 
 
 
 
 
 
 
 
 
295
 
296
  return app
297
 
 
52
  init_predictor()
53
 
54
 
55
+ def predict_roi(miner_name,region,prediction_date,machine_price,machine_hashrate,machine_power,machine_efficiency,electricity_rate):
56
  """Make prediction for a specific date"""
57
  try:
58
  window_size = 30
 
60
  # Convert prediction_date to datetime
61
  if isinstance(prediction_date, str):
62
  prediction_date = datetime.strptime(prediction_date, '%Y-%m-%d')
63
+
64
+ miner_price = float(machine_price)
65
+ miner_hashrate = float(machine_hashrate)
66
+ machine_power = float(machine_power)
67
+ machine_efficiency = float(machine_efficiency)
68
+ electricity_rate = float(electricity_rate)
69
+
70
+ print(f"User machine specs:")
71
+ print(f" Price: {miner_price}")
72
+ print(f" Hashrate (TH/s): {miner_hashrate}")
73
+ print(f" Power (W): {machine_power}")
74
+ print(f" Efficiency: {machine_efficiency}")
75
+ print(f" Elec rate: {electricity_rate} USD/kWh")
76
 
77
  print(f"\n{'='*80}")
78
  print(f"PREDICTION REQUEST")
 
101
  print(f"✅ Got {len(blockchain_df)} days of data")
102
  print(f" Date range: {blockchain_df['date'].min().date()} to {blockchain_df['date'].max().date()}")
103
 
104
+ price_source = "User input"
105
+ print(f" Using user-provided price for {miner_name}: ${miner_price:,.2f}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  # Get sequence (now uses blockchain_df which is date-specific)
108
  print(f"\n🔧 Preparing features...")
109
+ sequence, _, pred_date = get_latest_sequence(
110
+ blockchain_df,
111
+ miner_name,
112
+ miner_price,
113
+ region,
114
+ window_size,
115
+ machine_hashrate=miner_hashrate,
116
+ power=machine_power,
117
+ efficiency=machine_efficiency,
118
+ electricity_rate=electricity_rate,
119
+ )
120
  print(f"✅ Sequence prepared: {sequence.shape}")
121
 
122
  # Predict
 
125
  print(f"✅ Prediction: {result['predicted_label']} ({result['confidence']:.1%})")
126
 
127
  # Create displays
128
+ miner_info = create_miner_info(
129
+ miner_name,
130
+ miner_price,
131
+ region,
132
+ price_source,
133
+ prediction_date,
134
+ miner_hashrate,
135
+ machine_power,
136
+ machine_efficiency,
137
+ electricity_rate,
138
+ )
139
  prediction_html = warning_html + create_prediction_html(result, pred_date, window_size)
140
  confidence_chart = create_confidence_chart(result['probabilities'])
141
  price_chart = create_price_chart(blockchain_df, window_size)
 
159
  return error, error, None, None
160
 
161
 
162
+ def create_miner_info(
163
+ miner_name,
164
+ price,
165
+ region,
166
+ source,
167
+ prediction_date,
168
+ machine_hashrate,
169
+ machine_power,
170
+ machine_efficiency,
171
+ electricity_rate,
172
+ ):
173
+ """
174
+ Display miner info using user-provided specs (price, hashrate, power, efficiency, elec rate).
175
+ We still use MINER_SPECS only to get the pretty full_name.
176
+ """
177
  specs = MINER_SPECS[miner_name]
178
+ full_name = specs["full_name"]
179
+
180
+ elec_rate = float(electricity_rate)
181
+ daily_cost = (float(machine_power) * 24.0 / 1000.0) * elec_rate
182
+
183
+ # Color coding for price source
184
  if source == "API":
185
  badge_color = "#27ae60" # Green
186
+ elif source == "User input":
187
+ badge_color = "#3498db" # Blue
188
  else:
189
+ badge_color = "#e74c3c" # Red
190
+
191
  return f"""
192
  <div style="background: #1e1e1e; padding: 20px; border-radius: 10px; border: 1px solid #333; color: #ffffff;">
193
+ <h3 style="color: #F7931A; margin-top: 0;">{full_name}</h3>
194
  <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
195
  <div>
196
+ <p><strong>Hashrate:</strong> {machine_hashrate:.2f} TH/s</p>
197
+ <p><strong>Power:</strong> {machine_power:.1f} W</p>
198
+ <p><strong>Efficiency:</strong> {machine_efficiency:.2f} W/TH</p>
199
  </div>
200
  <div>
201
+ <p>
202
+ <strong>Price ({prediction_date.date()}):</strong> ${price:,.2f}
203
+ <span style="background: {badge_color}; color: white; padding: 2px 8px; border-radius: 4px; font-size: 0.8em;">
204
+ {source}
205
+ </span>
206
  </p>
207
+ <p><strong>Region:</strong> {region.title()}</p>
208
+ <p><strong>Electricity rate:</strong> {elec_rate:.4f} USD/kWh</p>
209
+ <p><strong>Estimated daily elec cost:</strong> ${daily_cost:,.2f}</p>
210
  </div>
211
  </div>
 
 
 
212
  </div>
213
  """
214
 
215
 
216
+
217
  def create_prediction_html(result, date, window):
218
  label = result['predicted_label']
219
  conf = result['confidence']
 
294
  placeholder="2024-12-08",
295
  elem_classes="date-input"
296
  )
297
+ machine_price = gr.Number(
298
+ label="Machine price (USD)",
299
+ value=2500.0, # you can choose a nicer default
300
+ precision=2,
301
+ )
302
+ machine_hashrate = gr.Number(
303
+ label="Machine hashrate (TH/s)",
304
+ value=100.0,
305
+ precision=2,
306
+ )
307
+ machine_power = gr.Number(
308
+ label="Power (W)",
309
+ value=3000.0,
310
+ precision=1,
311
+ )
312
+ machine_efficiency = gr.Number(
313
+ label="Efficiency (W/TH)",
314
+ value=30.0,
315
+ precision=2,
316
+ )
317
+ electricity_rate = gr.Number(
318
+ label="Electricity rate (USD/kWh)",
319
+ value=ELECTRICITY_RATES["texas"], # 0.1549 by default
320
+ precision=4,
321
+ )
322
 
323
  btn = gr.Button("🔮 Predict ROI", variant="primary", size="lg")
324
 
 
349
  conf_plot = gr.Plot()
350
  price_plot = gr.Plot()
351
 
352
+ btn.click(
353
+ fn=predict_roi,
354
+ inputs=[
355
+ miner,
356
+ region,
357
+ prediction_date,
358
+ machine_price,
359
+ machine_hashrate,
360
+ machine_power,
361
+ machine_efficiency,
362
+ electricity_rate,
363
+ ],
364
+ outputs=[miner_info, prediction, conf_plot, price_plot],
365
+ )
366
 
367
  return app
368