Spaces:
Build error
Build error
| 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() | |