Az-r-ow commited on
Commit
695af8c
·
1 Parent(s): f017054

chore: updated requirements file accodingly and moved the interface to right file

Browse files
Files changed (2) hide show
  1. app/app.py +206 -0
  2. requirements.txt +3 -1
app/app.py CHANGED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import numpy as np
4
+ import pandas as pd
5
+ import torch
6
+
7
+ from travel_resolver.libs.pathfinder.CSVTravelGraph import CSVTravelGraph
8
+ from travel_resolver.libs.pathfinder.graph import Graph
9
+
10
+ transcriber = pipeline(
11
+ "automatic-speech-recognition", model="openai/whisper-base.en", device="cpu"
12
+ )
13
+
14
+
15
+ def transcribe(audio):
16
+ """
17
+ Transcribe audio into text
18
+ """
19
+ sr, y = audio
20
+
21
+ # Convert to mono if stereo
22
+ if y.ndim > 1:
23
+ y = y.mean(axis=1)
24
+
25
+ y = y.astype(np.float32)
26
+ y /= np.max(np.abs(y))
27
+
28
+ return transcriber({"sampling_rate": sr, "raw": y})["text"]
29
+
30
+
31
+ def getCSVTravelGraph():
32
+ """
33
+ Generate Graph with the csv dataset
34
+ Returns:
35
+ (Graph): Graph
36
+ """
37
+ graphData = CSVTravelGraph("./data/sncf/timetables.csv")
38
+ return Graph(graphData.data)
39
+
40
+
41
+ def getDijkstraResult(depart, destination):
42
+ """
43
+ Args:
44
+ depart (str): station name
45
+ destination (str): station name
46
+ Generate dijkstraGraph and find the shortest way for the destination
47
+ Returns:
48
+ (str): Time of the shortest travel found
49
+ """
50
+ graph = getCSVTravelGraph()
51
+ path, cost = graph.RunDijkstraBetweenTwoNodes(depart, destination)
52
+ if destination in cost:
53
+ return [path, str(cost[destination]) + " minutes"]
54
+ return [[], "Temps non trouvé"]
55
+
56
+
57
+ def getAStarResult(depart, destination):
58
+ """
59
+ Args:
60
+ depart (str): station name
61
+ destination (str): station name
62
+ Generate AStarGraph and find the shortest way for the destination
63
+ Returns:
64
+ (str): Time of the shortest travel found
65
+ """
66
+ graph = getCSVTravelGraph()
67
+ heuristic = graph.RunDijkstra(destination)
68
+ path, cost = graph.RunAStar(depart, destination, heuristic)
69
+ if destination in cost:
70
+ return [path, str(cost[destination]) + " minutes"]
71
+ return [[], "Temps non trouvé"]
72
+
73
+
74
+ def getStationsByCityName(city: str):
75
+ data = pd.read_csv("../data/sncf/gares_info.csv", sep=",")
76
+ stations = tuple(data[data["Commune"] == city]["Nom de la gare"])
77
+ return stations
78
+
79
+
80
+ def handle_audio(audio):
81
+ promptAudio = transcribe(audio)
82
+ return (
83
+ gr.update(visible=True),
84
+ gr.update(visible=False),
85
+ gr.update(value=promptAudio),
86
+ gr.update(value="PARIS"),
87
+ gr.update(value="MONTPELLIER"),
88
+ )
89
+
90
+
91
+ def handle_file(file):
92
+ if file is not None:
93
+ with open(file.name, "r") as f:
94
+ file_content = f.read()
95
+ else:
96
+ file_content = "Aucun fichier uploadé."
97
+ return (
98
+ gr.update(visible=True),
99
+ gr.update(visible=False),
100
+ gr.update(value=file_content),
101
+ gr.update(value="PARIS"),
102
+ gr.update(value="MONTPELLIER"),
103
+ )
104
+
105
+
106
+ def handle_back():
107
+ return gr.update(visible=False), gr.update(visible=True)
108
+
109
+
110
+ def handleCityChange(city):
111
+ stations = getStationsByCityName(city)
112
+ return gr.update(choices=stations, value=stations[0], interactive=True)
113
+
114
+
115
+ def handleStationChange(departureStation, destinationStation):
116
+ if departureStation and destinationStation:
117
+ dijkstraPath, dijkstraCost = getDijkstraResult(
118
+ departureStation, destinationStation
119
+ )
120
+ dijkstraPathFormatted = "\n".join(
121
+ [f"{i + 1}. {elem}" for i, elem in enumerate(dijkstraPath)]
122
+ )
123
+ AStarPath, AStarCost = getAStarResult(departureStation, destinationStation)
124
+ AStarPathFormatted = "\n".join(
125
+ [f"{i + 1}. {elem}" for i, elem in enumerate(AStarPath)]
126
+ )
127
+ print(dijkstraPathFormatted)
128
+ return (
129
+ gr.update(value=dijkstraCost),
130
+ gr.update(value=dijkstraPathFormatted, lines=len(dijkstraPath)),
131
+ gr.update(value=AStarCost),
132
+ gr.update(value=AStarPathFormatted, lines=len(AStarPath)),
133
+ )
134
+ return (
135
+ gr.HTML("<p>Aucun prompt renseigné</p>"),
136
+ gr.update(value=""),
137
+ gr.HTML("<p>Aucun prompt renseigné</p>"),
138
+ gr.update(value=""),
139
+ )
140
+
141
+
142
+ with gr.Blocks(css="#back-button {width: fit-content}") as interface:
143
+ with gr.Column() as promptChooser:
144
+ with gr.Row():
145
+ audio = gr.Audio(label="Fichier audio")
146
+ file = gr.File(
147
+ label="Fichier texte", file_types=["text"], file_count="single"
148
+ )
149
+ with gr.Column(visible=False) as content:
150
+ backButton = gr.Button("← Back", elem_id="back-button")
151
+ with gr.Row():
152
+ with gr.Column(scale=1, min_width=300) as parameters:
153
+ prompt = gr.Textbox(label="Prompt")
154
+ departureCity = gr.Textbox(label="Ville de départ")
155
+ destinationCity = gr.Textbox(label="Ville de de destination")
156
+ with gr.Column(scale=2, min_width=300) as result:
157
+ with gr.Row():
158
+ departureStation = gr.Dropdown(label="Gare de départ")
159
+ destinationStation = gr.Dropdown(label="Gare d'arrivée")
160
+ with gr.Tab("Dijkstra"):
161
+ timeDijkstra = gr.HTML("<p>Aucun prompt renseigné</p>")
162
+ dijkstraPath = gr.Textbox(label="Chemin emprunté")
163
+
164
+ with gr.Tab("AStar"):
165
+ timeAStar = gr.HTML("<p>Aucun prompt renseigné</p>")
166
+ AstarPath = gr.Textbox(label="Chemin emprunté")
167
+ audio.change(
168
+ handle_audio,
169
+ inputs=[audio],
170
+ outputs=[
171
+ content,
172
+ promptChooser,
173
+ prompt,
174
+ departureCity,
175
+ destinationCity,
176
+ ], # On rend la section "content" visible
177
+ )
178
+ file.upload(
179
+ handle_file,
180
+ inputs=[file],
181
+ outputs=[
182
+ content,
183
+ promptChooser,
184
+ prompt,
185
+ departureCity,
186
+ destinationCity,
187
+ ], # On rend la section "content" visible
188
+ )
189
+ backButton.click(handle_back, inputs=[], outputs=[content, promptChooser])
190
+ departureCity.change(
191
+ handleCityChange, inputs=[departureCity], outputs=[departureStation]
192
+ )
193
+ destinationCity.change(
194
+ handleCityChange, inputs=[destinationCity], outputs=[destinationStation]
195
+ )
196
+ departureStation.change(
197
+ handleStationChange,
198
+ inputs=[departureStation, destinationStation],
199
+ outputs=[timeDijkstra, dijkstraPath, timeAStar, AstarPath],
200
+ )
201
+ destinationStation.change(
202
+ handleStationChange,
203
+ inputs=[departureStation, destinationStation],
204
+ outputs=[timeDijkstra, dijkstraPath, timeAStar, AstarPath],
205
+ )
206
+ interface.launch()
requirements.txt CHANGED
@@ -15,4 +15,6 @@ tabulate
15
  transformers
16
  sentencepiece
17
  stanza
18
- pydot
 
 
 
15
  transformers
16
  sentencepiece
17
  stanza
18
+ pydot
19
+ gradio
20
+ torchaudio