Spaces:
Sleeping
Sleeping
File size: 7,336 Bytes
0e2d97d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
import os
import streamlit as st
from dotenv import load_dotenv
from helper import get_model_response, get_income_statement, get_balance_sheet, get_ticker, process_pdf
import matplotlib.pyplot as plt
import pandas as pd
# Load environment variables
load_dotenv()
# Set Streamlit page config with black background and colored text
st.set_page_config(page_title="Fundamental Analysis Dashboard", layout="wide")
st.markdown(
"""
<style>
.reportview-container, .main, .block-container {
background-color: black;
color: white;
}
.stTextArea, .stTextArea textarea {
background-color: #333333;
color: white;
}
.stDataFrame {
color: white;
}
th {
color: white;
font-weight: bold;
}
td {
color: white;
}
.stButton button {
background-color: #333333;
color: white;
}
h1, h2, h3, h4, h5 {
color: #ffcc00 !important; /* Brighter color for title and subtitles */
}
</style>
""",
unsafe_allow_html=True,
)
# Function to style the dataframe with black background and white text/borders
def style_dataframe(df, highlight_columns=None, highlight_rows=None):
# Apply comma formatting to numeric columns
df = df.applymap(lambda x: f"{x:,.0f}" if isinstance(x, (int, float)) else x)
# Style DataFrame with black background, white text, and yellow borders
styled_df = df.style.set_properties(
**{
'background-color': 'black',
'color': 'white',
'border-color': '#ffcc00', # Yellow border (same as title color)
'border-style': 'solid',
'border-width': '1px'
}
).set_table_styles(
[
{'selector': 'thead th', 'props': [('color', 'white'), ('font-weight', 'bold'), ('border-color', '#ffcc00')]},
{'selector': 'thead', 'props': [('border-color', '#ffcc00')]},
{'selector': 'td', 'props': [('color', 'white'), ('border-color', '#ffcc00')]}, # Yellow borders in body
]
)
# Set text within white background to black
styled_df = styled_df.set_properties(subset=df.columns, **{'background-color': 'white', 'color': 'black'})
# Highlight specific columns (e.g., dates) with black text
if highlight_columns:
styled_df = styled_df.set_properties(subset=highlight_columns, **{'color': 'black', 'background-color': 'white'})
# Highlight specific rows (e.g., KPIs like total_rev, ebitda, net_income) with black text
if highlight_rows:
for row in highlight_rows:
styled_df = styled_df.set_properties(subset=pd.IndexSlice[row, :], **{'color': 'black', 'background-color': 'white'})
return styled_df
# Title of the app
st.title("Fundamental Analysis Dashboard with LLM Insights")
# Placeholder for uploaded report
st.header("Upload Annual Report")
# File uploader for the annual report
uploaded_file = st.file_uploader("Choose an annual report (PDF format)", type="pdf")
# Define the query for LLM
query = "How has the performance been in this year compared to last year?"
# Check if a file has been uploaded
if uploaded_file is not None:
# Save the uploaded file locally
with open(uploaded_file.name, "wb") as f:
f.write(uploaded_file.getbuffer())
# Process the PDF and get the database (Chroma object)
db = process_pdf(uploaded_file.name)
# Use the LLM to search for relevant context
docs = db.similarity_search(query)
context = docs[0].page_content
# Extract company name from the uploaded file name
company_name = uploaded_file.name.split('.')[0]
# Display insights generated from LLM
insights = get_model_response(query, context)
st.subheader("Insights from Annual Report")
st.text_area("Report Insights", value=insights, height=180)
st.write(f"Analyzing the report for: {company_name}")
# Get the ticker symbol for the company
ticker = get_ticker(company_name)
if ticker:
st.write(f"Ticker Symbol: {ticker}")
# Get income statement and balance sheet data
st.header(f"Profit and Loss KPIs for {company_name} (Last 9 Years)")
income_statement_df = get_income_statement(ticker)
if income_statement_df is not None:
st.write("All figures are in millions.")
transposed_df = income_statement_df.set_index('dates').T # Transpose to make years as columns
# Create columns for layout
col1, col2 = st.columns([2, 1])
# Display income statement table in the left column with styled DataFrame
with col1:
st.dataframe(style_dataframe(transposed_df))
# Extract year from dates for graph
income_statement_df['dates'] = pd.to_datetime(income_statement_df['dates'])
income_statement_df['year'] = income_statement_df['dates'].dt.year
# Display net_income graph in the right column
with col2:
fig, ax = plt.subplots()
fig.patch.set_facecolor('black')
ax.set_facecolor('black')
ax.plot(income_statement_df['year'], income_statement_df['net_income'], color='cyan', marker='o')
ax.set_title('Net Income Over Years', color='#ffcc00') # Brighter color for the graph title
ax.set_ylabel('Net Income (millions)', color='white')
ax.set_xlabel('Year', color='white')
ax.tick_params(colors='white')
st.pyplot(fig)
else:
st.write("No income statement data available.")
st.header(f"Balance Sheet KPIs for {company_name} (Last 9 Years)")
balance_sheet_df = get_balance_sheet(ticker)
if balance_sheet_df is not None:
st.write("All figures are in millions.")
transposed_balance_sheet_df = balance_sheet_df.set_index('dates').T
# Create columns for layout
col1, col2 = st.columns([2, 1])
# Display balance sheet table in the left column with styled DataFrame
with col1:
st.dataframe(style_dataframe(transposed_balance_sheet_df))
# Extract year from dates for graph
balance_sheet_df['dates'] = pd.to_datetime(balance_sheet_df['dates'])
balance_sheet_df['year'] = balance_sheet_df['dates'].dt.year
# Display current_assets graph in the right column
with col2:
fig, ax = plt.subplots()
fig.patch.set_facecolor('black')
ax.set_facecolor('black')
ax.plot(balance_sheet_df['year'], balance_sheet_df['current_assets'], color='green', marker='o')
ax.set_title('Current Assets Over Years', color='#ffcc00') # Brighter color for the graph title
ax.set_ylabel('Current Assets (millions)', color='white')
ax.set_xlabel('Year', color='white')
ax.tick_params(colors='white')
st.pyplot(fig)
else:
st.write("No balance sheet data available.")
else:
st.write(f"Unable to retrieve ticker symbol for {company_name}.")
# Note or disclaimer
st.markdown("**Note:** Data is fetched from Alphavantage API based on the uploaded PDF file name.")
|