Skip to content

Commit

Permalink
hide api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jackturner83 committed Feb 13, 2024
1 parent e2ae841 commit 4f1f22a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SENTIMENT_KEY=22f6dfa7bb114867a3bcb7c56b6c6410
GPT_KEY=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
16 changes: 8 additions & 8 deletions src/fetch_sentiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from textblob import TextBlob
import pandas as pd
from datetime import datetime, timedelta
from dotenv import load_dotenv
import os

# Load environment variables from .env
load_dotenv()

# Function to fetch news headlines for a specific date
def fetch_news(api_key, date):
def fetch_news(date):
all_headlines = []
url = "https://newsapi.org/v2/everything"
params = {
Expand All @@ -15,7 +18,7 @@ def fetch_news(api_key, date):
"to": date,
"sortBy": "publishedAt",
"language": "en",
"apiKey": api_key,
"apiKey": os.getenv("SENTIMENT_KEY"), # Use environment variable
}
response = requests.get(url, params=params)
if response.status_code == 200:
Expand All @@ -26,15 +29,13 @@ def fetch_news(api_key, date):
print(f"Failed to fetch news for {date}")
return all_headlines


# Function to calculate average sentiment score for headlines
def average_sentiment(headlines):
sentiment_scores = [TextBlob(headline).sentiment.polarity for headline in headlines]
return sum(sentiment_scores) / len(sentiment_scores) if sentiment_scores else 0


# Main function to fetch, process news headlines, and save to CSV
def main(api_key):
def main():
end_date = datetime.now()
start_date = end_date - timedelta(days=30)

Expand All @@ -45,7 +46,7 @@ def main(api_key):

for date in dates:
date_str = date.strftime("%Y-%m-%d")
headlines = fetch_news(api_key, date_str)
headlines = fetch_news(date_str)
if headlines: # Proceed if there are headlines for the day
avg_sentiment = average_sentiment(headlines)
average_sentiments.append(
Expand Down Expand Up @@ -75,5 +76,4 @@ def main(api_key):


if __name__ == "__main__":
api_key = "22f6dfa7bb114867a3bcb7c56b6c6410"
main(api_key)
main()
19 changes: 9 additions & 10 deletions src/gpt.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import os
from openai import OpenAI
import pandas as pd
from dotenv import load_dotenv

# Load environment variables from .env
load_dotenv()

# Function to load data from the combined CSV file
def load_data(combined_data_path, linear_regression_data):
df_combined = pd.read_csv(combined_data_path)
df_linear = pd.read_csv(linear_regression_data)
return df_combined, df_linear


# Function to create summaries or extract relevant data
def summarize_data(df_combined, df_linear):
combined_summary = df_combined.describe().to_string()
linear_regression_summary = df_linear.describe().to_string()
return combined_summary, linear_regression_summary


# Function to set up OpenAI API key
def setup_openai_api(api_key):
os.environ["OPENAI_API_KEY"] = api_key

def setup_openai_api():
os.environ["OPENAI_API_KEY"] = os.getenv("GPT_KEY") # Use environment variable

# Function to make predictions using OpenAI
def make_prediction(combined_summary, linear_regression_summary):
def make_prediction():
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
Expand All @@ -40,7 +40,6 @@ def make_prediction(combined_summary, linear_regression_summary):
)
return completion.choices[0].message


# Main function to orchestrate the modular components
def main():
# Get the current directory of the script
Expand All @@ -60,10 +59,10 @@ def main():
combined_summary, linear_regression_summary = summarize_data(df_combined, df_linear)

# Set up OpenAI API key
setup_openai_api("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")
setup_openai_api()

# Make a prediction
prediction = make_prediction(combined_summary, linear_regression_summary)
prediction = make_prediction()

# Save prediction results to CSV
prediction_df = pd.DataFrame({"Prediction": [prediction]})
Expand All @@ -75,4 +74,4 @@ def main():


if __name__ == "__main__":
main()
main()
28 changes: 7 additions & 21 deletions src/predict_closing_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,28 @@
from sklearn.metrics import mean_squared_error
from datetime import datetime


# Function to load data from the combined CSV file
def load_data(combined_data_path):
df_combined = pd.read_csv(combined_data_path)
return df_combined


# Function to split data into training and testing sets
def split_data(df_combined):
features = df_combined[["Average Sentiment", "Volume"]]
target = df_combined["Close"]
X_train, X_test, y_train, y_test = train_test_split(
features, target, test_size=0.2, random_state=42
)
return X_train, X_test, y_train, y_test


# Function to train a linear regression model
def train_model(X_train, y_train):
model = LinearRegression()
model.fit(X_train, y_train)
return model


# Function to evaluate the model's performance
def evaluate_model(model, X_test, y_test):
predictions = model.predict(X_test)
mse = mean_squared_error(y_test, predictions)
return mse


# Function to predict the next day's closing price
def predict_next_day_closing_price(model, next_day_features):
predicted_closing_price = model.predict([next_day_features])
return predicted_closing_price


# Main function to orchestrate the process
def main():
# Get the current directory of the script
Expand All @@ -54,8 +39,12 @@ def main():
# Load the data
df_combined = load_data(combined_data_path)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = split_data(df_combined)
# Features and target
X = df_combined[['Average Sentiment', 'Volume']]
y = df_combined['Close']

# Train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the linear regression model
model = train_model(X_train, y_train)
Expand All @@ -65,10 +54,7 @@ def main():
print(f"Mean Squared Error: {mse}")

# Example: Features for the next day
next_day_features = [
0.2,
10000,
] # Example values for 'Average Sentiment' and 'Volume'
next_day_features = [0.2, 10000] # Example values for 'Average Sentiment' and 'Volume'

# Predict the next day's closing price
next_day_closing_price = predict_next_day_closing_price(model, next_day_features)
Expand Down

0 comments on commit 4f1f22a

Please sign in to comment.