Spaces:
Sleeping
Sleeping
Download app.py from Huggingfacelearner123/AI-stocks: direct link, hf CLI and curl.
- Browser
- Download file 3.69 kB
-
https://huggingface.co/spaces/Huggingfacelearner123/AI-stocks/resolve/main/app.py
- Command line
-
hf download hf://spaces/Huggingfacelearner123/AI-stocks/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Huggingfacelearner123/AI-stocks/resolve/main/app.py
3.69 kB
| """ | |
| This Python code is designed to help investors make informed decisions on how to distribute their investment | |
| across five AI related stocks that are deemed to be potentially undervalued, based on their Price-to-Earnings (P/E) ratios. | |
| The goal is to allocate more funds to stocks with lower P/E ratios, under the premise that they might offer better value. | |
| The approach includes: | |
| 1. Listing a predefined set of stock tickers of interest. | |
| 2. Fetching P/E ratios for these stocks using the yfinance library. | |
| 3. Sorting these stocks by their P/E ratios from lowest to highest. | |
| 4. Selecting the top 5 stocks with the lowest P/E ratios. | |
| 5. Calculating how much to invest in each stock in inverse proportion to their P/E ratios. | |
| 6. Allocating the investment across these stocks based on the calculated proportions. | |
| The method prioritizes stocks with lower P/E ratios, intending to maximize the value of the investment by focusing on stocks that are potentially undervalued. | |
| """ | |
| # install and import the necessary modules | |
| # pip install -q yfinance | |
| # pip install -q gradio | |
| import yfinance as yf | |
| import pandas as pd | |
| import gradio as gr | |
| import logging | |
| # Suppress messages unless there is an error | |
| logging.basicConfig(level=logging.ERROR) | |
| def invest_in_stocks(investment_amount): | |
| tickers_list = ['AAPL', 'TSM', 'ADBE', 'CRM', 'AMD', 'AMZN', 'GOOG', 'MSFT', 'NVDA'] | |
| data = [] | |
| for ticker in tickers_list: | |
| stock = yf.Ticker(ticker) | |
| pe_ratio = stock.info.get('trailingPE', None) | |
| data.append({'Ticker': ticker, 'P/E Ratio': pe_ratio}) | |
| df = pd.DataFrame(data).copy() | |
| df_sorted = df.sort_values(by='P/E Ratio') | |
| df_top5_lowest_pe = df_sorted.head(5).copy() | |
| df_top5_lowest_pe['Inverse PE'] = 1 / df_top5_lowest_pe['P/E Ratio'] # invest in inverse proportion to the individual stock p/e; the cheaper stock gets more $ | |
| total_inverse_pe = df_top5_lowest_pe['Inverse PE'].sum() | |
| df_top5_lowest_pe['Investment Proportion'] = df_top5_lowest_pe['Inverse PE'] / total_inverse_pe | |
| df_top5_lowest_pe['Investment %'] = (100 * df_top5_lowest_pe['Investment Proportion']).round(0) # Round to whole number | |
| df_top5_lowest_pe['Investment $'] = (investment_amount * df_top5_lowest_pe['Investment Proportion']).round(0) # Round to whole number | |
| # Preparing the dataframe for output | |
| output_df = df_top5_lowest_pe[['Ticker', 'Investment %', 'Investment $']] | |
| return output_df | |
| # now use gradio to create a user interface, show a welcome message explaining the investment approach and ask for input | |
| welcome_message = """ | |
| # Welcome to the AI Portfolio Investment Tool! 🚀 | |
| This tool helps you **invest** in a portfolio of potentially undervalued **AI stocks** based on their **Price-to-Earnings (P/E) ratios**. | |
| With this interface, you can: | |
| - View a curated list of stocks. | |
| - Specify the amount you'd like to invest. | |
| - See how your investment would be allocated across the **top 5** selected stocks. | |
| ### Current Stock List: | |
| - Apple Inc. (AAPL) | |
| - Taiwan Semiconductor Manufacturing Company (TSM) | |
| - Adobe Inc. (ADBE) | |
| - Salesforce (CRM) | |
| - Advanced Micro Devices (AMD) | |
| - Amazon.com, Inc. (AMZN) | |
| - Alphabet Inc. (GOOG) | |
| - Microsoft Corporation (MSFT) | |
| - NVIDIA Corporation (NVDA) | |
| Enter the amount you'd like to invest below and see how it gets allocated. Let's get started! | |
| """ | |
| with gr.Blocks() as app: | |
| gr.Markdown(welcome_message) | |
| investment_input = gr.Number(label="Investment Amount ($)", value=100) | |
| output_table = gr.Dataframe(headers=["Ticker", "Investment %", "Investment $"], label="Investment Allocation") | |
| investment_input.change(fn=invest_in_stocks, inputs=investment_input, outputs=output_table) | |
| app.launch(share=True) | |