dindizz commited on
Commit
002c75f
·
verified ·
1 Parent(s): 349aad5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -27
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/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 the top market cap company headquarters.")
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()