Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Function to get power breakdown history
|
| 6 |
+
def get_power_breakdown_history(lat, lon):
|
| 7 |
+
api_token = os.getenv('API_TOKEN') # Get API token from environment variables
|
| 8 |
+
url = f'https://api.electricitymap.org/v3/power-breakdown/history?lat={lat}&lon={lon}'
|
| 9 |
+
headers = {
|
| 10 |
+
'auth-token': api_token
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
response = requests.get(url, headers=headers)
|
| 14 |
+
|
| 15 |
+
if response.status_code == 200:
|
| 16 |
+
data = response.json()
|
| 17 |
+
return data
|
| 18 |
+
else:
|
| 19 |
+
return f"Failed to retrieve data: {response.status_code}, {response.text}"
|
| 20 |
+
|
| 21 |
+
# Function to handle multiple inputs
|
| 22 |
+
def get_multiple_power_breakdowns(lat1, lon1, lat2, lon2, lat3, lon3, lat4, lon4, lat5, lon5):
|
| 23 |
+
coords = [(lat1, lon1), (lat2, lon2), (lat3, lon3), (lat4, lon4), (lat5, lon5)]
|
| 24 |
+
results = []
|
| 25 |
+
for lat, lon in coords:
|
| 26 |
+
if lat and lon:
|
| 27 |
+
result = get_power_breakdown_history(lat, lon)
|
| 28 |
+
results.append(f"Results for lat: {lat}, lon: {lon}\n{result}\n\n")
|
| 29 |
+
return "\n".join(results)
|
| 30 |
+
|
| 31 |
+
# Gradio interface
|
| 32 |
+
lat_lon_inputs = [
|
| 33 |
+
gr.Textbox(label='Latitude 1'), gr.Textbox(label='Longitude 1'),
|
| 34 |
+
gr.Textbox(label='Latitude 2'), gr.Textbox(label='Longitude 2'),
|
| 35 |
+
gr.Textbox(label='Latitude 3'), gr.Textbox(label='Longitude 3'),
|
| 36 |
+
gr.Textbox(label='Latitude 4'), gr.Textbox(label='Longitude 4'),
|
| 37 |
+
gr.Textbox(label='Latitude 5'), gr.Textbox(label='Longitude 5')
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
iface = gr.Interface(fn=get_multiple_power_breakdowns,
|
| 41 |
+
inputs=lat_lon_inputs,
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="Electricity Map Power Breakdown History",
|
| 44 |
+
description="Enter up to 5 pairs of latitude and longitude to get the power breakdown history.")
|
| 45 |
+
|
| 46 |
+
iface.launch()
|