Az-r-ow
commited on
Commit
·
4e0aa6b
1
Parent(s):
8115897
WIP: refactoring the interface
Browse files- app/__init__.py +0 -0
- app/app.py +55 -42
- app/helpers/__init__.py +0 -0
- app/{global_vars.py → helpers/global_vars.py} +1 -1
app/__init__.py
ADDED
|
File without changes
|
app/app.py
CHANGED
|
@@ -3,7 +3,7 @@ from transformers import pipeline
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
from travel_resolver.libs.nlp.ner.models import BiLSTM_NER, LSTM_NER, CamemBERT_NER
|
| 6 |
-
from .global_vars import entities_label_mapping, PROGRESS, HTML_COMPONENTS
|
| 7 |
from travel_resolver.libs.nlp.ner.data_processing import process_sentence
|
| 8 |
from travel_resolver.libs.pathfinder.CSVTravelGraph import CSVTravelGraph
|
| 9 |
from travel_resolver.libs.pathfinder.graph import Graph
|
|
@@ -15,52 +15,66 @@ transcriber = pipeline(
|
|
| 15 |
|
| 16 |
models = {"LSTM": LSTM_NER(), "BiLSTM": BiLSTM_NER(), "CamemBERT": CamemBERT_NER()}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
-
with gr.Column()
|
| 20 |
with gr.Row():
|
| 21 |
-
audio = gr.Audio(label="Fichier audio")
|
| 22 |
file = gr.File(
|
| 23 |
-
label="Fichier texte",
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
model = gr.Dropdown(
|
| 27 |
-
label="Modèle NER",
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
if audio:
|
| 33 |
-
render_tabs([transcribe(audio)], model, gr.Progress())
|
| 34 |
-
elif file:
|
| 35 |
-
with open(file.name, "r") as f:
|
| 36 |
-
sentences = f.read().split("\n")
|
| 37 |
-
render_tabs(sentences, model, gr.Progress())
|
| 38 |
-
|
| 39 |
-
@gr.render(inputs=[audio, model], triggers=[audio.change])
|
| 40 |
-
def handle_audio(audio, model, progress=gr.Progress()):
|
| 41 |
-
progress(
|
| 42 |
-
0,
|
| 43 |
-
)
|
| 44 |
-
promptAudio = transcribe(audio)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
@gr.render(
|
| 51 |
-
inputs=[file, model],
|
| 52 |
-
triggers=[file.upload],
|
| 53 |
-
)
|
| 54 |
-
def handle_file(file, model, progress=gr.Progress()):
|
| 55 |
-
progress(0, desc=PROGRESS.ANALYZING_FILE.value)
|
| 56 |
-
time.sleep(1)
|
| 57 |
-
if file is not None:
|
| 58 |
-
with open(file.name, "r") as f:
|
| 59 |
-
progress(0.33, desc=PROGRESS.READING_FILE.value)
|
| 60 |
-
file_content = f.read()
|
| 61 |
-
rows = file_content.split("\n")
|
| 62 |
-
sentences = [row for row in rows if row]
|
| 63 |
-
render_tabs(sentences, model, progress)
|
| 64 |
|
| 65 |
|
| 66 |
def handleCityChange(city):
|
|
@@ -207,10 +221,8 @@ def getDepartureAndArrivalFromText(text: str, model: str):
|
|
| 207 |
|
| 208 |
def render_tabs(sentences: list[str], model: str, progress_bar: gr.Progress):
|
| 209 |
idx = 0
|
| 210 |
-
with gr.
|
| 211 |
-
for sentence in progress_bar.tqdm(
|
| 212 |
-
sentences, desc=PROGRESS.PROCESSING_SENTENCES.value
|
| 213 |
-
):
|
| 214 |
with gr.Tab(f"Sentence {idx}"):
|
| 215 |
dep, arr = getDepartureAndArrivalFromText(sentence, model)
|
| 216 |
entities = []
|
|
@@ -310,6 +322,7 @@ def render_tabs(sentences: list[str], model: str, progress_bar: gr.Progress):
|
|
| 310 |
)
|
| 311 |
|
| 312 |
idx += 1
|
|
|
|
| 313 |
|
| 314 |
|
| 315 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
from travel_resolver.libs.nlp.ner.models import BiLSTM_NER, LSTM_NER, CamemBERT_NER
|
| 6 |
+
from helpers.global_vars import entities_label_mapping, PROGRESS, HTML_COMPONENTS
|
| 7 |
from travel_resolver.libs.nlp.ner.data_processing import process_sentence
|
| 8 |
from travel_resolver.libs.pathfinder.CSVTravelGraph import CSVTravelGraph
|
| 9 |
from travel_resolver.libs.pathfinder.graph import Graph
|
|
|
|
| 15 |
|
| 16 |
models = {"LSTM": LSTM_NER(), "BiLSTM": BiLSTM_NER(), "CamemBERT": CamemBERT_NER()}
|
| 17 |
|
| 18 |
+
|
| 19 |
+
def handle_model_change(audio, file, model):
|
| 20 |
+
if audio:
|
| 21 |
+
render_tabs([transcribe(audio)], model, gr.Progress())
|
| 22 |
+
elif file:
|
| 23 |
+
with open(file.name, "r") as f:
|
| 24 |
+
sentences = f.read().split("\n")
|
| 25 |
+
return render_tabs(sentences, model, gr.Progress())
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def handle_audio(audio, model, progress=gr.Progress()):
|
| 29 |
+
progress(
|
| 30 |
+
0,
|
| 31 |
+
)
|
| 32 |
+
promptAudio = transcribe(audio)
|
| 33 |
+
|
| 34 |
+
time.sleep(1)
|
| 35 |
+
|
| 36 |
+
return render_tabs([promptAudio], model, progress)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def handle_file(file, model, progress=gr.Progress()):
|
| 40 |
+
print("file upload")
|
| 41 |
+
progress(0, desc=PROGRESS.ANALYZING_FILE.value)
|
| 42 |
+
time.sleep(1)
|
| 43 |
+
if file is not None:
|
| 44 |
+
with open(file.name, "r") as f:
|
| 45 |
+
progress(0.33, desc=PROGRESS.READING_FILE.value)
|
| 46 |
+
file_content = f.read()
|
| 47 |
+
rows = file_content.split("\n")
|
| 48 |
+
sentences = [row for row in rows if row]
|
| 49 |
+
return render_tabs(sentences, model, progress)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
tabs_components = []
|
| 53 |
+
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
+
with gr.Column():
|
| 56 |
with gr.Row():
|
| 57 |
+
audio = gr.Audio(label="Fichier audio", interactive=True)
|
| 58 |
file = gr.File(
|
| 59 |
+
label="Fichier texte",
|
| 60 |
+
file_types=["text"],
|
| 61 |
+
file_count="single",
|
| 62 |
+
interactive=True,
|
| 63 |
)
|
| 64 |
|
| 65 |
model = gr.Dropdown(
|
| 66 |
+
label="Modèle NER",
|
| 67 |
+
choices=models.keys(),
|
| 68 |
+
value="CamemBERT",
|
| 69 |
+
interactive=True,
|
| 70 |
)
|
| 71 |
|
| 72 |
+
with gr.Column() as tabs:
|
| 73 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
audio.upload(handle_audio, inputs=[audio, model], outputs=[tabs])
|
| 76 |
+
file.upload(handle_file, inputs=[file, model], outputs=[tabs])
|
| 77 |
+
model.change(handle_model_change, inputs=[audio, file, model], outputs=[tabs])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
def handleCityChange(city):
|
|
|
|
| 221 |
|
| 222 |
def render_tabs(sentences: list[str], model: str, progress_bar: gr.Progress):
|
| 223 |
idx = 0
|
| 224 |
+
with gr.Column() as tabs:
|
| 225 |
+
for sentence in progress_bar.tqdm(sentences, desc=PROGRESS.PROCESSING.value):
|
|
|
|
|
|
|
| 226 |
with gr.Tab(f"Sentence {idx}"):
|
| 227 |
dep, arr = getDepartureAndArrivalFromText(sentence, model)
|
| 228 |
entities = []
|
|
|
|
| 322 |
)
|
| 323 |
|
| 324 |
idx += 1
|
| 325 |
+
return tabs
|
| 326 |
|
| 327 |
|
| 328 |
if __name__ == "__main__":
|
app/helpers/__init__.py
ADDED
|
File without changes
|
app/{global_vars.py → helpers/global_vars.py}
RENAMED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from enum import Enum
|
| 2 |
|
| 3 |
|
| 4 |
-
class
|
| 5 |
ANALYZING_FILE = "Analyzing file..."
|
| 6 |
ANALYZING_AUDIO = "Analyzing audio..."
|
| 7 |
READING_FILE = "Reading file..."
|
|
|
|
| 1 |
from enum import Enum
|
| 2 |
|
| 3 |
|
| 4 |
+
class PROGRESS(Enum):
|
| 5 |
ANALYZING_FILE = "Analyzing file..."
|
| 6 |
ANALYZING_AUDIO = "Analyzing audio..."
|
| 7 |
READING_FILE = "Reading file..."
|