|
import requests |
|
import gradio as gr |
|
import os |
|
import pandas as pd |
|
|
|
|
|
locations = [ |
|
{"Company": "Apple", "Location": "Cupertino, California, USA", "Latitude": 37.3349, "Longitude": -122.0090}, |
|
{"Company": "Microsoft", "Location": "Redmond, Washington, USA", "Latitude": 47.6424, "Longitude": -122.1362}, |
|
{"Company": "NVIDIA", "Location": "Santa Clara, California, USA", "Latitude": 37.3706, "Longitude": -121.9669}, |
|
{"Company": "Alphabet (Google)", "Location": "Mountain View, California, USA", "Latitude": 37.4219, "Longitude": -122.0840}, |
|
{"Company": "Amazon", "Location": "Seattle, Washington, USA", "Latitude": 47.6062, "Longitude": -122.3321}, |
|
{"Company": "Saudi Aramco", "Location": "Dhahran, Eastern Province, Saudi Arabia", "Latitude": 26.3032, "Longitude": 50.1503}, |
|
{"Company": "Meta Platforms (Facebook)", "Location": "Menlo Park, California, USA", "Latitude": 37.4848, "Longitude": -122.1484}, |
|
{"Company": "TSMC", "Location": "Hsinchu, Taiwan", "Latitude": 24.7851, "Longitude": 121.0177}, |
|
{"Company": "Berkshire Hathaway", "Location": "Omaha, Nebraska, USA", "Latitude": 41.2565, "Longitude": -95.9345}, |
|
{"Company": "Eli Lilly", "Location": "Indianapolis, Indiana, USA", "Latitude": 39.7684, "Longitude": -86.1581}, |
|
{"Company": "Tesla", "Location": "Palo Alto, California, USA", "Latitude": 37.3947, "Longitude": -122.1503}, |
|
{"Company": "Broadcom", "Location": "San Jose, California, USA", "Latitude": 37.3382, "Longitude": -121.8863}, |
|
{"Company": "Novo Nordisk", "Location": "Bagsværd, Denmark", "Latitude": 55.7500, "Longitude": 12.4500}, |
|
{"Company": "JPMorgan Chase", "Location": "New York City, New York, USA", "Latitude": 40.7128, "Longitude": -74.0060}, |
|
{"Company": "Walmart", "Location": "Bentonville, Arkansas, USA", "Latitude": 36.3729, "Longitude": -94.2088} |
|
] |
|
|
|
|
|
def get_carbon_intensity(lat, lon): |
|
api_token = os.getenv('API_TOKEN') |
|
url = f'https://api.electricitymap.org/v3/carbon-intensity/latest?lat={lat}&lon={lon}' |
|
headers = { |
|
'auth-token': api_token |
|
} |
|
|
|
response = requests.get(url, headers=headers) |
|
|
|
if response.status_code == 200: |
|
data = response.json() |
|
carbon_intensity = data.get("carbonIntensity", "N/A") |
|
return carbon_intensity |
|
else: |
|
return f"Failed to retrieve data: {response.status_code}, {response.text}" |
|
|
|
|
|
def get_all_carbon_intensities(): |
|
results = [] |
|
for location in locations: |
|
lat = location["Latitude"] |
|
lon = location["Longitude"] |
|
carbon_intensity = get_carbon_intensity(lat, lon) |
|
location["Carbon Intensity"] = carbon_intensity |
|
results.append(location) |
|
|
|
df = pd.DataFrame(results) |
|
return df |
|
|
|
|
|
with gr.Blocks() as iface: |
|
gr.Markdown("# Company Headquarters Carbon Intensity") |
|
gr.Markdown("Get the latest carbon intensity for top market cap company headquarters.") |
|
output = gr.DataFrame() |
|
gr.Button("Get Carbon Intensities").click(get_all_carbon_intensities, outputs=output) |
|
gr.Markdown( |
|
""" |
|
### Made by Venkataraghavan |
|
- Email: [[email protected]](mailto:[email protected]) |
|
- LinkedIn: [Venkataraghavan Srinivasan](https://www.linkedin.com/in/venkataraghavansrinivasan/) |
|
""" |
|
) |
|
|
|
iface.launch() |
|
|