dindizz commited on
Commit
d1f82d4
·
verified ·
1 Parent(s): 7d8856c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -58
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
- # Predefined data for company headquarters and their latitude and longitude
7
- locations = [
8
- {"Company": "Apple", "Location": "Cupertino, California, USA", "Latitude": 37.3349, "Longitude": -122.0090},
9
- {"Company": "Microsoft", "Location": "Redmond, Washington, USA", "Latitude": 47.6424, "Longitude": -122.1362},
10
- {"Company": "NVIDIA", "Location": "Santa Clara, California, USA", "Latitude": 37.3706, "Longitude": -121.9669},
11
- {"Company": "Alphabet (Google)", "Location": "Mountain View, California, USA", "Latitude": 37.4219, "Longitude": -122.0840},
12
- {"Company": "Amazon", "Location": "Seattle, Washington, USA", "Latitude": 47.6062, "Longitude": -122.3321},
13
- {"Company": "Saudi Aramco", "Location": "Dhahran, Eastern Province, Saudi Arabia", "Latitude": 26.3032, "Longitude": 50.1503},
14
- {"Company": "Meta Platforms (Facebook)", "Location": "Menlo Park, California, USA", "Latitude": 37.4848, "Longitude": -122.1484},
15
- {"Company": "TSMC", "Location": "Hsinchu, Taiwan", "Latitude": 24.7851, "Longitude": 121.0177},
16
- {"Company": "Berkshire Hathaway", "Location": "Omaha, Nebraska, USA", "Latitude": 41.2565, "Longitude": -95.9345},
17
- {"Company": "Eli Lilly", "Location": "Indianapolis, Indiana, USA", "Latitude": 39.7684, "Longitude": -86.1581},
18
- {"Company": "Tesla", "Location": "Palo Alto, California, USA", "Latitude": 37.3947, "Longitude": -122.1503},
19
- {"Company": "Broadcom", "Location": "San Jose, California, USA", "Latitude": 37.3382, "Longitude": -121.8863},
20
- {"Company": "Novo Nordisk", "Location": "Bagsværd, Denmark", "Latitude": 55.7500, "Longitude": 12.4500},
21
- {"Company": "JPMorgan Chase", "Location": "New York City, New York, USA", "Latitude": 40.7128, "Longitude": -74.0060},
22
- {"Company": "Walmart", "Location": "Bentonville, Arkansas, USA", "Latitude": 36.3729, "Longitude": -94.2088}
23
- ]
 
 
 
24
 
25
- # Function to get carbon intensity for a specific latitude and longitude
26
- def get_carbon_intensity(lat, lon):
27
- api_token = os.getenv('API_TOKEN') # Get API token from environment variables
28
- url = f'https://api.electricitymap.org/v3/carbon-intensity/latest?lat={lat}&lon={lon}'
29
- headers = {
30
- 'auth-token': api_token
31
- }
32
-
33
- response = requests.get(url, headers=headers)
34
-
35
  if response.status_code == 200:
36
- data = response.json()
37
- carbon_intensity = data.get("carbonIntensity", "N/A")
38
- return carbon_intensity
39
  else:
40
- return f"Failed to retrieve data: {response.status_code}, {response.text}"
41
 
42
- # Function to handle multiple inputs
43
- def get_all_carbon_intensities():
44
  results = []
45
- for location in locations:
46
- lat = location["Latitude"]
47
- lon = location["Longitude"]
48
- carbon_intensity = get_carbon_intensity(lat, lon)
49
- location["Carbon Intensity"] = carbon_intensity
50
- results.append(location)
51
-
52
- df = pd.DataFrame(results)
53
- return df
 
 
 
 
 
 
 
54
 
55
- # Gradio interface
56
- with gr.Blocks() as iface:
57
- gr.Markdown("# Company Headquarters Carbon Intensity")
58
- gr.Markdown("Get the latest carbon intensity for top market cap company headquarters.")
59
- output = gr.DataFrame()
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
- iface.launch()
 
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()