aayush9 commited on
Commit
4b2f91d
·
verified ·
1 Parent(s): b325f9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -97,7 +97,7 @@ def get_stock_data(api_key, symbol, start_date, end_date):
97
  else:
98
  return []
99
 
100
- def predict_stock_price(model, data_loader):
101
  model.eval()
102
  predictions = []
103
  with torch.no_grad():
@@ -109,8 +109,6 @@ def predict_stock_price(model, data_loader):
109
  return predictions
110
 
111
 
112
-
113
-
114
  # Function to load HF model and tokenizer
115
  def load_hf_model_and_tokenizer(model_name):
116
  model = BertForSequenceClassification.from_pretrained(model_name)
@@ -228,21 +226,22 @@ with tab2:
228
 
229
  # Predictions
230
  predictions = predict_stock_price(model, new_dataloader)
231
-
232
  # Convert predictions to percentage change
233
- predicted_percentage_change = predictions # Modify this line as needed based on how your model is trained to predict percentage change
 
 
 
 
 
234
 
235
  # Get actual percentage change from the CSV file
236
- actual_percentage_change = new_data['percentage_change'].values
 
237
 
238
  # Predictions for tomorrow
239
  tomorrow_date = datetime.now() + timedelta(days=1)
240
- tomorrow_prediction = pipeline('text-classification', model=model_name, tokenizer=tokenizer, token=api_token)(f"Publication Date: {tomorrow_date}, Sentiment Polarity: 0, Sentiment Confidence: 0, Keywords: None, Stock Date: None")
241
- print(tomorrow_prediction)
242
- if tomorrow_prediction:
243
- tomorrow_prediction_value = tomorrow_prediction[0]['score']
244
- else:
245
- tomorrow_prediction_value = None
246
  import subprocess
247
 
248
  # Function to run cdb2.py script
@@ -262,20 +261,20 @@ with tab2:
262
  fig, ax = plt.subplots(figsize=(12, 8))
263
 
264
  # Plot actual vs predicted percentage change
265
- ax.plot(new_data['stock_date'], actual_percentage_change, label='Actual Percentage Change', marker='o', linestyle='-')
266
 
267
  # Plot predicted percentage change if available
268
  if predicted_percentage_change:
269
- ax.plot(new_data['stock_date'], predicted_percentage_change, label='Predicted Percentage Change', marker='x', linestyle='--')
270
 
271
  # Plot tomorrow's prediction
272
- ax.plot(tomorrow_date, tomorrow_prediction_value, label='Tomorrow Prediction', marker='*', linestyle='--')
273
 
274
  # Draw a dotted green line from the last predicted percentage change to tomorrow's prediction if predictions are available
275
  if predicted_percentage_change:
276
  last_predicted_date = new_data['stock_date'].iloc[-1]
277
  last_predicted_change = predicted_percentage_change[-1]
278
- ax.plot([last_predicted_date, tomorrow_date], [last_predicted_change, tomorrow_prediction_value], 'g--')
279
 
280
 
281
  # Formatting
 
97
  else:
98
  return []
99
 
100
+ def predict_stock_price(model, new_dataloader):
101
  model.eval()
102
  predictions = []
103
  with torch.no_grad():
 
109
  return predictions
110
 
111
 
 
 
112
  # Function to load HF model and tokenizer
113
  def load_hf_model_and_tokenizer(model_name):
114
  model = BertForSequenceClassification.from_pretrained(model_name)
 
226
 
227
  # Predictions
228
  predictions = predict_stock_price(model, new_dataloader)
 
229
  # Convert predictions to percentage change
230
+ # Average out every two adjacent values in the list
231
+ averaged_predictions = [(predictions[i] + predictions[i+1]) / 2 for i in range(0, len(predictions)-1, 2)]
232
+ averaged_predictions.append(predictions[-1])
233
+
234
+ # Store the averaged predictions in the same list
235
+ predicted_percentage_change = averaged_predictions
236
 
237
  # Get actual percentage change from the CSV file
238
+ actual_percentage_change = new_data['percentage_change'].values[::2]
239
+
240
 
241
  # Predictions for tomorrow
242
  tomorrow_date = datetime.now() + timedelta(days=1)
243
+ tomorrow_prediction = predictions[-1]
244
+ print(tomorrow_date)
 
 
 
 
245
  import subprocess
246
 
247
  # Function to run cdb2.py script
 
261
  fig, ax = plt.subplots(figsize=(12, 8))
262
 
263
  # Plot actual vs predicted percentage change
264
+ ax.plot(new_data['stock_date'][::2][:-1], actual_percentage_change[:-1], label='Actual Percentage Change', marker='o', linestyle='-')
265
 
266
  # Plot predicted percentage change if available
267
  if predicted_percentage_change:
268
+ ax.plot(new_data['stock_date'][::2], predicted_percentage_change, label='Predicted Percentage Change', marker='x', linestyle='--')
269
 
270
  # Plot tomorrow's prediction
271
+ ax.plot(tomorrow_date, tomorrow_prediction, label='Tomorrow Prediction', marker='*', linestyle='--')
272
 
273
  # Draw a dotted green line from the last predicted percentage change to tomorrow's prediction if predictions are available
274
  if predicted_percentage_change:
275
  last_predicted_date = new_data['stock_date'].iloc[-1]
276
  last_predicted_change = predicted_percentage_change[-1]
277
+ ax.plot([last_predicted_date, tomorrow_date], [last_predicted_change, tomorrow_prediction], 'g--')
278
 
279
 
280
  # Formatting