-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
143 lines (114 loc) · 4.67 KB
/
ui.py
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
from typing import Dict
import pandas as pd
import plotly.express as px
import streamlit as st
from altair import List
from constants import DATA_DISPLAY_MODES, DEFAULT_ITEMS, DISCLAIMER, MODES, SECTIONS
from generate import CompanyFinancials, summarize_filing_document
from utils import CompanyFilingTexts
def setup_sidebar(tickers: List[str]) -> Dict[str, str]:
"""Setup the sidebar layout, including title, notes, settings, etc..
Includes setup for additional menu options for the "Analysis" mode.
Returns a dictionary containing all selected options.
"""
st.sidebar.title("LLM + 10-K")
st.sidebar.subheader("_Analysis of 10-K Filings with the power of LLM's_")
st.sidebar.markdown("##### _By Richard So_")
st.sidebar.markdown(DISCLAIMER)
st.sidebar.markdown("---")
st.sidebar.markdown("## Display Options")
res = {}
res["ticker"] = st.sidebar.selectbox("Company/Ticker", tickers)
mode_option = st.sidebar.radio("Mode", MODES)
res["mode"] = mode_option
# show extra options for "Analyze" mode
if mode_option == "Analyze":
# graph or table view
res["view"] = st.sidebar.radio("Data View", DATA_DISPLAY_MODES)
# edit financial items for analysis
expander = st.sidebar.expander("Advanced Settings", expanded=False)
res["fin_items"] = expander.text_area(
"Financial Items (newline-separated)", "\n".join(DEFAULT_ITEMS)
)
# rerun/reset button
res["reset_findata_cache"] = st.sidebar.button(
"**Rerun LLM & Reset Data Cache**",
type="primary",
help="WARNING: This will take a (long) while!",
use_container_width=True,
)
else:
res["view"] = None
return res
def display_financial_data(
financials: CompanyFinancials, ticker: str, table: bool = False
) -> None:
"""Display financial data as either a graph or a table."""
# convert CompanyFinancials dictionary into dataframe
graph_data = pd.DataFrame.from_dict(financials, orient="index")
# sort columns for consistency, and get column title names
chart_options = sorted(graph_data.columns.tolist())
chart_option_names = [opt.replace("_", " ").title() for opt in chart_options]
# turn year index into column for plotly
graph_data.index.name = "year"
graph_data.reset_index(inplace=True)
if table: # display as a table instead
# rename df columns
graph_data.columns = ["Year"] + chart_option_names
# get rid of commas in year value
graph_data["Year"] = graph_data["Year"].astype(str)
# display table
st.dataframe(graph_data, use_container_width=True, hide_index=True, height=600)
return
# display as a graph
for chart_option, chart_option_name in zip(chart_options, chart_option_names):
line = px.line(
graph_data,
x="year",
y=chart_option,
title=f"{ticker} {chart_option_name}",
labels={chart_option: f"{chart_option_name} (USD)", "year": "Year"},
markers=True,
)
st.plotly_chart(line, use_container_width=True)
def display_summarizer(filings: CompanyFilingTexts, stream: bool = True) -> None:
"""Display the summarizer interface for 10-K filings."""
summarizer_options = st.form(key="summarizer_options")
c1, c2 = summarizer_options.columns(2)
section_name = c1.selectbox("Section", ["Business", "Risk", "MD&A", "Financials"])
if not section_name:
section_name = "Business"
year = c2.selectbox("Year", list(filings.keys()))
if not year:
year = list(filings.keys())[0]
c3, c4 = summarizer_options.columns([0.8, 0.2])
override_instr = c3.text_input(
"Override Instructions (Optional)",
placeholder="Answer using bullet points. Use 1-2 paragraphs. etc...",
)
c4.write("####") # offset the button lower to align with the text input
submitted = c4.form_submit_button(
"Summarize",
help="Summarize the selected section and year of the 10-K filing.",
use_container_width=True,
)
if not submitted:
return
section_code = SECTIONS[section_name]
document = filings[year][section_code]
if not document:
# section does not exist for the filing (e.g. no "Risk" section in earlier 10-K filings)
st.error(f"No {section_name} section found for the filing year of {year}.")
return
gen_res = summarize_filing_document(
document,
section_name,
filings.ticker,
year,
override_instr=override_instr,
stream=stream,
)
if stream:
st.write_stream(gen_res)
else:
st.write(gen_res)