sasan commited on
Commit
e5f444f
1 Parent(s): 187a965

chore: Update vehicle destination in calculate_route function

Browse files
Files changed (3) hide show
  1. kitt.py +53 -98
  2. skills/common.py +1 -1
  3. skills/routing.py +43 -18
kitt.py CHANGED
@@ -1,42 +1,20 @@
1
  import gradio as gr
2
- import plotly.express as px
3
  import requests
4
 
5
- # INTERFACE WITH AUDIO TO AUDIO
6
-
7
-
8
- def transcript(
9
- general_context, link_to_audio, voice, emotion, place, time, delete_history, state
10
- ):
11
- """this function manages speech-to-text to input Fnanswer function and text-to-speech with the Fnanswer output"""
12
- # load audio from a specific path
13
- audio_path = link_to_audio
14
- audio_array, sampling_rate = librosa.load(
15
- link_to_audio, sr=16000
16
- ) # "sr=16000" ensures that the sampling rate is as required
17
-
18
- # process the audio array
19
- input_features = processor(
20
- audio_array, sampling_rate, return_tensors="pt"
21
- ).input_features
22
- predicted_ids = modelw.generate(input_features)
23
- transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
24
- quest_processing = FnAnswer(
25
- general_context, transcription, place, time, delete_history, state
26
- )
27
- state = quest_processing[2]
28
- print("langue " + quest_processing[3])
29
-
30
- tts.tts_to_file(
31
- text=str(quest_processing[0]),
32
- file_path="output.wav",
33
- speaker_wav=f"Audio_Files/{voice}.wav",
34
- language=quest_processing[3],
35
- emotion="angry",
36
- )
37
 
38
- audio_path = "output.wav"
39
- return audio_path, state["context"], state
40
 
41
 
42
  # to be able to use the microphone on chrome, you will have to go to chrome://flags/#unsafely-treat-insecure-origin-as-secure and enter http://10.186.115.21:7860/
@@ -47,42 +25,6 @@ def transcript(
47
  # What's the closest restaurant from here?
48
 
49
 
50
- import gradio as gr
51
-
52
- shortcut_js = """
53
- <script>
54
- function shortcuts(e) {
55
- var event = document.all ? window.event : e;
56
- switch (e.target.tagName.toLowerCase()) {
57
- case "input":
58
- case "textarea":
59
- break;
60
- default:
61
- if (e.key.toLowerCase() == "r" && e.ctrlKey) {
62
- console.log("recording")
63
- document.getElementById("recorder").start_recording();
64
- }
65
- if (e.key.toLowerCase() == "s" && e.ctrlKey) {
66
- console.log("stopping")
67
- document.getElementById("recorder").stop_recording();
68
- }
69
- }
70
- }
71
- document.addEventListener('keypress', shortcuts, false);
72
- </script>
73
- """
74
-
75
- # with gr.Blocks(head=shortcut_js) as demo:
76
- # action_button = gr.Button(value="Name", elem_id="recorder")
77
- # textbox = gr.Textbox()
78
- # action_button.click(lambda : "button pressed", None, textbox)
79
-
80
- # demo.launch()
81
-
82
-
83
- # Generate options for hours (00-23)
84
- hour_options = [f"{i:02d}:00:00" for i in range(24)]
85
-
86
  model_answer = ""
87
  general_context = ""
88
  # Define the initial state with some initial context.
@@ -93,25 +35,29 @@ initial_context = initial_state["context"]
93
 
94
 
95
  with gr.Blocks(theme=gr.themes.Default()) as demo:
 
 
 
96
 
97
  with gr.Row():
98
  with gr.Column(scale=1, min_width=300):
99
  time_picker = gr.Dropdown(
100
- choices=hour_options, label="What time is it?", value="08:00:00"
101
  )
102
  history = gr.Radio(
103
- ["Yes", "No"], label="Maintain the conversation history?", value="No"
104
  )
105
  voice_character = gr.Radio(
106
  choices=[
107
- "Rick Sanches",
108
  "Eddie Murphy",
109
  "David Attenborough",
110
- "Morgan Freeman",
111
  ],
112
  label="Choose a voice",
113
- value="Rick Sancher",
114
  show_label=True,
 
115
  )
116
  emotion = gr.Radio(
117
  choices=["Cheerful", "Grumpy"],
@@ -119,36 +65,36 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
119
  value="Cheerful",
120
  show_label=True,
121
  )
122
- # place = gr.Radio(
123
- # choices=[
124
- # "Luxembourg Gare, Luxembourg",
125
- # "Kirchberg Campus, Kirchberg",
126
- # "Belval Campus, Belval",
127
- # "Eiffel Tower, Paris",
128
- # "Thionville, France",
129
- # ],
130
- # label="Choose a location for your car",
131
- # value="Kirchberg Campus, Kirchberg",
132
- # show_label=True,
133
- # )
134
  origin = gr.Textbox(
135
  value="Luxembourg Gare, Luxembourg", label="Origin", interactive=True
136
  )
137
  destination = gr.Textbox(
138
- value="Kirchberg Campus, Kirchberg",
139
  label="Destination",
140
  interactive=True,
141
  )
142
- recorder = gr.Audio(
143
- type="filepath", label="input audio", elem_id="recorder"
144
- )
145
  with gr.Column(scale=2, min_width=600):
146
  map_plot = gr.Plot()
147
- origin.submit(fn=calculate_route, outputs=map_plot)
148
- destination.submit(fn=calculate_route, outputs=map_plot)
149
- output_audio = gr.Audio(label="output audio")
150
  # map_if = gr.Interface(fn=plot_map, inputs=year_input, outputs=map_plot)
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  # iface = gr.Interface(
153
  # fn=transcript,
154
  # inputs=[
@@ -165,12 +111,21 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
165
  # head=shortcut_js,
166
  # )
167
 
 
 
 
 
 
 
 
 
168
  # close all interfaces open to make the port available
169
  gr.close_all()
170
  # Launch the interface.
171
 
172
- demo.queue().launch(
173
- debug=True, server_name="0.0.0.0", server_port=7860, ssl_verify=False
174
- )
 
175
 
176
  # iface.launch(debug=True, share=False, server_name="0.0.0.0", server_port=7860, ssl_verify=False)
 
1
  import gradio as gr
 
2
  import requests
3
 
4
+ import skills
5
+ from skills.common import config, vehicle
6
+ from skills.routing import calculate_route
7
+
8
+
9
+ # Generate options for hours (00-23)
10
+ hour_options = [f"{i:02d}:00" for i in range(24)]
11
+
12
+ def set_time(time_picker):
13
+ vehicle.time = time_picker
14
+ return vehicle.model_dump_json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def get_vehicle_status(state):
17
+ return state.value["vehicle"].model_dump_json()
18
 
19
 
20
  # to be able to use the microphone on chrome, you will have to go to chrome://flags/#unsafely-treat-insecure-origin-as-secure and enter http://10.186.115.21:7860/
 
25
  # What's the closest restaurant from here?
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  model_answer = ""
29
  general_context = ""
30
  # Define the initial state with some initial context.
 
35
 
36
 
37
  with gr.Blocks(theme=gr.themes.Default()) as demo:
38
+ state = gr.State(
39
+ value={"context": initial_context, "query": "", "vehicle": vehicle, "route_points": []}
40
+ )
41
 
42
  with gr.Row():
43
  with gr.Column(scale=1, min_width=300):
44
  time_picker = gr.Dropdown(
45
+ choices=hour_options, label="What time is it? (HH:MM)", value="08:00", interactive=True
46
  )
47
  history = gr.Radio(
48
+ ["Yes", "No"], label="Maintain the conversation history?", value="No", interactive=True
49
  )
50
  voice_character = gr.Radio(
51
  choices=[
52
+ "Morgan Freeman",
53
  "Eddie Murphy",
54
  "David Attenborough",
55
+ "Rick Sanches",
56
  ],
57
  label="Choose a voice",
58
+ value="Morgan Freeman",
59
  show_label=True,
60
+ interactive=True,
61
  )
62
  emotion = gr.Radio(
63
  choices=["Cheerful", "Grumpy"],
 
65
  value="Cheerful",
66
  show_label=True,
67
  )
 
 
 
 
 
 
 
 
 
 
 
 
68
  origin = gr.Textbox(
69
  value="Luxembourg Gare, Luxembourg", label="Origin", interactive=True
70
  )
71
  destination = gr.Textbox(
72
+ value="Kirchberg Campus, Luxembourg",
73
  label="Destination",
74
  interactive=True,
75
  )
76
+
 
 
77
  with gr.Column(scale=2, min_width=600):
78
  map_plot = gr.Plot()
79
+
 
 
80
  # map_if = gr.Interface(fn=plot_map, inputs=year_input, outputs=map_plot)
81
 
82
+ with gr.Row():
83
+ with gr.Column():
84
+ recorder = gr.Audio(
85
+ type="filepath", label="Input audio", elem_id="recorder"
86
+ )
87
+ input_text = gr.Textbox(
88
+ value="How is the weather?", label="Input text", interactive=True
89
+ )
90
+ vehicle_status = gr.Textbox(
91
+ value=get_vehicle_status(state), label="Vehicle status", interactive=False
92
+ )
93
+ with gr.Column():
94
+ output_audio = gr.Audio(label="output audio")
95
+ output_text = gr.TextArea(
96
+ value="", label="Output text", interactive=False
97
+ )
98
  # iface = gr.Interface(
99
  # fn=transcript,
100
  # inputs=[
 
111
  # head=shortcut_js,
112
  # )
113
 
114
+ # Update plot based on the origin and destination
115
+ # Sets the current location and destination
116
+ origin.submit(fn=calculate_route, inputs=[origin, destination], outputs=[map_plot, vehicle_status])
117
+ destination.submit(fn=calculate_route, inputs=[origin, destination], outputs=[map_plot, vehicle_status])
118
+
119
+ # Update time based on the time picker
120
+ time_picker.select(fn=set_time, inputs=[time_picker], outputs=[vehicle_status])
121
+
122
  # close all interfaces open to make the port available
123
  gr.close_all()
124
  # Launch the interface.
125
 
126
+ if __name__ == "__main__":
127
+ demo.launch(
128
+ debug=True, server_name="0.0.0.0", server_port=7860, ssl_verify=False
129
+ )
130
 
131
  # iface.launch(debug=True, share=False, server_name="0.0.0.0", server_port=7860, ssl_verify=False)
skills/common.py CHANGED
@@ -58,5 +58,5 @@ vehicle = VehicleStatus(
58
  location_coordinates=(49.6002, 6.1296),
59
  date="2025-03-29",
60
  time="08:00:20",
61
- destination="Kirchberg Campus, Kirchberg"
62
  )
 
58
  location_coordinates=(49.6002, 6.1296),
59
  date="2025-03-29",
60
  time="08:00:20",
61
+ destination="Kirchberg Campus, Luxembourg"
62
  )
skills/routing.py CHANGED
@@ -17,36 +17,56 @@ def find_coordinates(address):
17
  return lat, lon
18
 
19
 
20
- def calculate_route():
21
- origin = "49.631997,6.171029"
22
- destination = "49.586745,6.140002"
23
-
24
- url = f"https://api.tomtom.com/routing/1/calculateRoute/{origin}:{destination}/json?key={config.TOMTOM_API_KEY}"
25
- response = requests.get(url)
26
- data = response.json()
27
 
28
  lats = []
29
  lons = []
30
 
31
- for point in data["routes"][0]["legs"][0]["points"]:
32
  lats.append(point["latitude"])
33
  lons.append(point["longitude"])
34
  # fig = px.line_geo(lat=lats, lon=lons)
35
  # fig.update_geos(fitbounds="locations")
36
 
37
- fig = px.line_mapbox(lat=lats, lon=lons, zoom=12, height=600)
 
 
38
 
39
  fig.update_layout(
40
  mapbox_style="open-street-map",
41
- mapbox_zoom=12,
42
- mapbox_center_lat=lats[0],
43
- mapbox_center_lon=lons[0],
44
  )
45
- fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
46
-
47
  return fig
48
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def find_route_tomtom(
51
  lat_depart="0",
52
  lon_depart="0",
@@ -75,8 +95,6 @@ def find_route_tomtom(
75
 
76
  result = response["routes"][0]["summary"]
77
 
78
-
79
-
80
  distance_m = result["lengthInMeters"]
81
  duration_s = result["travelTimeInSeconds"]
82
  arrival_time = result["arrivalTime"]
@@ -112,7 +130,11 @@ def find_route(destination=""):
112
  lat_depart, lon_depart, lat_dest, lon_dest, departure_time
113
  )
114
 
115
- distance, duration, arrival_time = trip_info["distance_m"], trip_info["duration_s"], trip_info["arrival_time"]
 
 
 
 
116
 
117
  # Calculate distance in kilometers (1 meter = 0.001 kilometers)
118
  distance_km = distance * 0.001
@@ -131,4 +153,7 @@ def find_route(destination=""):
131
  arrival_hour_display = arrival_time.strftime("%H:%M")
132
 
133
  # return the distance and time
134
- return f"The route to {destination} is {distance_km:.2f} km and {time_display}. Leaving now, the arrival time is estimated at {arrival_hour_display}.", raw_response["routes"][0]["legs"][0]["points"]
 
 
 
 
17
  return lat, lon
18
 
19
 
20
+ def plot_route(points):
21
+ import plotly.express as px
 
 
 
 
 
22
 
23
  lats = []
24
  lons = []
25
 
26
+ for point in points:
27
  lats.append(point["latitude"])
28
  lons.append(point["longitude"])
29
  # fig = px.line_geo(lat=lats, lon=lons)
30
  # fig.update_geos(fitbounds="locations")
31
 
32
+ fig = px.line_mapbox(
33
+ lat=lats, lon=lons, zoom=12, height=600, color_discrete_sequence=["red"]
34
+ )
35
 
36
  fig.update_layout(
37
  mapbox_style="open-street-map",
38
+ # mapbox_zoom=12,
 
 
39
  )
40
+ fig.update_geos(fitbounds="locations")
41
+ fig.update_layout(margin={"r": 20, "t": 20, "l": 20, "b": 20})
42
  return fig
43
 
44
 
45
+ def calculate_route(origin, destination):
46
+ """This function is called when the origin or destination is updated in the GUI. It calculates the route between the origin and destination."""
47
+ print(f"calculate_route(origin: {origin}, destination: {destination})")
48
+ origin_coords = find_coordinates(origin)
49
+ destination_coords = find_coordinates(destination)
50
+
51
+ orig_coords_str = ",".join(map(str, origin_coords))
52
+ dest_coords_str = ",".join(map(str, destination_coords))
53
+ print(f"origin_coords: {origin_coords}, destination_coords: {destination_coords}")
54
+
55
+ vehicle.destination = destination
56
+ vehicle.location_coordinates = origin_coords
57
+ vehicle.location = origin
58
+
59
+ # origin = "49.631997,6.171029"
60
+ # destination = "49.586745,6.140002"
61
+
62
+ url = f"https://api.tomtom.com/routing/1/calculateRoute/{orig_coords_str}:{dest_coords_str}/json?key={config.TOMTOM_API_KEY}"
63
+ response = requests.get(url)
64
+ data = response.json()
65
+ points = data["routes"][0]["legs"][0]["points"]
66
+
67
+ return plot_route(points), vehicle.model_dump_json()
68
+
69
+
70
  def find_route_tomtom(
71
  lat_depart="0",
72
  lon_depart="0",
 
95
 
96
  result = response["routes"][0]["summary"]
97
 
 
 
98
  distance_m = result["lengthInMeters"]
99
  duration_s = result["travelTimeInSeconds"]
100
  arrival_time = result["arrivalTime"]
 
130
  lat_depart, lon_depart, lat_dest, lon_dest, departure_time
131
  )
132
 
133
+ distance, duration, arrival_time = (
134
+ trip_info["distance_m"],
135
+ trip_info["duration_s"],
136
+ trip_info["arrival_time"],
137
+ )
138
 
139
  # Calculate distance in kilometers (1 meter = 0.001 kilometers)
140
  distance_km = distance * 0.001
 
153
  arrival_hour_display = arrival_time.strftime("%H:%M")
154
 
155
  # return the distance and time
156
+ return (
157
+ f"The route to {destination} is {distance_km:.2f} km and {time_display}. Leaving now, the arrival time is estimated at {arrival_hour_display}.",
158
+ raw_response["routes"][0]["legs"][0]["points"],
159
+ )