gaia / func_utils.py
AliceTt's picture
added formatting of synthesis
fe52f62
import folium
import requests
import gradio as gr
from compute_yield import plot_yield
from summary_test import get_summaries
from visualize.visualize import get_plots
# code from https://huggingface.co/spaces/gaia-mistral/pest-livestock-information
def get_geolocation(adresse, latitude, longitude):
"""Return latitude, longitude & code INSEE from an adress. Latitude & longitude only if they are not specified in the function.
Args:
adresse (str): Adress or city for example
Returns:
Tuple(float, float, str): latitude, longitude & code INSEE
"""
if latitude is not None and longitude is not None:
url = f"https://data.geopf.fr/geocodage/reverse?lon={longitude}&lat={latitude}&index=parcel"
else:
url = f"https://data.geopf.fr/geocodage/search?q={adresse}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()["features"][0]
if "parcel" in url:
properties = data["properties"]
code_insee = properties["departmentcode"] + properties["municipalitycode"]
else:
coordinates = data["geometry"]["coordinates"]
latitude, longitude = coordinates[1], coordinates[0]
code_insee = data["properties"]["citycode"]
return latitude, longitude, code_insee
return None, None, None
# code from https://huggingface.co/spaces/gaia-mistral/pest-livestock-information
def on_init(lat, lon, address):
map_html, lat, lon = show_map(lat, lon, address)
return lat, lon, map_html
def on_delete(lat, lon, address):
address = lat = lon = None
map_html, lat, lon = show_map(lat, lon, address)
return lat, lon, address, map_html
# code from https://huggingface.co/spaces/gaia-mistral/pest-livestock-information
def show_map(lat, lon, address):
if address:
lat_tmp, lon_tmp, code_insee = get_geolocation(address, None, None)
if lat_tmp or lon_tmp:
lat, lon = lat_tmp, lon_tmp
else:
return "Adress not found. Please enter a valid address", ""
lat = lat or 48.832408
lon = lon or 2.28619
if lat and lon:
lat_tmp, lon_tmp, code_insee = get_geolocation(None, lat, lon)
location_map = folium.Map(location=[lat, lon], zoom_start=14)
folium.Marker([lat, lon]).add_to(location_map)
map_html = location_map._repr_html_()
return map_html, lat, lon
def go_to_page_2():
return gr.Column(visible=False), gr.Column(visible=True)
def launch_simulation(lat, lon, address, culture):
current_situation_summary, agripv_summary = get_summaries()
page1, page_2 = go_to_page_2()
plot_1, plot_2, plot_3, plot_4, plot_5 = get_plots()
current_plot_yields = plot_yield(
latitude=lat, longitude=lon, culture=culture, shading_coef=0
)
shaded_plot_yields = plot_yield(
latitude=lat, longitude=lon, culture=culture, shading_coef=0.2
)
return (
current_situation_summary,
agripv_summary,
plot_1,
plot_2,
plot_3,
plot_4,
plot_5,
current_plot_yields,
shaded_plot_yields,
page1,
page_2,
gr.Button(visible=True),
)