File size: 1,827 Bytes
69fa870
 
 
 
 
 
 
86550f3
69fa870
 
 
 
 
 
 
 
b8c5963
69fa870
c1b8c12
69fa870
 
 
 
 
 
 
b8c5963
 
69fa870
 
 
 
 
 
 
 
 
 
 
 
 
 
1f9b1c1
b8c5963
69fa870
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
import gradio as gr
import os

# Function to get power breakdown history
def get_power_breakdown_history(lat, lon):
    api_token = os.getenv('API_TOKEN')  # Get API token from environment variables
    url = f'https://api.electricitymap.org/v3/power-breakdown/latest?lat={lat}&lon={lon}'
    headers = {
        'auth-token': api_token
    }
    
    response = requests.get(url, headers=headers)
    
    if response.status_code == 200:
        data = response.json()
        return data
    else:
        return f"Failed to retrieve data: {response.status_code}, {response.text}"

# Function to handle multiple inputs
def get_multiple_power_breakdowns(lat1, lon1, lat2, lon2, lat3, lon3, lat4, lon4, lat5, lon5):
    coords = [(lat1, lon1), (lat2, lon2), (lat3, lon3), (lat4, lon4), (lat5, lon5)]
    results = []
    for lat, lon in coords:
        if lat and lon:
            result = get_power_breakdown_history(lat, lon)
            results.append(f"Results for lat: {lat}, lon: {lon}\n{result}\n\n")
    return "\n".join(results)

# Gradio interface
lat_lon_inputs = [
    gr.Textbox(label='Latitude 1'), gr.Textbox(label='Longitude 1'),
    gr.Textbox(label='Latitude 2'), gr.Textbox(label='Longitude 2'),
    gr.Textbox(label='Latitude 3'), gr.Textbox(label='Longitude 3'),
    gr.Textbox(label='Latitude 4'), gr.Textbox(label='Longitude 4'),
    gr.Textbox(label='Latitude 5'), gr.Textbox(label='Longitude 5')
]

iface = gr.Interface(fn=get_multiple_power_breakdowns, 
                     inputs=lat_lon_inputs, 
                     outputs="text",
                     title="Electricity Map Power Source Breakdown",
                     description="Enter up to 5 pairs of latitude and longitude to get the breakdown history of Power Source (Renewable,Nuclear,Fossil Fuel etc).")

iface.launch()