Spaces:
Sleeping
Sleeping
Add Google Finance
Browse files- app.py +100 -61
- flagged/log.csv +0 -9
app.py
CHANGED
@@ -1,71 +1,110 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
import gradio as gr
|
3 |
-
import gradio as gr; print(gr.__version__)
|
4 |
-
|
5 |
-
# Replace with path to your ESG data (CSV or other supported format)
|
6 |
-
data_path = "ESG_data.csv"
|
7 |
-
company_ratings = [
|
8 |
-
{"Company Name": "Apple Inc.", "Rating": 4.5},
|
9 |
-
{"Company Name": "Amazon.com, Inc.", "Rating": 4.2},
|
10 |
-
{"Company Name": "Microsoft Corporation", "Rating": 4.7},
|
11 |
-
{"Company Name": "Alphabet Inc. (Google)", "Rating": 4.8},
|
12 |
-
{"Company Name": "Tesla, Inc.", "Rating": 3.9},
|
13 |
-
{"Company Name": "Meta Platforms Inc. (Facebook)", "Rating": 3.1},
|
14 |
-
]
|
15 |
-
|
16 |
-
# Load ESG data
|
17 |
-
esg_data = pd.DataFrame(company_ratings)
|
18 |
import gradio as gr
|
19 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
# Returns:
|
41 |
-
# pandas.DataFrame: Subset of ESG data for the ticker,
|
42 |
-
# containing ESG scores if found, or an empty DataFrame
|
43 |
-
# if not found.
|
44 |
-
# """
|
45 |
-
# filtered_data = esg_data[esg_data["Ticker Symbol"] == ticker.upper()]
|
46 |
-
# return filtered_data if not filtered_data.empty else pd.DataFrame()
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
# dataframe (pandas.DataFrame): DataFrame containing ESG scores.
|
55 |
-
# """
|
56 |
-
# if dataframe.empty:
|
57 |
-
# return "No ESG data found for this ticker."
|
58 |
-
# else:
|
59 |
-
# # Select relevant ESG score columns (adjust based on your data)
|
60 |
-
# esg_scores = dataframe[["Ticker Symbol", "Governance Score", "Social Score", "Environmental Score"]]
|
61 |
-
# return gr.DataTable(dataframe=esg_scores.to_dict())
|
62 |
|
63 |
-
# iface = gr.Interface(
|
64 |
-
# fn=get_esg_scores,
|
65 |
-
# inputs=gr.inputs.Textbox(label="Ticker Symbol"),
|
66 |
-
# outputs=esg_data,
|
67 |
-
# title="ESG Score Lookup",
|
68 |
-
# description="Enter a company ticker symbol to view its ESG scores (if available).",
|
69 |
-
# )
|
70 |
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import requests
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
|
6 |
+
example_tickers = [
|
7 |
+
{"Ticker": "PLD", "Company Name": "Prologis Inc"},
|
8 |
+
{"Ticker": "PSA", "Company Name": "Public Storage"},
|
9 |
+
{"Ticker": "O", "Company Name": "Realty Income Corp"},
|
10 |
+
{
|
11 |
+
"Ticker": "META",
|
12 |
+
"Company Name": "Meta Platforms",
|
13 |
+
},
|
14 |
+
{"Ticker": "AMZN", "Company Name": "Amazon.com"},
|
15 |
+
{"Ticker": "MSFT", "Company Name": "Microsoft Corporation"},
|
16 |
+
]
|
17 |
|
18 |
|
19 |
+
def get_esg_from_yahoo_finance(row):
|
20 |
+
elements = []
|
21 |
+
# This is a standard user-agent of Chrome browser running on Windows 10
|
22 |
+
headers = {
|
23 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
|
24 |
+
}
|
25 |
+
html = requests.get(
|
26 |
+
"https://finance.yahoo.com/quote/" + row.Ticker + "/sustainability",
|
27 |
+
headers=headers,
|
28 |
+
).text
|
29 |
+
soup = BeautifulSoup(html, "html.parser")
|
30 |
+
|
31 |
+
scores = soup.find_all("div", {"class": "content svelte-y3c2sq"})
|
32 |
+
for score in scores:
|
33 |
+
elements.append(float(score.find("h4").text.strip()))
|
34 |
+
if elements:
|
35 |
+
row["Total ESG Risk Score"] = elements[0]
|
36 |
+
row["Environmental Risk Score"] = elements[1]
|
37 |
+
row["Social Risk Score"] = elements[2]
|
38 |
+
row["Governance Risk Score"] = elements[3]
|
39 |
+
else:
|
40 |
+
row["Total ESG Risk Score"] = None
|
41 |
+
row["Environmental Risk Score"] = None
|
42 |
+
row["Social Risk Score"] = None
|
43 |
+
row["Governance Risk Score"] = None
|
44 |
+
return row
|
45 |
+
|
46 |
+
|
47 |
+
def get_esg_score_from_google_finance(row):
|
48 |
+
headers = {
|
49 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36"
|
50 |
+
}
|
51 |
+
|
52 |
+
html = requests.get(
|
53 |
+
f"https://www.google.com/finance/quote/META:NASDAQ", headers=headers, timeout=30
|
54 |
+
).text
|
55 |
+
soup = BeautifulSoup(html, "html.parser")
|
56 |
+
scores = soup.find_all("div", {"class": "IPIeJ"})
|
57 |
+
|
58 |
+
row["CDP Score"] = scores[0].find("div", {"class": "P6K39c"}).text
|
59 |
+
|
60 |
+
return row
|
61 |
+
|
62 |
+
|
63 |
+
example_input_data = pd.DataFrame(example_tickers)
|
64 |
+
|
65 |
+
|
66 |
+
inputs = [
|
67 |
+
gr.Dataframe(
|
68 |
+
row_count=(6, "dynamic"),
|
69 |
+
col_count=(1, "dynamic"),
|
70 |
+
label="Input Data",
|
71 |
+
interactive=1,
|
72 |
+
)
|
73 |
+
]
|
74 |
|
75 |
+
outputs = [
|
76 |
+
gr.Dataframe(
|
77 |
+
row_count=(6, "dynamic"),
|
78 |
+
col_count=(7, "fixed"),
|
79 |
+
label="ESG Scores",
|
80 |
+
headers=[
|
81 |
+
"Ticker",
|
82 |
+
"Company Name",
|
83 |
+
"Total ESG Risk Score",
|
84 |
+
"Environmental Risk Score",
|
85 |
+
"Social Risk Score",
|
86 |
+
"Governance Risk Score",
|
87 |
+
"CDP Score",
|
88 |
+
],
|
89 |
+
)
|
90 |
+
]
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
def get_esg_scores(input_dataframe):
|
94 |
+
input_dataframe = input_dataframe.apply(
|
95 |
+
lambda x: get_esg_from_yahoo_finance(x), axis=1
|
96 |
+
)
|
97 |
+
input_dataframe = input_dataframe.apply(
|
98 |
+
lambda x: get_esg_score_from_google_finance(x), axis=1
|
99 |
+
)
|
100 |
|
101 |
+
return input_dataframe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
gr.Interface(
|
105 |
+
fn=get_esg_scores,
|
106 |
+
inputs=inputs,
|
107 |
+
outputs=outputs,
|
108 |
+
title="🌳ESG Data Scraper🌳\n\nIt scrapes ESG ratings from Yahoo Finance and Google Finance!",
|
109 |
+
examples=[[example_input_data.head(6)]],
|
110 |
+
).launch()
|
flagged/log.csv
DELETED
@@ -1,9 +0,0 @@
|
|
1 |
-
name,Output,timestamp
|
2 |
-
,Hello !!,2024-05-20 02:22:21.273157
|
3 |
-
,Hello !!,2024-05-20 02:22:22.248193
|
4 |
-
,Hello !!,2024-05-20 02:22:23.235456
|
5 |
-
,Hello !!,2024-05-20 02:22:23.675012
|
6 |
-
,Hello !!,2024-05-20 02:22:27.643848
|
7 |
-
,Hello !!,2024-05-20 02:22:28.585934
|
8 |
-
,Hello !!,2024-05-20 02:22:29.303183
|
9 |
-
,Hello !!,2024-05-20 02:22:30.054843
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|