Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,63 +4,89 @@ import gradio as gr
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Constants
|
| 7 |
-
API_URL = 'https://api.electricitymap.org/v3/
|
| 8 |
API_TOKEN = os.getenv('ELECTRICITYMAP_API_TOKEN')
|
| 9 |
|
| 10 |
-
# Define the data for company headquarters
|
| 11 |
data = {
|
| 12 |
-
"Company": ["
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
df = pd.DataFrame(data)
|
| 29 |
|
| 30 |
# Function to make API request
|
| 31 |
-
def
|
| 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('
|
| 37 |
else:
|
| 38 |
return 'Error'
|
| 39 |
|
| 40 |
-
# Define the function to fetch
|
| 41 |
-
def
|
| 42 |
results = []
|
| 43 |
for index, row in df.iterrows():
|
| 44 |
lat, lon = row["Latitude"], row["Longitude"]
|
| 45 |
-
|
| 46 |
results.append({
|
| 47 |
"Company": row["Company"],
|
| 48 |
"Location": row["Location"],
|
| 49 |
"Latitude": row["Latitude"],
|
| 50 |
"Longitude": row["Longitude"],
|
| 51 |
-
"
|
| 52 |
})
|
| 53 |
return pd.DataFrame(results)
|
| 54 |
|
| 55 |
# Define Gradio interface
|
| 56 |
-
def
|
| 57 |
-
result_df =
|
| 58 |
return result_df
|
| 59 |
|
| 60 |
-
gr_interface = gr.Interface(fn=
|
| 61 |
inputs=None,
|
| 62 |
outputs="dataframe",
|
| 63 |
-
title="
|
| 64 |
-
description="Click the button below to see the latest
|
| 65 |
|
| 66 |
gr_interface.launch()
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Constants
|
| 7 |
+
API_URL = 'https://api.electricitymap.org/v3/carbon-intensity/latest'
|
| 8 |
API_TOKEN = os.getenv('ELECTRICITYMAP_API_TOKEN')
|
| 9 |
|
| 10 |
+
# Define the data for NIFTY 50 company headquarters
|
| 11 |
data = {
|
| 12 |
+
"Company": ["Reliance Industries", "Tata Consultancy Services", "HDFC Bank", "Infosys", "Hindustan Unilever",
|
| 13 |
+
"ICICI Bank", "Kotak Mahindra Bank", "SBI", "Bharti Airtel", "HCL Technologies",
|
| 14 |
+
"Asian Paints", "Bajaj Finance", "ITC", "Adani Green Energy", "Axis Bank",
|
| 15 |
+
"Maruti Suzuki", "Larsen & Toubro", "Tata Motors", "Sun Pharma", "HDFC",
|
| 16 |
+
"Wipro", "UltraTech Cement", "Nestle India", "Adani Ports", "Bajaj Finserv",
|
| 17 |
+
"Titan Company", "Grasim Industries", "Mahindra & Mahindra", "Tech Mahindra", "Divi's Laboratories",
|
| 18 |
+
"JSW Steel", "Power Grid Corporation", "Tata Steel", "Britannia Industries", "Hindalco Industries",
|
| 19 |
+
"Eicher Motors", "Dr. Reddy's Laboratories", "Tata Consumer Products", "HDFC Life", "SBI Life Insurance",
|
| 20 |
+
"Hero MotoCorp", "Coal India", "Indian Oil Corporation", "Oil and Natural Gas Corporation", "Bharat Petroleum",
|
| 21 |
+
"Cipla", "Shree Cement", "Bajaj Auto", "IndusInd Bank", "NTPC"],
|
| 22 |
+
"Location": ["Mumbai, Maharashtra", "Mumbai, Maharashtra", "Mumbai, Maharashtra", "Bengaluru, Karnataka", "Mumbai, Maharashtra",
|
| 23 |
+
"Mumbai, Maharashtra", "Mumbai, Maharashtra", "Mumbai, Maharashtra", "New Delhi, Delhi", "Noida, Uttar Pradesh",
|
| 24 |
+
"Mumbai, Maharashtra", "Pune, Maharashtra", "Kolkata, West Bengal", "Ahmedabad, Gujarat", "Mumbai, Maharashtra",
|
| 25 |
+
"New Delhi, Delhi", "Mumbai, Maharashtra", "Mumbai, Maharashtra", "Mumbai, Maharashtra", "Mumbai, Maharashtra",
|
| 26 |
+
"Bengaluru, Karnataka", "Mumbai, Maharashtra", "Gurugram, Haryana", "Ahmedabad, Gujarat", "Pune, Maharashtra",
|
| 27 |
+
"Bengaluru, Karnataka", "Nagda, Madhya Pradesh", "Mumbai, Maharashtra", "Pune, Maharashtra", "Hyderabad, Telangana",
|
| 28 |
+
"Mumbai, Maharashtra", "New Delhi, Delhi", "Mumbai, Maharashtra", "Bengaluru, Karnataka", "Mumbai, Maharashtra",
|
| 29 |
+
"Gurugram, Haryana", "Hyderabad, Telangana", "Mumbai, Maharashtra", "Mumbai, Maharashtra", "Mumbai, Maharashtra",
|
| 30 |
+
"New Delhi, Delhi", "Kolkata, West Bengal", "New Delhi, Delhi", "New Delhi, Delhi", "Mumbai, Maharashtra",
|
| 31 |
+
"Mumbai, Maharashtra", "Kolkata, West Bengal", "Pune, Maharashtra", "Mumbai, Maharashtra", "New Delhi, Delhi"],
|
| 32 |
+
"Latitude": [19.0760, 19.0760, 19.0760, 12.9716, 19.0760,
|
| 33 |
+
19.0760, 19.0760, 19.0760, 28.6139, 28.5355,
|
| 34 |
+
19.0760, 18.5204, 22.5726, 23.0225, 19.0760,
|
| 35 |
+
28.6139, 19.0760, 19.0760, 19.0760, 19.0760,
|
| 36 |
+
12.9716, 19.0760, 28.4595, 23.0225, 18.5204,
|
| 37 |
+
12.9716, 23.5330, 19.0760, 18.5204, 17.3850,
|
| 38 |
+
19.0760, 28.6139, 19.0760, 12.9716, 19.0760,
|
| 39 |
+
28.4595, 17.3850, 19.0760, 19.0760, 19.0760,
|
| 40 |
+
28.6139, 22.5726, 28.6139, 28.6139, 19.0760,
|
| 41 |
+
19.0760, 22.5726, 18.5204, 19.0760, 28.6139],
|
| 42 |
+
"Longitude": [72.8777, 72.8777, 72.8777, 77.5946, 72.8777,
|
| 43 |
+
72.8777, 72.8777, 72.8777, 77.2090, 77.3910,
|
| 44 |
+
72.8777, 73.8567, 88.3639, 72.5714, 72.8777,
|
| 45 |
+
77.2090, 72.8777, 72.8777, 72.8777, 72.8777,
|
| 46 |
+
77.5946, 72.8777, 77.0266, 72.5714, 73.8567,
|
| 47 |
+
77.5946, 75.4254, 72.8777, 73.8567, 78.4867,
|
| 48 |
+
72.8777, 77.2090, 72.8777, 77.5946, 72.8777,
|
| 49 |
+
77.0266, 78.4867, 72.8777, 72.8777, 72.8777,
|
| 50 |
+
77.2090, 88.3639, 77.2090, 77.2090, 72.8777,
|
| 51 |
+
72.8777, 88.3639, 73.8567, 72.8777, 77.2090]
|
| 52 |
}
|
| 53 |
|
| 54 |
df = pd.DataFrame(data)
|
| 55 |
|
| 56 |
# Function to make API request
|
| 57 |
+
def get_carbon_intensity(lat, lon):
|
| 58 |
headers = {'auth-token': API_TOKEN}
|
| 59 |
params = {'lat': lat, 'lon': lon}
|
| 60 |
response = requests.get(API_URL, headers=headers, params=params)
|
| 61 |
if response.status_code == 200:
|
| 62 |
+
return response.json().get('carbonIntensity', 'N/A')
|
| 63 |
else:
|
| 64 |
return 'Error'
|
| 65 |
|
| 66 |
+
# Define the function to fetch carbon intensity for all companies
|
| 67 |
+
def fetch_carbon_intensities():
|
| 68 |
results = []
|
| 69 |
for index, row in df.iterrows():
|
| 70 |
lat, lon = row["Latitude"], row["Longitude"]
|
| 71 |
+
carbon_intensity = get_carbon_intensity(lat, lon)
|
| 72 |
results.append({
|
| 73 |
"Company": row["Company"],
|
| 74 |
"Location": row["Location"],
|
| 75 |
"Latitude": row["Latitude"],
|
| 76 |
"Longitude": row["Longitude"],
|
| 77 |
+
"Carbon Intensity": carbon_intensity
|
| 78 |
})
|
| 79 |
return pd.DataFrame(results)
|
| 80 |
|
| 81 |
# Define Gradio interface
|
| 82 |
+
def show_carbon_intensity_table():
|
| 83 |
+
result_df = fetch_carbon_intensities()
|
| 84 |
return result_df
|
| 85 |
|
| 86 |
+
gr_interface = gr.Interface(fn=show_carbon_intensity_table,
|
| 87 |
inputs=None,
|
| 88 |
outputs="dataframe",
|
| 89 |
+
title="Carbon Intensity by NIFTY 50 Company Headquarters",
|
| 90 |
+
description="Click the button below to see the latest carbon intensity for NIFTY 50 company headquarters.")
|
| 91 |
|
| 92 |
gr_interface.launch()
|