AliceTt commited on
Commit
e05937f
·
1 Parent(s): b126e08

added localisation and culture choices in front

Browse files
Files changed (2) hide show
  1. app.py +114 -35
  2. func_utils.py +62 -0
app.py CHANGED
@@ -1,44 +1,123 @@
1
  import gradio as gr
2
- #from mistralai import Mistral, UserMessage
 
3
  from chatbot_gaia.src.main_flow import kickoff
4
  import pandas as pd
5
 
6
 
7
  import os
8
 
9
- title = "Gaia Mistral Chat Demo"
10
  description = "Example of simple chatbot with Gradio and Mistral AI via its API"
11
- placeholder = "Posez moi une question sur l'agriculture"
12
- examples = ["Je suis un agriculture basé pas loin de bordeaux et je cultive du mais et je cherche des recommandations d'autres cultures mieux adapté aux changements climatique ?",
13
- "Je suis un agriculteur basé au sud de la France vers Nice, je cherche des recommandations de cultures mieux adapté aux changements climatiques ?",]
14
-
15
- #api_key = os.environ.get("MISTRAL_API_KEY")
16
- #client = MistralClient(api_key=api_key)
17
- #client = Mistral(api_key=api_key)
18
- #model = 'open-mixtral-8x7b'
19
- """
20
- def chat_with_mistral(user_input, history):
21
- messages = [{"role": "user", "content": user_input}]
22
-
23
- chat_response = client.chat.complete(model=model, messages=messages)P
24
- return chat_response.choices[0].message.content
25
- """
26
-
27
- def chat_with_agrienergy(user_input, history):
28
- messages = [{"role": "user", "content": user_input}]
29
- result = kickoff(user_input=user_input)
30
- return f"{result}"
31
-
32
- iface = gr.ChatInterface(
33
- fn=chat_with_agrienergy,
34
- chatbot=gr.Chatbot(height=300),
35
- textbox=gr.Textbox(placeholder=placeholder, container=False, scale=7),
36
- title=title,
37
- description=description,
38
- theme="soft",
39
- examples=examples,
40
- cache_examples=False,
41
- )
42
-
43
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+
3
+ # from mistralai import Mistral, UserMessage
4
  from chatbot_gaia.src.main_flow import kickoff
5
  import pandas as pd
6
 
7
 
8
  import os
9
 
10
+ title = "Démo GAIA - Les bénéfices de l'ombrage"
11
  description = "Example of simple chatbot with Gradio and Mistral AI via its API"
12
+ # placeholder = "Posez moi une question sur l'agriculture"
13
+ # examples = ["Je suis un agriculture basé pas loin de bordeaux et je cultive du mais et je cherche des recommandations d'autres cultures mieux adapté aux changements climatique ?",
14
+ # "Je suis un agriculteur basé au sud de la France vers Nice, je cherche des recommandations de cultures mieux adapté aux changements climatiques ?",]
15
+
16
+ # #api_key = os.environ.get("MISTRAL_API_KEY")
17
+ # #client = MistralClient(api_key=api_key)
18
+ # #client = Mistral(api_key=api_key)
19
+ # #model = 'open-mixtral-8x7b'
20
+ # """
21
+ # def chat_with_mistral(user_input, history):
22
+ # messages = [{"role": "user", "content": user_input}]
23
+
24
+ # chat_response = client.chat.complete(model=model, messages=messages)P
25
+ # return chat_response.choices[0].message.content
26
+ # """
27
+
28
+ # def chat_with_agrienergy(user_input, history):
29
+ # messages = [{"role": "user", "content": user_input}]
30
+ # result = kickoff(user_input=user_input)
31
+ # return f"{result}"
32
+
33
+ # iface = gr.ChatInterface(
34
+ # fn=chat_with_agrienergy,
35
+ # chatbot=gr.Chatbot(height=300),
36
+ # textbox=gr.Textbox(placeholder=placeholder, container=False, scale=7),
37
+ # title=title,
38
+ # description=description,
39
+ # theme="soft",
40
+ # examples=examples,
41
+ # cache_examples=False,
42
+ # )
43
+
44
+ # iface.launch(share=True)
45
+
46
+
47
+ # import gradio as gr
48
+ from func_utils import *
49
+
50
+ with gr.Blocks() as demo:
51
+ gr.HTML(
52
+ """
53
+ <style>
54
+ /* Custom style for a specific row */
55
+ .box {
56
+ background-color: #90909b;
57
+ # padding: 20px;
58
+ border-radius: 10px;
59
+ # border: solid 2px #4CAF50;
60
+ display: flex;
61
+ align-content: center;
62
+ padding: 20px;
63
+ }
64
+ .culture_box {
65
+ background-color: #52525b;
66
+ border-radius: 10px;
67
+ display: flex;
68
+ align-content: center;
69
+ }
70
+ </style>
71
+ """
72
+ )
73
+ demo.title = "Démo GAIA - Les bénéfices de l'ombrage"
74
+ gr.HTML("<h1 style='text-align: center;'>Les bénéfices de l'ombrage</h1>")
75
+ gr.HTML(
76
+ "<p style='border: solid white 1px; border-radius: 10px; padding:20px'>L'outil vous permet de voir les avantages potentiels de l'ombrage sur votre exploitation. </p>"
77
+ )
78
+
79
+ with gr.Blocks() as infos:
80
+
81
+ infos.title = "Informations sur votre exploitation"
82
+ gr.HTML("<h2>Renseignez les informations relatives à votre projet</h2>")
83
+ with gr.Row(equal_height=True):
84
+ with gr.Column(variant="panel", scale=1):
85
+ with gr.Row(equal_height=True, elem_classes="box"):
86
+ with gr.Tab(label="Adresse", scale=1):
87
+ address = gr.Textbox(
88
+ label="Addresse",
89
+ info="Adresse de votre projet",
90
+ )
91
+ with gr.Tab(label="Coordonnées GPS", scale=1):
92
+ lat = gr.Number(
93
+ label="Latitude",
94
+ info="Latitude de votre projet",
95
+ )
96
+ lon = gr.Number(
97
+ label="Longitude",
98
+ info="Longitude de votre projet",
99
+ )
100
+ place_btn = gr.Button(value="Valider la localisation", size="sm")
101
+ place_cancel_btn = gr.Button(
102
+ value="Réinitialiser la localisation", size="sm"
103
+ )
104
+
105
+ with gr.Row(elem_classes="box"):
106
+ culture = gr.Textbox(
107
+ label="Culture", scale=1, elem_classes="culture_box"
108
+ )
109
+
110
+ with gr.Column(variant="panel", scale=3):
111
+ map = gr.HTML()
112
+
113
+ simulation_btn = gr.Button(value="Lancer la simulation", size="lg")
114
 
115
+ demo.load(on_init, [lat, lon, address], [lat, lon, map])
116
+ place_btn.click(on_init, [lat, lon, address], [lat, lon, map])
117
+ place_cancel_btn.click(on_delete, [lat, lon, map], [lat, lon, address, map])
118
+ simulation_btn.click(
119
+ launch_simulation, [lat, lon, address, culture], [lat, lon, address, map]
120
+ )
121
+ demo.title = "Démo GAIA - Les bénéfices de l'ombrage"
122
+ # demo.description = "Example of simple chatbot with Gradio and Mistral AI via its API"
123
+ demo.launch()
func_utils.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import folium
2
+ import requests
3
+
4
+
5
+ # https://huggingface.co/spaces/gaia-mistral/pest-livestock-information
6
+ def get_geolocation(adresse, latitude, longitude):
7
+ """Return latitude, longitude & code INSEE from an adress. Latitude & longitude only if they are not specified in the function.
8
+ Args:
9
+ adresse (str): Adress or city for example
10
+ Returns:
11
+ Tuple(float, float, str): latitude, longitude & code INSEE
12
+ """
13
+ if latitude is not None and longitude is not None:
14
+ url = f"https://data.geopf.fr/geocodage/reverse?lon={longitude}&lat={latitude}&index=parcel"
15
+ else:
16
+ url = f"https://data.geopf.fr/geocodage/search?q={adresse}"
17
+
18
+ response = requests.get(url)
19
+ if response.status_code == 200:
20
+ data = response.json()["features"][0]
21
+ if "parcel" in url:
22
+ properties = data["properties"]
23
+ code_insee = properties["departmentcode"] + properties["municipalitycode"]
24
+ else:
25
+ coordinates = data["geometry"]["coordinates"]
26
+ latitude, longitude = coordinates[1], coordinates[0]
27
+ code_insee = data["properties"]["citycode"]
28
+ return latitude, longitude, code_insee
29
+ return None, None, None
30
+
31
+
32
+ def on_init(lat, lon, address):
33
+ map_html, lat, lon = show_map(lat, lon, address)
34
+ return lat, lon, map_html
35
+
36
+
37
+ def on_delete(lat, lon, address):
38
+ address = lat = lon = None
39
+ map_html, lat, lon = show_map(lat, lon, address)
40
+ return lat, lon, address, map_html
41
+
42
+
43
+ def show_map(lat, lon, address):
44
+ if address:
45
+ lat_tmp, lon_tmp, code_insee = get_geolocation(address, None, None)
46
+ if lat_tmp or lon_tmp:
47
+ lat, lon = lat_tmp, lon_tmp
48
+ else:
49
+ return "Adress not found. Please enter a valid address", ""
50
+ lat = lat or 48.832408
51
+ lon = lon or 2.28619
52
+ if lat and lon:
53
+ lat_tmp, lon_tmp, code_insee = get_geolocation(None, lat, lon)
54
+ location_map = folium.Map(location=[lat, lon], zoom_start=14)
55
+ folium.Marker([lat, lon]).add_to(location_map)
56
+ map_html = location_map._repr_html_()
57
+ return map_html, lat, lon
58
+
59
+
60
+ def launch_simulation(lat, lon, address, culture):
61
+ # Do something
62
+ pass