Matthias GOUPIL
commited on
Commit
·
ced8d95
1
Parent(s):
2d9f2c6
feat(interface): linking the AStar graph to the interface with Paul
Browse files
app/travel_resolver/libs/pathfinder/CSVTravelGraph.py
CHANGED
|
@@ -2,19 +2,18 @@ import pandas as pd
|
|
| 2 |
from typing import Dict
|
| 3 |
|
| 4 |
class CSVTravelGraph():
|
| 5 |
-
def __init__(self, csv_file: str
|
| 6 |
"""
|
| 7 |
Read csv and create the graph in function of the given mode
|
| 8 |
"""
|
| 9 |
self.csv = pd.read_csv(csv_file, sep="\t")
|
| 10 |
-
self.
|
| 11 |
-
if(mode =="Dijkstra"): self.data = self.generateDijkstra()
|
| 12 |
|
| 13 |
-
def
|
| 14 |
"""
|
| 15 |
-
Create a
|
| 16 |
Returns:
|
| 17 |
-
(Dict[str, Dict[str, float]]): The
|
| 18 |
"""
|
| 19 |
graph: Dict[str, Dict[str, float]] = {}
|
| 20 |
|
|
|
|
| 2 |
from typing import Dict
|
| 3 |
|
| 4 |
class CSVTravelGraph():
|
| 5 |
+
def __init__(self, csv_file: str):
|
| 6 |
"""
|
| 7 |
Read csv and create the graph in function of the given mode
|
| 8 |
"""
|
| 9 |
self.csv = pd.read_csv(csv_file, sep="\t")
|
| 10 |
+
self.data = self.generateGraph()
|
|
|
|
| 11 |
|
| 12 |
+
def generateGraph(self):
|
| 13 |
"""
|
| 14 |
+
Create a graph by browsing the data retrieved in the csv
|
| 15 |
Returns:
|
| 16 |
+
(Dict[str, Dict[str, float]]): The graph
|
| 17 |
"""
|
| 18 |
graph: Dict[str, Dict[str, float]] = {}
|
| 19 |
|
app/travel_resolver/libs/speech2text/interface.py
CHANGED
|
@@ -22,6 +22,15 @@ def transcribe(audio):
|
|
| 22 |
|
| 23 |
return transcriber({"sampling_rate": sr, "raw": y})["text"]
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def getDijkstraResult(depart, destination):
|
| 26 |
"""
|
| 27 |
Args:
|
|
@@ -31,27 +40,50 @@ def getDijkstraResult(depart, destination):
|
|
| 31 |
Returns:
|
| 32 |
(str): Time of the shortest travel found
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
graph = Graph(dijkstraGraphData.data)
|
| 36 |
distances = graph.RunDijkstra(depart)
|
| 37 |
if(destination in distances): return str(distances[destination]) + " minutes"
|
| 38 |
return "Temps non trouvé"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def submit(audio, departV, destinationV):
|
| 41 |
promptValue = transcribe(audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return {
|
| 43 |
prompt: gr.TextArea(label="Prompt", value=promptValue, visible=True),
|
| 44 |
depart: gr.Textbox(label="Départ", visible=True),
|
| 45 |
destination: gr.Textbox(label="Destination", visible=True),
|
| 46 |
-
timeDijkstra: gr.Textbox(label="Temps trouvé", value=
|
|
|
|
|
|
|
| 47 |
}
|
|
|
|
| 48 |
def clear():
|
| 49 |
return {
|
| 50 |
timeDijkstra:gr.HTML("<p>Aucun prompt renseigné</p>"),
|
| 51 |
timeAStar:gr.HTML("<p>Aucun prompt renseigné</p>"),
|
| 52 |
prompt: gr.TextArea(label="Prompt", visible=False),
|
| 53 |
depart:gr.Textbox(label="Départ", visible=False, value="Gare de Amiens"),
|
| 54 |
-
destination: gr.Textbox(label="Destination", visible=False, value="Gare de Jeumont")
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
with gr.Blocks() as interface:
|
|
@@ -67,15 +99,16 @@ with gr.Blocks() as interface:
|
|
| 67 |
timeDijkstra=gr.HTML("<p>Aucun prompt renseigné</p>")
|
| 68 |
with gr.Tab("AStar"):
|
| 69 |
timeAStar=gr.HTML("<p>Aucun prompt renseigné</p>")
|
|
|
|
| 70 |
submitButton.click(
|
| 71 |
submit,
|
| 72 |
[audio, depart, destination],
|
| 73 |
-
[prompt, depart, destination, timeDijkstra],
|
| 74 |
)
|
| 75 |
audio.clear(
|
| 76 |
clear,
|
| 77 |
[],
|
| 78 |
-
[timeDijkstra, timeAStar, prompt, depart, destination]
|
| 79 |
)
|
| 80 |
interface.launch()
|
| 81 |
|
|
|
|
| 22 |
|
| 23 |
return transcriber({"sampling_rate": sr, "raw": y})["text"]
|
| 24 |
|
| 25 |
+
def getCSVTravelGraph():
|
| 26 |
+
"""
|
| 27 |
+
Generate Graph with the csv dataset
|
| 28 |
+
Returns:
|
| 29 |
+
(Graph): Graph
|
| 30 |
+
"""
|
| 31 |
+
graphData = CSVTravelGraph("../data/sncf/timetables.csv")
|
| 32 |
+
return Graph(graphData.data)
|
| 33 |
+
|
| 34 |
def getDijkstraResult(depart, destination):
|
| 35 |
"""
|
| 36 |
Args:
|
|
|
|
| 40 |
Returns:
|
| 41 |
(str): Time of the shortest travel found
|
| 42 |
"""
|
| 43 |
+
graph = getCSVTravelGraph()
|
|
|
|
| 44 |
distances = graph.RunDijkstra(depart)
|
| 45 |
if(destination in distances): return str(distances[destination]) + " minutes"
|
| 46 |
return "Temps non trouvé"
|
| 47 |
|
| 48 |
+
def getAStarResult(depart, destination):
|
| 49 |
+
"""
|
| 50 |
+
Args:
|
| 51 |
+
depart (str): station name
|
| 52 |
+
destination (str): station name
|
| 53 |
+
Generate AStarGraph and find the shortest way for the destination
|
| 54 |
+
Returns:
|
| 55 |
+
(str): Time of the shortest travel found
|
| 56 |
+
"""
|
| 57 |
+
graph = getCSVTravelGraph()
|
| 58 |
+
heuristic = graph.RunDijkstra(destination)
|
| 59 |
+
path, cost = graph.RunAStar(depart,destination, heuristic)
|
| 60 |
+
if(destination in cost): return [path, str(cost[destination]) + " minutes"]
|
| 61 |
+
return [[],"Temps non trouvé"]
|
| 62 |
+
|
| 63 |
def submit(audio, departV, destinationV):
|
| 64 |
promptValue = transcribe(audio)
|
| 65 |
+
dijkstraResult = getDijkstraResult(departV, destinationV)
|
| 66 |
+
AStarPath, AStarCost = getAStarResult(departV, destinationV)
|
| 67 |
+
print(AStarPath)
|
| 68 |
+
AStarPathFormatted = "\n".join([f"{i+1}. {elem}" for i, elem in enumerate(AStarPath)])
|
| 69 |
+
print(AStarPathFormatted)
|
| 70 |
return {
|
| 71 |
prompt: gr.TextArea(label="Prompt", value=promptValue, visible=True),
|
| 72 |
depart: gr.Textbox(label="Départ", visible=True),
|
| 73 |
destination: gr.Textbox(label="Destination", visible=True),
|
| 74 |
+
timeDijkstra: gr.Textbox(label="Temps trouvé", value=dijkstraResult),
|
| 75 |
+
timeAStar: gr.Textbox(label="Temps trouvé", value=AStarCost),
|
| 76 |
+
path: gr.Textbox(label="Chemin emprunté", value=AStarPathFormatted, visible=True, lines=len(AStarPath))
|
| 77 |
}
|
| 78 |
+
|
| 79 |
def clear():
|
| 80 |
return {
|
| 81 |
timeDijkstra:gr.HTML("<p>Aucun prompt renseigné</p>"),
|
| 82 |
timeAStar:gr.HTML("<p>Aucun prompt renseigné</p>"),
|
| 83 |
prompt: gr.TextArea(label="Prompt", visible=False),
|
| 84 |
depart:gr.Textbox(label="Départ", visible=False, value="Gare de Amiens"),
|
| 85 |
+
destination: gr.Textbox(label="Destination", visible=False, value="Gare de Jeumont"),
|
| 86 |
+
path: gr.Textbox(label="Chemin emprunté", value="", visible=False)
|
| 87 |
}
|
| 88 |
|
| 89 |
with gr.Blocks() as interface:
|
|
|
|
| 99 |
timeDijkstra=gr.HTML("<p>Aucun prompt renseigné</p>")
|
| 100 |
with gr.Tab("AStar"):
|
| 101 |
timeAStar=gr.HTML("<p>Aucun prompt renseigné</p>")
|
| 102 |
+
path=gr.Textbox(label="Chemin emprunté", visible=False)
|
| 103 |
submitButton.click(
|
| 104 |
submit,
|
| 105 |
[audio, depart, destination],
|
| 106 |
+
[prompt, depart, destination, timeDijkstra, timeAStar, path],
|
| 107 |
)
|
| 108 |
audio.clear(
|
| 109 |
clear,
|
| 110 |
[],
|
| 111 |
+
[timeDijkstra, timeAStar, prompt, depart, destination, path]
|
| 112 |
)
|
| 113 |
interface.launch()
|
| 114 |
|