Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Predefined data for Shell offices and their latitude and longitude
|
| 7 |
+
locations = [
|
| 8 |
+
{"Office": "Shell Centre", "Location": "London, United Kingdom", "Latitude": 51.5074, "Longitude": -0.1278},
|
| 9 |
+
{"Office": "Shell House", "Location": "Singapore", "Latitude": 1.3521, "Longitude": 103.8198},
|
| 10 |
+
{"Office": "Shell Technology Center", "Location": "Houston, Texas, USA", "Latitude": 29.7604, "Longitude": -95.3698},
|
| 11 |
+
{"Office": "Shell India Markets Pvt Ltd", "Location": "Bengaluru, India", "Latitude": 12.9716, "Longitude": 77.5946},
|
| 12 |
+
{"Office": "Shell Australia", "Location": "Perth, Australia", "Latitude": -31.9505, "Longitude": 115.8605},
|
| 13 |
+
{"Office": "Shell Canada", "Location": "Calgary, Alberta, Canada", "Latitude": 51.0447, "Longitude": -114.0719},
|
| 14 |
+
{"Office": "Shell Argentina", "Location": "Buenos Aires, Argentina", "Latitude": -34.6037, "Longitude": -58.3816},
|
| 15 |
+
{"Office": "Shell Nigeria", "Location": "Lagos, Nigeria", "Latitude": 6.5244, "Longitude": 3.3792},
|
| 16 |
+
{"Office": "Shell China", "Location": "Beijing, China", "Latitude": 39.9042, "Longitude": 116.4074},
|
| 17 |
+
{"Office": "Shell Russia", "Location": "Moscow, Russia", "Latitude": 55.7558, "Longitude": 37.6173},
|
| 18 |
+
{"Office": "Shell Germany", "Location": "Hamburg, Germany", "Latitude": 53.5511, "Longitude": 9.9937},
|
| 19 |
+
{"Office": "Shell Netherlands", "Location": "The Hague, Netherlands", "Latitude": 52.0705, "Longitude": 4.3007},
|
| 20 |
+
{"Office": "Shell South Africa", "Location": "Cape Town, South Africa", "Latitude": -33.9249, "Longitude": 18.4241},
|
| 21 |
+
{"Office": "Shell Mexico", "Location": "Mexico City, Mexico", "Latitude": 19.4326, "Longitude": -99.1332},
|
| 22 |
+
{"Office": "Shell Brazil", "Location": "Rio de Janeiro, Brazil", "Latitude": -22.9068, "Longitude": -43.1729}
|
| 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("# Shell Offices Carbon Intensity")
|
| 58 |
+
gr.Markdown("Get the latest carbon intensity for various Shell office locations globally.")
|
| 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: [venkat.srinivasan@gbs-alumni.com](mailto:venkat.srinivasan@gbs-alumni.com)
|
| 65 |
+
- LinkedIn: [Venkataraghavan Srinivasan](https://www.linkedin.com/in/venkataraghavansrinivasan/)
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
iface.launch()
|