Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,66 @@
|
|
|
|
1 |
import requests
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
-
import pandas as pd
|
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 |
-
headers = {
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
response = requests.get(url, headers=headers)
|
34 |
-
|
35 |
if response.status_code == 200:
|
36 |
-
|
37 |
-
carbon_intensity = data.get("carbonIntensity", "N/A")
|
38 |
-
return carbon_intensity
|
39 |
else:
|
40 |
-
return
|
41 |
|
42 |
-
#
|
43 |
-
def
|
44 |
results = []
|
45 |
-
for
|
46 |
-
lat =
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
gr.Button("Get Carbon Intensities").click(get_all_carbon_intensities, outputs=output)
|
61 |
-
gr.Markdown(
|
62 |
-
"""
|
63 |
-
### Made by Venkataraghavan
|
64 |
-
- Email: [[email protected]](mailto:[email protected])
|
65 |
-
- LinkedIn: [Venkataraghavan Srinivasan](https://www.linkedin.com/in/venkataraghavansrinivasan/)
|
66 |
-
"""
|
67 |
-
)
|
68 |
|
69 |
-
|
|
|
1 |
+
import pandas as pd
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
import os
|
|
|
5 |
|
6 |
+
# Constants
|
7 |
+
API_URL = 'https://api.electricitymap.org/v3/power-breakdown/latest'
|
8 |
+
API_TOKEN = os.getenv('ELECTRICITYMAP_API_TOKEN')
|
9 |
+
|
10 |
+
# Define the data for company headquarters
|
11 |
+
data = {
|
12 |
+
"Company": ["Apple", "Microsoft", "NVIDIA", "Alphabet (Google)", "Amazon", "Saudi Aramco",
|
13 |
+
"Meta Platforms (Facebook)", "TSMC", "Berkshire Hathaway", "Eli Lilly",
|
14 |
+
"Tesla", "Broadcom", "Novo Nordisk", "JPMorgan Chase", "Walmart"],
|
15 |
+
"Location": ["Cupertino, California, USA", "Redmond, Washington, USA", "Santa Clara, California, USA",
|
16 |
+
"Mountain View, California, USA", "Seattle, Washington, USA", "Dhahran, Eastern Province, Saudi Arabia",
|
17 |
+
"Menlo Park, California, USA", "Hsinchu, Taiwan", "Omaha, Nebraska, USA", "Indianapolis, Indiana, USA",
|
18 |
+
"Palo Alto, California, USA", "San Jose, California, USA", "Bagsværd, Denmark",
|
19 |
+
"New York City, New York, USA", "Bentonville, Arkansas, USA"],
|
20 |
+
"Latitude": [37.3349, 47.6424, 37.3706, 37.4219, 47.6062, 26.3032,
|
21 |
+
37.4848, 24.7851, 41.2565, 39.7684, 37.3947, 37.3382,
|
22 |
+
55.7500, 40.7128, 36.3729],
|
23 |
+
"Longitude": [-122.0090, -122.1362, -121.9669, -122.0840, -122.3321, 50.1503,
|
24 |
+
-122.1484, 121.0177, -95.9345, -86.1581, -122.1503, -121.8863,
|
25 |
+
12.4500, -74.0060, -94.2088]
|
26 |
+
}
|
27 |
|
28 |
+
df = pd.DataFrame(data)
|
29 |
+
|
30 |
+
# Function to make API request
|
31 |
+
def get_power_breakdown(lat, lon):
|
32 |
+
headers = {'auth-token': API_TOKEN}
|
33 |
+
params = {'lat': lat, 'lon': lon}
|
34 |
+
response = requests.get(API_URL, headers=headers, params=params)
|
|
|
|
|
|
|
35 |
if response.status_code == 200:
|
36 |
+
return response.json().get('fossilFreePercentage', 'N/A')
|
|
|
|
|
37 |
else:
|
38 |
+
return 'Error'
|
39 |
|
40 |
+
# Define the function to fetch fossil-free percentage for all companies
|
41 |
+
def fetch_fossil_free_percentages():
|
42 |
results = []
|
43 |
+
for index, row in df.iterrows():
|
44 |
+
lat, lon = row["Latitude"], row["Longitude"]
|
45 |
+
fossil_free_percentage = get_power_breakdown(lat, lon)
|
46 |
+
results.append({
|
47 |
+
"Company": row["Company"],
|
48 |
+
"Location": row["Location"],
|
49 |
+
"Latitude": row["Latitude"],
|
50 |
+
"Longitude": row["Longitude"],
|
51 |
+
"Fossil-Free Percentage": fossil_free_percentage
|
52 |
+
})
|
53 |
+
return pd.DataFrame(results)
|
54 |
+
|
55 |
+
# Define Gradio interface
|
56 |
+
def show_fossil_free_table():
|
57 |
+
result_df = fetch_fossil_free_percentages()
|
58 |
+
return result_df
|
59 |
|
60 |
+
gr_interface = gr.Interface(fn=show_fossil_free_table,
|
61 |
+
inputs=None,
|
62 |
+
outputs="dataframe",
|
63 |
+
title="Fossil-Free Percentage by Company Headquarters",
|
64 |
+
description="Click the button below to see the latest fossil-free energy usage percentages for all company headquarters.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
gr_interface.launch()
|