-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyesg1.py
More file actions
318 lines (256 loc) · 13.8 KB
/
yesg1.py
File metadata and controls
318 lines (256 loc) · 13.8 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import streamlit as st
import pandas as pd
import yesg
import plotly.graph_objects as go
import plotly.express as px
from streamlit_lottie import st_lottie_spinner
import json
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate
import os
from dotenv import load_dotenv
# Load variables from .env file
load_dotenv()
def loadLottieFile(filePath: str):
with open(file=filePath, mode = "r") as f:
return json.load(f)
buildingDatabase = loadLottieFile("ESG.json")
# os.environ["TOKENIZERS_PARALLELISM"] = "false"
class ESGSummaryGenerator:
def __init__(self, model_name):
self.model_name = model_name
self.groq_api_key = os.getenv("GROQ_API_KEY")
self.chain = None
self.generate_summary_chain()
def generate_summary_chain(self):
self.llm = ChatGroq(groq_api_key=self.groq_api_key, model_name=self.model_name)
self.prompt_template = ChatPromptTemplate.from_template("""
You are an expert in Environmental, Social and Governance (ESG) analysis.
You are Given the following ESG data for a ticker (or a company).
Just for your context, The Data is from 2014 to up until November 2019, and higher the score, more sustainable the companies are!
For the ticker, summarize the key findings and critically justify those with logical reasonings.
Utilize whatever knowledge you have about the ticker (or the company) and justify why they've been given such scores and what might be the reasons for that.
for example- which particular key issues in Environment or Social Sector were material to those companies,
what do you think about why they might've got such socre on each key issue under different themes in each pillar E, S and G (as per MSCI standards of ESG rating),
which different domains and its activities might be resulting in high or low E, S or G Scores
(lets say if its a retail company, logistics and transportation might be resulting in high carbon emissions
or if its a cement manufacturing company, their production lines might result in high emissions
or if its tech company, their data centers might be producing a lot of heat),
think like this for all different key issues which might be material to this particular company.
Present the output as HTML (with each ticker name as heading and its corresponding information as appropriate text).
Keep the response to the point and short! Do not fabricate anything.
Provide only the response as asked of you and no added human like comments like "Here is my response" or anything of that sort.
<data>
{data}
</data>
""")
def generate_summary(self, esg_dataframe):
summaries = {}
for ticker in esg_dataframe['ticker'].unique():
ticker_data = esg_dataframe[esg_dataframe['ticker'] == ticker]
context = ticker_data.to_html(index=False)
prompt = self.prompt_template.format(data=context)
response = self.llm.invoke(prompt)
summaries[ticker] = f"<h2>{ticker}</h2> {response.content}"
return summaries
st.set_page_config(layout="wide",
page_title="ESG Analysis")
# page_icon="Portfolio_Icon.jpg")
# List of top tickers
tickers_dict = {
"MSFT": "Microsoft Corporation",
"AAPL": "Apple Inc.",
"NVDA": "NVIDIA Corporation",
# "GOOG": "Alphabet Inc.",
"AMZN": "Amazon.com, Inc.",
# "META": "Meta Platforms, Inc.",
"TSLA": "Tesla, Inc.",
"JPM": "JPMorgan Chase & Co.",
"WMT": "Walmart Inc.",
"XOM": "Exxon Mobil Corporation",
"PG": "The Procter & Gamble Company",
"JNJ": "Johnson & Johnson",
"HD": "The Home Depot, Inc.",
"BAC": "Bank of America Corporation",
"NFLX": "Netflix, Inc.",
"AMD": "Advanced Micro Devices, Inc.",
"KO": "The Coca-Cola Company",
"CSCO": "Cisco Systems, Inc.",
"MCD": "McDonald's Corporation",
"DIS": "The Walt Disney Company",
"BABA": "Alibaba Group Holding Limited",
"GE": "GE Aerospace",
"VZ": "Verizon Communications Inc.",
"IBM": "International Business Machines Corporation",
"PFE": "Pfizer Inc.",
"INTC": "Intel Corporation",
"T": "AT&T Inc.",
"C": "Citigroup Inc.",
"BA": "The Boeing Company",
"F": "Ford Motor Company"
}
# Function to fetch ESG data for multiple tickers
def get_multiple_esg_full(tickers):
print(tickers)
esg_data = []
for ticker in tickers:
try:
data = yesg.get_historic_esg(ticker=ticker)
data['ticker'] = ticker # Add ticker to the data
data = data[data.index < '2019-11-01'] # Filter data before November 2019
esg_data.append(data)
except Exception as e:
print(f"Error retrieving data for {ticker}: {e}")
return pd.concat(esg_data).fillna(0)
# Streamlit app
st.title('Interactive ESG Dashboard')
# Sidebar for user input
st.sidebar.header('User Input')
selected_tickers = st.sidebar.multiselect('Select companies', list(tickers_dict.keys()), default=[],key="selectedTickers")
parameters = st.sidebar.multiselect('Select ESG parameters', ['Environmental', 'Social', 'Governance'], default=[], key="selectedParameteres")
# Form to submit selection
with st.sidebar.form(key='form1'):
submit_button = st.form_submit_button(label='Submit')
if submit_button:
with st_lottie_spinner(buildingDatabase , height=200):
# Fetch ESG data for selected tickers
esg_df = get_multiple_esg_full(selected_tickers)
esg_summary_generator = ESGSummaryGenerator(model_name="llama3-8b-8192")
summary_html = esg_summary_generator.generate_summary(esg_df)
st.header("Key Findings (Gen AI)")
st.subheader("Headstart for Brainstroming")
# st.markdown(summary_html, unsafe_allow_html=True)
listOfTickers = list(summary_html.keys())
tabsOfTickers = st.tabs(listOfTickers)
for tck, tabTicker in zip(listOfTickers, tabsOfTickers):
with tabTicker:
st.markdown(summary_html[tck], unsafe_allow_html=True)
st.divider()
st.divider()
st.header("E, S and G Score!")
e, s, g = st.tabs(['Environmental', 'Social', 'Governance'])
with e:
# Filter data based on selected parameters
if 'Environmental' in parameters:
st.subheader('Environmental Scores')
fig_env = go.Figure()
for ticker in selected_tickers:
company_data = esg_df[esg_df['ticker'] == ticker]
fig_env.add_trace(go.Scatter(x=company_data.index, y=company_data['E-Score'], mode='lines+markers', name=tickers_dict[ticker],
line=dict(width=2), marker=dict(size=6)))
fig_env.update_layout(title='Environmental Scores Over Time', xaxis_title='Date', yaxis_title='E-Score',
template='plotly_dark',
title_font=dict(size=24, family='Arial, sans-serif', color='white'),
font=dict(family='Arial, sans-serif', color='white'))
st.plotly_chart(fig_env, use_container_width=True)
with s:
if 'Social' in parameters:
st.subheader('Social Scores')
fig_soc = go.Figure()
for ticker in selected_tickers:
company_data = esg_df[esg_df['ticker'] == ticker]
fig_soc.add_trace(go.Scatter(x=company_data.index, y=company_data['S-Score'], mode='lines+markers', name=tickers_dict[ticker],
line=dict(width=2), marker=dict(size=6)))
fig_soc.update_layout(title='Social Scores Over Time', xaxis_title='Date', yaxis_title='S-Score',
template='plotly_dark',
title_font=dict(size=24, family='Arial, sans-serif', color='white'),
font=dict(family='Arial, sans-serif', color='white'))
st.plotly_chart(fig_soc, use_container_width=True)
with g:
if 'Governance' in parameters:
st.subheader('Governance Scores')
fig_gov = go.Figure()
for ticker in selected_tickers:
company_data = esg_df[esg_df['ticker'] == ticker]
fig_gov.add_trace(go.Scatter(x=company_data.index, y=company_data['G-Score'], mode='lines+markers', name=tickers_dict[ticker],
line=dict(width=2),
marker=dict(size=6))
)
fig_gov.update_layout(title='Governance Scores Over Time', xaxis_title='Date', yaxis_title='G-Score',
# template='plotly_dark',
title_font=dict(size=24, family='Arial, sans-serif', color='white'),
font=dict(family='Arial, sans-serif', color='white'))
st.plotly_chart(fig_gov, use_container_width=True)
cl1, cl2, cl3 = st.columns((2,2,2))
# Additional visualizations and summaries
cl1.subheader('Summary Statistics')
summary_stats = esg_df.groupby('ticker')[['E-Score', 'S-Score', 'G-Score']].mean().reset_index()
summary_stats.columns = ['Ticker', 'Avg Environmental Score', 'Avg Social Score', 'Avg Governance Score']
cl1.write(summary_stats)
cl2.subheader('Correlation Matrix')
correlation_matrix = esg_df[['E-Score', 'S-Score', 'G-Score']].corr()
cl2.write(correlation_matrix)
# Enhancing the dashboard with attractive visualizations
cl3.subheader('Interactive ESG Comparison')
cl3.vega_lite_chart(esg_df, {
'mark': {'type': 'bar', 'tooltip': True},
'encoding': {
'x': {'field': 'ticker', 'type': 'nominal'},
'y': {'field': 'value', 'type': 'quantitative'},
'color': {'field': 'variable', 'type': 'nominal'},
'tooltip': [{'field': 'ticker', 'type': 'nominal'}, {'field': 'value', 'type': 'quantitative'}]
},
'transform': [
{'fold': ['E-Score', 'S-Score', 'G-Score']}
]
})
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
st.subheader('A radar chart can show the ESG scores of each company across different dimensions (E, S, G).')
# Filter data before November 2019
esg_df_filtered = esg_df.loc[esg_df.index < '2019-11-01']
# Create radar chart
fig_radar = go.Figure()
for ticker in selected_tickers:
company_data = esg_df_filtered[esg_df_filtered['ticker'] == ticker]
fig_radar.add_trace(go.Scatterpolar(
r=[company_data['E-Score'].mean(), company_data['S-Score'].mean(), company_data['G-Score'].mean()],
theta=['E', 'S', 'G'],
fill='toself',
name=tickers_dict[ticker]
))
fig_radar.update_layout(
title='ESG Radar Chart',
polar=dict(
radialaxis=dict(visible=True, range=[0, 100])
),
showlegend=True,
title_font=dict(size=24, family='Arial, sans-serif', color='black'),
font=dict(family='Arial, sans-serif', color='black'),
plot_bgcolor='#F0F0F0', # Light gray background
paper_bgcolor='#F0F0F0' # Light gray background
)
st.plotly_chart(fig_radar, use_container_width=True)
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
st.subheader('BUBBLE CHARTS 3 Dimensional')
# Create 3D Bubble Chart with time animation
fig = px.scatter_3d(esg_df, x='E-Score', y='S-Score', z='G-Score', color='ticker', size='E-Score',
labels={'E-Score': 'Environmental Score', 'S-Score': 'Social Score', 'G-Score': 'Governance Score'},
title='3D Bubble Chart of ESG Scores with Time',
hover_name='ticker',
animation_frame=esg_df.index.strftime('%Y-%m-%d'),
size_max=25,
height=1000)
# Update layout
fig.update_layout(
scene=dict(
xaxis_title='Environmental Score',
yaxis_title='Social Score',
zaxis_title='Governance Score',
),
title_font=dict(size=24, family='Arial, sans-serif', color='black'),
font=dict(family='Arial, sans-serif', color='black'),
plot_bgcolor='#F0F0F0', # Light gray background
paper_bgcolor='#F0F0F0' # Light gray background
)
# Show the chart
st.plotly_chart(fig, use_container_width=True)
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
st.subheader('A pie chart can visually represent the proportion of each ESG score type across all selected companies.')
total_scores = esg_df[['E-Score', 'S-Score', 'G-Score']].sum()
fig_pie = go.Figure(data=[go.Pie(labels=['Environmental', 'Social', 'Governance'], values=total_scores,
hole=0.3, pull=[0.05, 0.05, 0.05])])
fig_pie.update_layout(title='Proportion of ESG Scores Across Companies',
title_font=dict(size=24, family='Arial, sans-serif', color='black'),
font=dict(family='Arial, sans-serif', color='black'),
plot_bgcolor='#F0F0F0', paper_bgcolor='#F0F0F0') # Light gray background
st.plotly_chart(fig_pie, use_container_width=True)