import os import pandas as pd from kaggle.api.kaggle_api_extended import KaggleApi import streamlit as st # App title st.title("Automated Investment Advisory App") # Retrieve Kaggle credentials from Hugging Face secrets api_key = os.getenv("Kaggle_API_Key") username = os.getenv("Kaggle_Username") # Make sure you add this to Hugging Face secrets # Function to set up Kaggle API configuration def setup_kaggle_api(): # Check if both API key and username are provided if api_key and username: # Create the .kaggle directory if it doesn't exist kaggle_dir = os.path.expanduser("~/.kaggle") os.makedirs(kaggle_dir, exist_ok=True) # Write kaggle.json file with credentials kaggle_json_path = os.path.join(kaggle_dir, "kaggle.json") with open(kaggle_json_path, "w") as f: f.write(f'{{"username":"{username}", "key":"{api_key}"}}') # Set correct permissions for the kaggle.json file os.chmod(kaggle_json_path, 0o600) # Authenticate Kaggle API api = KaggleApi() api.authenticate() return api else: st.error("Kaggle API credentials not found. Please ensure 'Kaggle_API_Key' and 'Kaggle_Username' are set in Hugging Face secrets.") return None # Set up and authenticate Kaggle API api = setup_kaggle_api() # Download and load dataset if API is configured if api: dataset_name = "borismarjanovic/price-volume-data-for-all-us-stocks-etfs" try: # Download the dataset to the current directory (where app.py exists) api.dataset_download_files(dataset_name, path=".", unzip=True) # Load dataset (adjust the filename if needed) df = pd.read_csv("all_stocks_5yr.csv") # App UI for user inputs st.sidebar.header("Investment Advisory Options") # Input for investment goal and risk tolerance investment_goal = st.sidebar.number_input("Enter your desired annual return (%)", min_value=0.0, step=0.1) risk_tolerance = st.sidebar.selectbox("Risk Tolerance", ["Low", "Medium", "High"]) # Filtering dataset based on user preferences # Assuming dataset has columns 'Name', 'Close', 'Volume', and others that reflect company details # Here we use a simplified filtering approach based on risk tolerance if risk_tolerance == "Low": suggested_companies = df[df['Volume'] < df['Volume'].quantile(0.33)] # Example logic for lower volatility elif risk_tolerance == "Medium": suggested_companies = df[(df['Volume'] >= df['Volume'].quantile(0.33)) & (df['Volume'] < df['Volume'].quantile(0.66))] else: suggested_companies = df[df['Volume'] >= df['Volume'].quantile(0.66)] # Higher volume for high risk # Display suggested companies st.write(f"Suggested companies for {risk_tolerance} risk tolerance:") st.dataframe(suggested_companies[['Name', 'Close', 'Volume']].head(10)) # Show insights for a selected company selected_company = st.selectbox("Select a company for more details", suggested_companies['Name'].unique()) company_data = suggested_companies[suggested_companies['Name'] == selected_company] st.write(f"Details for {selected_company}:") st.dataframe(company_data) except Exception as e: st.error(f"An error occurred: {e}") else: st.warning("Unable to load data due to missing Kaggle API credentials.")