Clément Simon commited on
Commit
aeaf613
1 Parent(s): 754727f

feat: plotly map

Browse files
Files changed (2) hide show
  1. app.py +56 -23
  2. requirements.txt +2 -1
app.py CHANGED
@@ -2,41 +2,74 @@ import gradio as gr
2
  from mistralai.client import MistralClient, ChatMessage
3
  import os
4
  from dotenv import load_dotenv
 
5
 
6
  # Load environment variables
7
  load_dotenv()
8
  api_key = os.getenv('API_KEY')
9
-
10
- # Initialize Mistral client with the API key
11
  client = MistralClient(api_key=api_key)
 
 
12
 
13
- def answer_question(question):
14
- """Directly ask Mistral the question and return the answer."""
15
- # Format the user's question for Mistral
16
- user_message = question
17
 
18
- # Use the run_mistral function to get an answer
19
- answer = run_mistral(user_message)
20
-
21
- return answer
22
 
23
- def run_mistral(user_message, model="mistral-medium"):
24
- """Interact with Mistral using chat."""
25
- messages = [ChatMessage(role="user", content=user_message)]
26
  chat_response = client.chat(model=model, messages=messages)
27
  return chat_response.choices[0].message.content
28
 
29
- app = gr.Interface(
30
- fn=chat_with_mistral,
31
- inputs=gr.Textbox(lines=2, placeholder=placeholder),
32
- outputs="text",
33
- title=title,
34
- description=description,
35
- examples=examples
36
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- # Create a map using the library folium
 
 
 
 
 
 
39
 
40
 
41
  if __name__ == "__main__":
42
- app.launch(share=True) # Set `share=True` to create a public link
 
2
  from mistralai.client import MistralClient, ChatMessage
3
  import os
4
  from dotenv import load_dotenv
5
+ import plotly.graph_objects as go
6
 
7
  # Load environment variables
8
  load_dotenv()
9
  api_key = os.getenv('API_KEY')
 
 
10
  client = MistralClient(api_key=api_key)
11
+ model = 'mistral-small'
12
+
13
 
14
+ title = "Gaia Mistral Chat Demo"
15
+ description = "Example of simple chatbot with Gradio and Mistral AI via its API"
16
+ placeholder = "Posez moi une question sur l'agriculture"
17
+ examples = ["Comment fait on pour produire du maïs ?", "Rédige moi une lettre pour faire un stage dans une exploitation agricole", "Comment reprendre une exploitation agricole ?"]
18
 
 
 
 
 
19
 
20
+ def chat_with_mistral(user_input):
21
+ messages = [ChatMessage(role="user", content=user_input)]
22
+
23
  chat_response = client.chat(model=model, messages=messages)
24
  return chat_response.choices[0].message.content
25
 
26
+ def create_world_map(
27
+ lat=45.5017,
28
+ lon=-73.5673,
29
+ ):
30
+ fig = go.Figure(go.Scattermapbox
31
+ (
32
+ lat=[lat],
33
+ lon=[lon],
34
+ mode='markers',
35
+ marker=go.scattermapbox.Marker(size=14),
36
+ text=['Montreal'],
37
+ ))
38
+
39
+ fig.update_layout(
40
+ mapbox_style="open-street-map",
41
+ hovermode='closest',
42
+ mapbox=dict(
43
+ bearing=0,
44
+ center=go.layout.mapbox.Center(
45
+ lat=lat,
46
+ lon=lon,
47
+ ),
48
+ pitch=0,
49
+ zoom=5
50
+ ),)
51
+
52
+ return fig
53
+
54
+ with gr.Blocks() as demo:
55
+ with gr.Column():
56
+ with gr.Row():
57
+ user_input = gr.Textbox(lines=2, placeholder=placeholder)
58
+ send_chat_btn = gr.Button(value="Send")
59
+ lat = gr.Number(value=45.5017, label="Latitude")
60
+ lon = gr.Number(value=-73.5673, label="Longitude")
61
+ update_map_btn = gr.Button(value="Update Map")
62
+
63
+ chat_output = gr.Textbox(lines=2, placeholder="Réponse")
64
 
65
+ # map:
66
+ map = gr.Plot()
67
+ demo.load(chat_with_mistral, user_input, chat_output)
68
+ send_chat_btn.click(chat_with_mistral, user_input, chat_output)
69
+ # map:
70
+ demo.load(create_world_map, [lat, lon], map)
71
+ update_map_btn.click(create_world_map, [lat, lon], map)
72
 
73
 
74
  if __name__ == "__main__":
75
+ demo.launch(share=True) # Set `share=True` to create a public link
requirements.txt CHANGED
@@ -2,4 +2,5 @@ python-dotenv
2
  mistralai
3
  langchain-community
4
  faiss-cpu
5
- gradio
 
 
2
  mistralai
3
  langchain-community
4
  faiss-cpu
5
+ gradio
6
+ plotly