File size: 15,924 Bytes
695af8c
 
 
 
49f1d28
4e0aa6b
49f1d28
695af8c
 
90838cb
dcd93f5
65b2047
3c21054
695af8c
 
49f1d28
695af8c
 
dcd93f5
 
4e0aa6b
 
 
 
 
 
 
65b2047
 
4e0aa6b
 
 
 
 
e80c2cb
4e0aa6b
 
e80c2cb
 
 
 
 
 
4e0aa6b
 
 
 
8115897
e80c2cb
 
 
 
 
849fa7f
e80c2cb
 
 
 
 
4e0aa6b
dcd93f5
4e0aa6b
e80c2cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd93f5
 
4e0aa6b
 
 
 
dcd93f5
 
e80c2cb
 
 
 
 
 
 
 
 
 
 
 
 
65b2047
e80c2cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcd93f5
 
 
 
65b2047
 
 
 
 
dcd93f5
 
 
 
 
 
a4bde73
 
 
 
65b2047
 
 
 
 
 
 
 
 
 
 
a4bde73
 
 
 
 
 
 
65b2047
 
 
a4bde73
65b2047
 
 
 
 
 
 
 
dcd93f5
 
 
 
 
 
 
65b2047
dcd93f5
 
 
 
 
 
65b2047
dcd93f5
 
8115897
dcd93f5
8115897
dcd93f5
a4bde73
dcd93f5
49f1d28
695af8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90838cb
3c21054
695af8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90838cb
65b2047
 
 
 
 
 
 
 
 
90838cb
65b2047
 
 
 
 
 
695af8c
 
dcd93f5
 
 
49f1d28
dcd93f5
49f1d28
 
dcd93f5
 
 
 
 
695af8c
49f1d28
 
695af8c
dcd93f5
 
e80c2cb
 
 
 
 
 
 
 
 
 
dcd93f5
 
 
e80c2cb
 
 
 
 
 
 
 
 
 
dcd93f5
 
 
 
 
 
4e0aa6b
 
dcd93f5
 
65b2047
dcd93f5
 
 
 
 
 
 
 
 
 
 
 
65b2047
 
 
dcd93f5
 
65b2047
 
 
 
 
dcd93f5
 
 
a4bde73
8115897
 
dcd93f5
 
 
 
 
 
 
 
 
65b2047
dcd93f5
 
 
695af8c
 
dcd93f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65b2047
dcd93f5
a4bde73
dcd93f5
 
 
65b2047
dcd93f5
a4bde73
dcd93f5
65b2047
 
 
a4bde73
65b2047
dcd93f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65b2047
 
 
 
 
 
 
dcd93f5
 
 
 
65b2047
 
 
 
 
 
 
dcd93f5
 
 
 
49f1d28
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import gradio as gr
from transformers import pipeline
import numpy as np
import pandas as pd
from travel_resolver.libs.nlp.ner.models import BiLSTM_NER, LSTM_NER, CamemBERT_NER
from helpers.global_vars import entities_label_mapping, PROGRESS, HTML_COMPONENTS
from travel_resolver.libs.nlp.ner.data_processing import process_sentence
from travel_resolver.libs.pathfinder.CSVTravelGraph import CSVTravelGraph
from travel_resolver.libs.pathfinder.graph import Graph
from helpers.utils import get_data_path
import time
import plotly.graph_objects as go
import os

transcriber = pipeline(
    "automatic-speech-recognition", model="openai/whisper-base", device="cpu"
)

models = {"LSTM": LSTM_NER(), "BiLSTM": BiLSTM_NER(), "CamemBERT": CamemBERT_NER()}


def handle_audio(audio, model, progress=gr.Progress()):
    progress(
        0,
    )
    promptAudio = transcribe(audio)

    print(f"prompt : {promptAudio}")

    time.sleep(1)

    return render_tabs([promptAudio], model, progress)


def handle_text(text, model, progress=gr.Progress()):
    progress(0, desc=PROGRESS.ANALYZING_FILE.value)
    time.sleep(1)
    if text and text.strip():
        progress(0.33, desc=PROGRESS.READING_FILE.value)
        sentences = [
            sentence.strip() for sentence in text.split("\n") if sentence.strip()
        ]
        return render_tabs(sentences, model, progress)


tabs_components = []

with gr.Blocks() as demo:
    # Add disclaimer
    gr.HTML(
        """
        <div style="background-color: #f0f8ff; padding: 15px; border-radius: 5px; margin-bottom: 20px; border-left: 4px solid #007acc;">
            <p style="margin: 0; font-size: 14px; color: #333;">
                <strong>Disclaimer:</strong> This simple app is meant to showcase NER for French text and will only work for French text and cities in France since the database is from SNCF. If you're interested in checking out the work behind it, <a href="https://github.com/Az-r-ow/TravelNER" target="_blank">click here</a>.
            </p>
        </div>
        """
    )

    with gr.Column():
        with gr.Row():
            audio = gr.Audio(label="Fichier audio", interactive=True)
            with gr.Column():
                # Example sentence button
                example_sentence = "Je souhaite aller de Paris à Lyon demain matin."
                example_btn = gr.Button(
                    'Exemple : "' + example_sentence + '"',
                    variant="secondary",
                )

                text_input = gr.Textbox(
                    label="Texte français",
                    placeholder="Enter text : (ex: Je souhaite aller de Paris à Lyon demain matin.)",
                    lines=3,
                    interactive=True,
                )

                submit_btn = gr.Button("Analyser le texte", variant="primary")

        model = gr.Dropdown(
            label="Modèle NER",
            choices=models.keys(),
            value="CamemBERT",
            interactive=True,
        )

        # Output container and state
        text_state = gr.State("")
        model_state = gr.State("CamemBERT")
        audio_state = gr.State(None)

    # Handle example button click
    example_btn.click(lambda: example_sentence, outputs=[text_input])

    # Handle submit button click - update state
    submit_btn.click(
        lambda text, model: (text, model),
        inputs=[text_input, model],
        outputs=[text_state, model_state],
    )

    # Handle audio input - update state
    audio.change(
        lambda audio, model: (audio, model),
        inputs=[audio, model],
        outputs=[audio_state, model_state],
    )

    # Render output based on state changes
    @gr.render(inputs=[text_state, model_state, audio_state])
    def render_output(text_input_value, model_value, audio_value):
        if audio_value is not None:
            return handle_audio(audio_value, model_value)
        elif text_input_value and text_input_value.strip():
            return handle_text(text_input_value, model_value)


def handleCityChange(city):
    stations = getStationsByCityName(city)
    return gr.update(
        choices=[station["Nom de le gare"] for station in stations],
        value=stations[0]["Nom de la gare"],
        interactive=True,
    )


def formatPath(path):
    return "\n".join([f"{i + 1}. {elem}" for i, elem in enumerate(path)])


def plotMap(stationsInformation: dict = None):
    stationNames = stationsInformation["stations"] if stationsInformation else []
    stationsLat = stationsInformation["lat"] if stationsInformation else []
    stationsLon = stationsInformation["lon"] if stationsInformation else []

    plt = go.Figure(
        go.Scattermapbox(
            lat=stationsLat,
            lon=stationsLon,
            mode="markers+lines",
            marker=go.scattermapbox.Marker(size=14),
            text=stationNames,
        )
    )

    # France's default coordinates
    defaultLat = 46.227638
    defaultLon = 2.213749

    centerLat = stationsLat[0] if stationsLat else defaultLat
    centerLon = stationsLon[0] if stationsLon else defaultLon

    plt.update_layout(
        mapbox_style="open-street-map",
        mapbox=dict(
            center=go.layout.mapbox.Center(lat=centerLat, lon=centerLon),
            pitch=0,
            zoom=3,
        ),
    )

    return plt


def handleStationChange(departureStation, destinationStation):
    if departureStation and destinationStation:
        dijkstraPath, dijkstraCost = getDijkstraResult(
            departureStation, destinationStation
        )
        dijkstraPathFormatted = formatPath(dijkstraPath)
        AStarPath, AStarCost = getAStarResult(departureStation, destinationStation)
        AStarStationsInformation = getStationsInformation(AStarPath)
        AStarPathFormatted = formatPath(AStarPath)
        return (
            gr.update(value=dijkstraCost),
            gr.update(value=dijkstraPathFormatted, lines=len(dijkstraPath)),
            gr.update(value=AStarCost),
            gr.update(value=AStarPathFormatted, lines=len(AStarPath)),
            plotMap(AStarStationsInformation),
        )
    return (
        gr.HTML(HTML_COMPONENTS.NO_PROMPT.value),
        gr.update(value=""),
        gr.HTML(HTML_COMPONENTS.NO_PROMPT.value),
        gr.update(value=""),
        gr.update(visible=None),
    )


def transcribe(audio):
    """
    Transcribe audio into text
    """
    sr, y = audio

    # Convert to mono if stereo
    if y.ndim > 1:
        y = y.mean(axis=1)

    y = y.astype(np.float32)
    y /= np.max(np.abs(y))

    return transcriber({"sampling_rate": sr, "raw": y})["text"]


def getCSVTravelGraph():
    """
    Generate Graph with the csv dataset
    Returns:
        (Graph): Graph
    """
    timetables_file = get_data_path("sncf", "timetables.csv")
    graphData = CSVTravelGraph(timetables_file)
    return Graph(graphData.data)


def getDijkstraResult(depart, destination):
    """
    Args:
        depart (str): station name
        destination (str): station name
    Generate dijkstraGraph and find the shortest way for the destination
    Returns:
        (str): Time of the shortest travel found
    """
    graph = getCSVTravelGraph()
    path, cost = graph.RunDijkstraBetweenTwoNodes(depart, destination)
    if destination in cost:
        return [path, str(cost[destination]) + " minutes"]
    return [[], "Temps non trouvé"]


def getAStarResult(depart, destination):
    """
    Args:
        depart (str): station name
        destination (str): station name
    Generate AStarGraph and find the shortest way for the destination
    Returns:
        (str): Time of the shortest travel found
    """
    graph = getCSVTravelGraph()
    heuristic = graph.RunDijkstra(destination)
    path, cost = graph.RunAStar(depart, destination, heuristic)
    if destination in cost:
        return [path, str(cost[destination]) + " minutes"]
    return [[], "Temps non trouvé"]


def getStationsByCityName(city: str):
    data = pd.read_csv(get_data_path("sncf", "gares_info.csv"), sep=",")
    stations = data[data["Commune"] == city]
    return dict(
        stations=stations["Nom de la gare"].to_list(),
        lat=stations["Latitude"].to_list(),
        lon=stations["Longitude"].to_list(),
    )


def getStationsInformation(stations: list[str]):
    data = pd.read_csv(get_data_path("sncf", "gares_info.csv"), sep=",")
    data = data[data["Nom de la gare"].isin(stations)]
    return dict(
        stations=data["Nom de la gare"].to_list(),
        lat=data["Latitude"].to_list(),
        lon=data["Longitude"].to_list(),
    )


def getEntitiesPositions(text, entity):
    start_idx = text.find(entity)
    end_idx = start_idx + len(entity)

    return start_idx, end_idx


def getDepartureAndArrivalFromText(text: str, model: str):
    entities = models[model].get_entities(text)
    if not isinstance(entities, list):
        entities = entities.tolist()
    tokenized_sentence = process_sentence(text, return_tokens=True)

    dep = None
    arr = None

    if 1 in entities:
        dep_idx = entities.index(1)
        # Add bounds checking to prevent IndexError
        if dep_idx < len(tokenized_sentence):
            dep = tokenized_sentence[dep_idx]
            start, end = getEntitiesPositions(text, dep)
            dep = {
                "entity": entities_label_mapping[1],
                "word": dep,
                "start": start,
                "end": end,
            }

    if 2 in entities:
        arr_idx = entities.index(2)
        # Add bounds checking to prevent IndexError
        if arr_idx < len(tokenized_sentence):
            arr = tokenized_sentence[arr_idx]
            start, end = getEntitiesPositions(text, arr)
            arr = {
                "entity": entities_label_mapping[2],
                "word": arr,
                "start": start,
                "end": end,
            }

    return dep, arr


def render_tabs(sentences: list[str], model: str, progress_bar: gr.Progress):
    idx = 0
    with gr.Column() as tabs:
        for sentence in progress_bar.tqdm(sentences, desc=PROGRESS.PROCESSING.value):
            with gr.Tab(f"Sentence {idx}"):
                dep, arr = getDepartureAndArrivalFromText(sentence, model)
                print(f"dep: {dep}, arr: {arr}")
                entities = []
                for entity in [dep, arr]:
                    if entity:
                        entities.append(entity)

                # Format the classified entities
                departureCityValue = dep["word"].upper() if dep else ""
                arrivalCityValue = arr["word"].upper() if arr else ""

                # Get the available stations
                departureStations = getStationsByCityName(departureCityValue)
                departureStationValue = (
                    departureStations["stations"][0]
                    if len(departureStations["stations"])
                    else ""
                )
                arrivalStations = getStationsByCityName(arrivalCityValue)
                arrivalStationValue = (
                    arrivalStations["stations"][0]
                    if len(arrivalStations["stations"])
                    else ""
                )

                dijkstraPathValues = []
                AStarPathValues = []
                AStarStationsInformation = None
                timeDijkstraValue = HTML_COMPONENTS.NO_PROMPT.value
                timeAStarValue = HTML_COMPONENTS.NO_PROMPT.value

                # Get the paths and time for the two algorithms
                if departureStationValue and arrivalStationValue:
                    dijkstraPathValues, timeDijkstraValue = getDijkstraResult(
                        departureStationValue, arrivalStationValue
                    )
                    AStarPathValues, timeAStarValue = getAStarResult(
                        departureStationValue, arrivalStationValue
                    )
                    AStarStationsInformation = getStationsInformation(AStarPathValues)

                dijkstraPathFormatted = formatPath(dijkstraPathValues)
                AStarPathFormatted = formatPath(AStarPathValues)

                with gr.Row():
                    with gr.Column(scale=1, min_width=300):
                        gr.HighlightedText(
                            value={"text": sentence, "entities": entities}
                        )
                        departureCity = gr.Textbox(
                            label="Ville de départ",
                            value=departureCityValue,
                        )
                        arrivalCity = gr.Textbox(
                            label="Ville d'arrivée",
                            value=arrivalCityValue,
                        )
                    with gr.Column(scale=2, min_width=300):
                        with gr.Row():
                            departureStation = gr.Dropdown(
                                label="Gare de départ",
                                choices=departureStations["stations"],
                                value=departureStationValue,
                                allow_custom_value=True,
                            )
                            arrivalStation = gr.Dropdown(
                                label="Gare d'arrivée",
                                choices=arrivalStations["stations"],
                                value=arrivalStationValue,
                                allow_custom_value=True,
                            )

                        plt = plotMap(AStarStationsInformation)

                        map = gr.Plot(plt, min_width=300)

                        with gr.Tab("Dijkstra"):
                            timeDijkstra = gr.HTML(value=timeDijkstraValue)
                            dijkstraPath = gr.Textbox(
                                label="Chemin emprunté",
                                value=dijkstraPathFormatted,
                                lines=len(dijkstraPathValues),
                            )

                        with gr.Tab("AStar"):
                            timeAStar = gr.HTML(value=timeAStarValue)
                            AstarPath = gr.Textbox(
                                label="Chemin emprunté",
                                value=AStarPathFormatted,
                                lines=len(AStarPathValues),
                            )

                        departureCity.change(
                            handleCityChange,
                            inputs=[departureCity],
                            outputs=[departureStation],
                        )
                        arrivalCity.change(
                            handleCityChange,
                            inputs=[arrivalCity],
                            outputs=[arrivalStation],
                        )
                        departureStation.change(
                            handleStationChange,
                            inputs=[departureStation, arrivalStation],
                            outputs=[
                                timeDijkstra,
                                dijkstraPath,
                                timeAStar,
                                AstarPath,
                                map,
                            ],
                        )
                        arrivalStation.change(
                            handleStationChange,
                            inputs=[departureStation, arrivalStation],
                            outputs=[
                                timeDijkstra,
                                dijkstraPath,
                                timeAStar,
                                AstarPath,
                                map,
                            ],
                        )

                    idx += 1


if __name__ == "__main__":
    demo.launch()