dindizz commited on
Commit
4ed4318
·
verified ·
1 Parent(s): 66e6411

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ iface = gr.Interface(fn=get_all_carbon_intensities,
57
+ inputs=[],
58
+ outputs=gr.DataFrame(),
59
+ title="Company Headquarters Carbon Intensity",
60
+ description="Get the latest carbon intensity for predefined company headquarters.",
61
+ footer="Made by Venkataraghavan\nEmail: [email protected]\nLinkedIn: https://www.linkedin.com/in/venkataraghavansrinivasan/")
62
+
63
+ iface.launch()